Last active
December 31, 2015 16:59
-
-
Save kawasima/8017193 to your computer and use it in GitHub Desktop.
Convert a cell value to a suitable type for Clojure and retrieve it, using POI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import [org.apache.poi.ss.usermodel Cell DateUtil FormulaError])) | |
(defmulti cell-value (fn [cell] | |
(let [type (.getCellType cell)] | |
(if (= type Cell/CELL_TYPE_FORMULA) | |
(.getCachedFormulaResultType cell) | |
type)))) | |
(defmethod cell-value Cell/CELL_TYPE_STRING [cell] | |
(.getStringCellValue cell)) | |
(defmethod cell-value Cell/CELL_TYPE_BOOLEAN [cell] | |
(.getBooleanCellValue cell)) | |
(defmethod cell-value Cell/CELL_TYPE_NUMERIC [cell] | |
(if (DateUtil/isCellDateFormatted cell) | |
(.getDateCellValue cell) | |
(.getNumericCellValue cell))) | |
(defmethod cell-value Cell/CELL_TYPE_ERROR [cell] | |
(-> cell (.getErrorCellValue) (FormulaError/forInt))) | |
(defmethod cell-value Cell/CELL_TYPE_BLANK [cell] nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment