Skip to content

Instantly share code, notes, and snippets.

@kawasima
Last active December 31, 2015 16:59
Show Gist options
  • Save kawasima/8017193 to your computer and use it in GitHub Desktop.
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.
(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