-
Notifications
You must be signed in to change notification settings - Fork 9
/
read_xlsx.clj
142 lines (127 loc) · 5.51 KB
/
read_xlsx.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(ns zero-one.fxl.read-xlsx
(:require
[failjure.core :as f]
[zero-one.fxl.alignments :as alignments]
[zero-one.fxl.borders :as borders]
[zero-one.fxl.colours :as colours]
[zero-one.fxl.defaults :as defaults])
(:import
[java.io FileInputStream]
[org.apache.poi.xssf.usermodel XSSFWorkbook]
[org.apache.poi.ss.usermodel Font]))
(defn- extract-cell-value [cell]
(let [cell-type (.. cell getCellType name)]
(case cell-type
"NUMERIC" (.getNumericCellValue cell)
"STRING" (.getStringCellValue cell)
"BOOLEAN" (.getBooleanCellValue cell)
"ERROR" (.getErrorCellString cell)
"FORMULA" (try
(.getNumericCellValue cell)
(catch Exception _ (.getRawValue cell)))
nil)))
(defn- extract-cell-border-style [cell]
(let [cell-style (.getCellStyle cell)]
{:bottom-border
{:style (-> cell-style .getBorderBottom borders/border-style-lookup)
:colour (-> cell-style .getBottomBorderColor colours/colours-lookup)}
:left-border
{:style (-> cell-style .getBorderLeft borders/border-style-lookup)
:colour (-> cell-style .getLeftBorderColor colours/colours-lookup)}
:right-border
{:style (-> cell-style .getBorderRight borders/border-style-lookup)
:colour (-> cell-style .getRightBorderColor colours/colours-lookup)}
:top-border
{:style (-> cell-style .getBorderTop borders/border-style-lookup)
:colour (-> cell-style .getTopBorderColor colours/colours-lookup)}}))
(defn- extract-cell-alignment-style [cell]
(let [cell-style (.getCellStyle cell)]
{:horizontal (-> cell-style
.getAlignment
alignments/horizontal-alignment-lookup)
:vertical (-> cell-style
.getVerticalAlignment
alignments/vertical-alignment-lookup)}))
(defn- extract-cell-font-style [workbook cell]
(let [font-index (-> cell .getCellStyle .getFontIndexAsInt)
font (.getFontAt workbook font-index)]
{:bold (.getBold font)
:italic (.getItalic font)
:strikeout (.getStrikeout font)
:underline (not= (.getUnderline font) Font/U_NONE)
:font-name (.getFontName font)
:font-colour (-> font .getColor colours/colours-lookup)
:font-size (.getFontHeightInPoints font)}))
(defn- prune-cell-style [cell-style]
(into {}
(filter
(fn [[k v]] (not= v (defaults/style k)))
cell-style)))
(defn- extract-cell-style [workbook cell]
(let [col-index (.getColumnIndex cell)
cell-style (merge
(extract-cell-border-style cell)
(extract-cell-alignment-style cell)
(extract-cell-font-style workbook cell)
{:background-colour (-> cell
.getCellStyle
.getFillForegroundColor
colours/colours-lookup)
:data-format (-> cell
.getCellStyle
.getDataFormatString)
:col-size (-> cell
.getSheet
(.getColumnWidth col-index)
(/ 256.0)
int)
:row-size (-> cell
.getRow
.getHeightInPoints
int)})]
(prune-cell-style cell-style)))
(defn- extract-poi-cells [workbook]
(letfn [(->seq [iterable] (-> iterable .iterator iterator-seq))]
(->> workbook
->seq
(mapcat ->seq)
(mapcat ->seq))))
(defn- poi-cell->fxl-cell [workbook poi-cell]
(let [poi-row (.getRow poi-cell)]
{:coord {:row (.getRowNum poi-row)
:col (.getColumnIndex poi-cell)
:sheet (.. poi-row getSheet getSheetName)}
:value (extract-cell-value poi-cell)
:style (extract-cell-style workbook poi-cell)}))
(defn- throwable-read-xlsx! [path]
(let [input-stream (FileInputStream. path)
workbook (XSSFWorkbook. input-stream)
poi-cells (extract-poi-cells workbook)
cells (map #(poi-cell->fxl-cell workbook %) poi-cells)]
(.close workbook)
cells))
(defn read-xlsx! [path]
(f/try* (throwable-read-xlsx! path)))
(comment
(def cells (read-xlsx! "test/resources/dummy-spreadsheet.xlsx"))
(map clojure.pprint/pprint (set (map :style cells)))
(clojure.pprint/pprint (filter #(-> % :coord :row (= 4)) cells))
(def cell (first cells))
(def cell-style (:style cell))
(set (map :value cells))
(def workbook (XSSFWorkbook. "test/resources/dummy-spreadsheet.xlsx"))
(def sheet (.getSheet workbook "Sheet1"))
(def row (.getRow sheet 4))
(def cell (.getCell row 2))
(-> cell .getCellStyle .getFillPattern)
(-> cell .getCellStyle .getFillBackgroundColor)
(-> cell .getCellStyle .getFillForegroundColor colours/colours-lookup)
(count (map #(.getIndex % ) (vals colours/colours)))
(count colours/colours)
(def cells (-> row .iterator iterator-seq))
(def cell (first cells))
(def cell-style (.getCellStyle cell))
(-> cell-style .getFillBackgroundColor colours/colours-lookup)
(def cells (read-xlsx! "test/resources/dummy-spreadsheet.xlsx"))
(map (comp :background-colour :style) cells)
(println "end"))