Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for streaming API. #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/dk/ative/docjure/spreadsheet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(java.io FileOutputStream FileInputStream InputStream OutputStream)
(java.util Date Calendar)
(org.apache.poi.xssf.usermodel XSSFWorkbook)
(org.apache.poi.xssf.streaming SXSSFWorkbook)
(org.apache.poi.hssf.usermodel HSSFWorkbook)
(org.apache.poi.ss.usermodel Workbook Sheet Cell Row
FormulaError
Expand Down Expand Up @@ -186,7 +187,7 @@
(when new-key
{new-key (read-cell cell)})))

(defn select-columns
(defn select-columns
"Takes two arguments: column hashmap and a sheet. The column hashmap
specifies the mapping from spreadsheet columns dictionary keys:
its keys are the spreadsheet column names and the values represent
Expand Down Expand Up @@ -289,6 +290,40 @@
(add-rows! sheet data)
workbook))

(defn with-streaming-workbook!
"Creates new XLSX workbook.
body-fn takes as an argument function that appends new
row to the worksheet. Works in streaming mode with constant
memory requirements.

Example:
(with-streaming-workbook! \"Test.xlsx\" \"SHEETNAME\"
(fn [append-row!]
(doseq [i (range 2000)]
(append-row! (range i)))))"
[file-name sheet-name body-fn]
(let [wb (SXSSFWorkbook. 1)
^Sheet sh (add-sheet! wb sheet-name)
row-num (atom 0)
append-row!
(fn [row-data]
(let [r (.createRow sh @row-num)]
(swap! row-num inc)
(doseq [[j value] (map-indexed #(list %1 %2) row-data)]
(set-cell! (.createCell r j) value))))]
(try
(body-fn append-row!)
(save-workbook-into-file! file-name wb)
(finally
(.dispose wb)))))

(defn save-data-to-xlsx!
"Create new XLSX workbook and stream data into the sheet."
[file-name sheet-name data]
(with-streaming-workbook! file-name sheet-name
(fn [append-row!]
(doseq [row data] (append-row! row)))))

(defn create-xls-workbook
"Create a new XLS workbook with a single sheet and the data specified."
[sheet-name data]
Expand Down