From b595d6259dbeabbb8fe1fcb5bad66c68ef5ee18f Mon Sep 17 00:00:00 2001 From: Sid Kurias Date: Fri, 28 Jun 2013 16:28:48 +0530 Subject: [PATCH] Reads an entire sheet and returns a clojure map. Added two functions to read a sheet and return contents as a clojure map. The map is a nested map. The outer map is keyed on row number and the inner map by column number. --- src/net/cgrand/spreadmap.clj | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/net/cgrand/spreadmap.clj b/src/net/cgrand/spreadmap.clj index 0d1686b..b3cc1ba 100644 --- a/src/net/cgrand/spreadmap.clj +++ b/src/net/cgrand/spreadmap.clj @@ -1,6 +1,7 @@ (ns net.cgrand.spreadmap (:require [clojure.java.io :as io]) - (:import [org.apache.poi.ss.usermodel Workbook WorkbookFactory CellValue DateUtil Cell] + (:import [org.apache.poi.ss.usermodel Workbook WorkbookFactory CellValue + DateUtil Cell Row Sheet] [org.apache.poi.ss.formula.eval ValueEval StringEval BoolEval NumberEval BlankEval ErrorEval] [org.apache.poi.ss.formula IStabilityClassifier EvaluationWorkbook EvaluationSheet EvaluationName EvaluationCell FormulaParser FormulaType] [org.apache.poi.ss.util CellReference AreaReference])) @@ -174,4 +175,27 @@ BlankEval (value [v wb cref] nil)) -(defn fm= [formula-string] {:formula formula-string}) \ No newline at end of file +(defn fm= [formula-string] {:formula formula-string}) + +(defn- get-cells [ss sh row] + (let [fc (.getFirstCellNum row) + lc (.getLastCellNum row) + rnum (.getRowNum row)] + (map (fn [c] {(str (CellReference/convertNumToColString c)) + (.valAt ss [sh rnum c])}) + (range fc lc)))) + +(defn read-sheet + "Given a SpreadSheet and a sheet index, will read the entire sheet and + return contents as a nested map. The outer map is keyed by the row number + and the inner map by the column name." + [^SpreadSheet ss ^java.lang.Integer idx] + (let [sheet (-> (.wb ss) (.getSheetAt idx)) + fr (.getFirstRowNum sheet) + lr (.getLastRowNum sheet)] + (into {} + (map (fn [r] + (let [row (.getRow sheet r)] + ;;inc row number as it is 0 based. + {(inc (.getRowNum row)) (into {} (get-cells ss idx row))})) + (range fr (inc lr))))))