Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add an unsupported, alpha, hssf output format

  • Loading branch information...
commit cd3b543ae893f3d607a815d67183398f7bc43102 1 parent d65790e
@joegallo authored
Showing with 51 additions and 0 deletions.
  1. +1 −0  project.clj
  2. +50 −0 src/doric/hssf.clj
View
1  project.clj
@@ -1,4 +1,5 @@
(defproject doric "0.7.0-SNAPSHOT"
:description "Clojure table layout"
:dependencies [[org.clojure/clojure "1.3.0"]]
+ :dev-dependencies [[org.apache.poi/poi "3.7"]]
:multi-deps {"1.2" [[org.clojure/clojure "1.2.1"]]})
View
50 src/doric/hssf.clj
@@ -0,0 +1,50 @@
+(ns doric.hssf
+ (:import (org.apache.poi.hssf.usermodel HSSFSheet HSSFWorkbook)
+ (org.apache.poi.ss.usermodel CellStyle Font)))
+
+(defn th [col]
+ [(:title col) col])
+
+(defn td [col row]
+ [(row (:name col)) col])
+
+(defn cell-style [wb align & [font]]
+ (let [cs (.createCellStyle wb)]
+ (.setAlignment cs (condp = align
+ :left CellStyle/ALIGN_LEFT
+ :center CellStyle/ALIGN_CENTER
+ :right CellStyle/ALIGN_RIGHT))
+ (when font
+ (.setFont cs font))
+ cs))
+
+(defn render [table]
+ (let [wb (HSSFWorkbook.)
+ ^HSSFSheet sh (.createSheet wb "Sheet1")
+ ^Font bold (.createFont wb)
+ _ (.setBoldweight bold Font/BOLDWEIGHT_BOLD)]
+ (let [row (.createRow sh 0)]
+ (doseq [[j [s m]] (map-indexed vector (first table))]
+ (let [c (.createCell row j)]
+ (.setCellValue c s)
+ (.setCellStyle c (cell-style wb (:title-align m) bold)))))
+ (doseq [[i r] (map-indexed vector (rest table))]
+ (let [row (.createRow sh (inc i))]
+ (doseq [[j [s m]] (map-indexed vector r)]
+ (let [c (.createCell row j)]
+ (.setCellValue c (if (number? s)
+ (double s)
+ s))
+ (.setCellStyle c (cell-style wb (:align m)))))))
+ wb))
+
+;; this is an undocumented alpha format that i wrote primarily just to
+;; see how well doric could handle it -- if you use it, please be
+;; aware that this is not supported in any way. that said, you could
+;; use it like this, more or less:
+
+#_
+(with-open [f (java.io.FileOutputStream. "out.xls")]
+ (.write (table* ^{:format 'doric.hssf} [{:a 1 :b 2}{:a 3 :b 4}])))
+
+;; fun, huh?
Please sign in to comment.
Something went wrong with that request. Please try again.