Skip to content

Commit 6b3b728

Browse files
authored
Add support for LocalDate/LocalDateTime (#9)
- upgrade poi version to 4.1.2 for LocalDate/LocalDateTime support
1 parent 1f94466 commit 6b3b728

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:dependencies [[org.clojure/clojure "1.10.1"]
77
[com.taoensso/tufte "2.0.1"]
8-
[org.apache.poi/poi-ooxml "4.0.0"]
8+
[org.apache.poi/poi-ooxml "4.1.2"]
99
[org.jodconverter/jodconverter-core "4.0.0-RELEASE"]])

src/excel_clj/poi.clj

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
[taoensso.encore :as enc]
1313
[taoensso.tufte :as tufte])
1414
(:import (java.io Closeable BufferedInputStream InputStream)
15-
(org.apache.poi.ss.usermodel RichTextString Sheet Cell Row Workbook)
15+
(org.apache.poi.ss.usermodel RichTextString Sheet Cell Row Workbook DateUtil)
1616
(java.util Date Calendar)
17+
(java.time LocalDate LocalDateTime)
1718
(org.apache.poi.ss.util CellRangeAddress)
1819
(org.apache.poi.xssf.streaming SXSSFWorkbook)
1920
(org.apache.poi.xssf.usermodel XSSFWorkbook)))
@@ -91,7 +92,7 @@
9192
[^Cell cell data]
9293
;; These types are allowed natively
9394
(if-type
94-
[data [Boolean Calendar String Date Double RichTextString]]
95+
[data [Boolean Calendar String Date LocalDate LocalDateTime Double RichTextString]]
9596
(doto cell (.setCellValue data))
9697

9798
;; Apache POI requires that numbers be doubles
@@ -309,10 +310,12 @@
309310
; the template sheet to overwrite completely
310311
sh (sheet-writer w "raw")]
311312

312-
(doseq [header ["Date" ; use the same headers as in the template
313+
(doseq [header ["Date"
313314
"Webserver Uptime"
314315
"REST API Uptime"
315-
"WebSocket API Uptime"]]
316+
"WebSocket API Uptime"
317+
"Local Datetime"
318+
"Local Date"]]
316319
(write! sh header))
317320

318321
(newline! sh)
@@ -322,13 +325,18 @@
322325
one-hour (* 1000 60 60)]
323326
(dotimes [i 99]
324327
(let [row-ts (+ start-ts (* i one-hour))
325-
ymd {:data-format :ymd :alignment :left}]
326-
(write! sh (Date. ^long row-ts) ymd 1 1))
327-
328-
; random uptime values
329-
(write! sh (- 1.0 (rand 0.25)))
330-
(write! sh (- 1.0 (rand 0.25)))
331-
(write! sh (- 1.0 (rand 0.25)))
328+
ymd {:data-format :ymd :alignment :left}
329+
dt {:data-format :datetime :alignment :left}]
330+
(write! sh (Date. ^long row-ts) ymd 1 1)
331+
332+
; random uptime values
333+
(write! sh (- 1.0 (rand 0.25)))
334+
(write! sh (- 1.0 (rand 0.25)))
335+
(write! sh (- 1.0 (rand 0.25)))
336+
337+
; LocalDate / LocalDateTime value
338+
(write! sh (DateUtil/toLocalDateTime (Date. ^long row-ts)) dt 1 1)
339+
(write! sh (.toLocalDate (DateUtil/toLocalDateTime (Date. ^long row-ts))) ymd 1 1))
332340
(newline! sh))))))
333341

334342

src/excel_clj/style.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
{:accounting "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"
192192
:number "#.###############"
193193
:ymd "yyyy-MM-dd"
194+
:datetime "yyyy-MM-dd hh:mm:ss"
194195
:percent "0.00%"})
195196

196197

0 commit comments

Comments
 (0)