From df3e52da1aac92c4bc3d016629b9ba34dc06ef24 Mon Sep 17 00:00:00 2001 From: bagl Date: Mon, 18 Apr 2016 14:06:23 -0700 Subject: [PATCH 1/3] Add support for streaming API. --- src/dk/ative/docjure/spreadsheet.clj | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/dk/ative/docjure/spreadsheet.clj b/src/dk/ative/docjure/spreadsheet.clj index f5e7c16..b4487b0 100644 --- a/src/dk/ative/docjure/spreadsheet.clj +++ b/src/dk/ative/docjure/spreadsheet.clj @@ -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 @@ -289,6 +290,38 @@ (add-rows! sheet data) workbook)) +(defmacro with-streaming-workbook! + "Creates new XLSX workbook. + Use function append-row! to append new row to the worksheet. + Works in streaming mode with constant memory requirements. + Needs enough memory to fit in the biggest row. + + Example: + (with-streaming-workbook! \"Test.xlsx\" \"SHEETNAME\" + (doseq [i (range 2000)] + (append-row! (range i))))" + [file-name sheet-name & body] + `(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 + (do ~@body) + (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 + (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] From f9ae849047723e0aa13687d40d5bf4da0a37090b Mon Sep 17 00:00:00 2001 From: Petr Vapenka Date: Tue, 19 Apr 2016 10:22:04 +0200 Subject: [PATCH 2/3] Make with-streaming-workbook! a function --- src/dk/ative/docjure/spreadsheet.clj | 46 +++++++++++++++------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/dk/ative/docjure/spreadsheet.clj b/src/dk/ative/docjure/spreadsheet.clj index b4487b0..8ec643a 100644 --- a/src/dk/ative/docjure/spreadsheet.clj +++ b/src/dk/ative/docjure/spreadsheet.clj @@ -290,37 +290,39 @@ (add-rows! sheet data) workbook)) -(defmacro with-streaming-workbook! +(defn with-streaming-workbook! "Creates new XLSX workbook. - Use function append-row! to append new row to the worksheet. - Works in streaming mode with constant memory requirements. - Needs enough memory to fit in the biggest row. + 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\" - (doseq [i (range 2000)] - (append-row! (range i))))" - [file-name sheet-name & body] - `(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 - (do ~@body) - (save-workbook-into-file! ~file-name wb#) - (finally - (.dispose wb#))))) + (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 - (doseq [row data] (append-row! row)))) + (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." From 21b3ab3ebc5d702be31584bab60ef401e7b0e9bb Mon Sep 17 00:00:00 2001 From: Petr Vapenka Date: Tue, 19 Apr 2016 10:22:49 +0200 Subject: [PATCH 3/3] Remove trailing whitespace --- src/dk/ative/docjure/spreadsheet.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dk/ative/docjure/spreadsheet.clj b/src/dk/ative/docjure/spreadsheet.clj index 8ec643a..b11afca 100644 --- a/src/dk/ative/docjure/spreadsheet.clj +++ b/src/dk/ative/docjure/spreadsheet.clj @@ -187,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