-
-
Save terjesb/3369009 to your computer and use it in GitHub Desktop.
Creating and updating Excel files with Clojure/poi.apache.org
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
((ns excel.core | |
(:import [org.apache.poi.hssf.usermodel HSSFWorkbook HSSFSheet HSSFRow | |
HSSFRichTextString HSSFFont HSSFDataFormat | |
HSSFCellStyle HSSFCell]) | |
(:import [java.io FileOutputStream FileInputStream IOException]) | |
#_(:import [org.apache.poi.ss.util CellRangeAdrress])) | |
(defn make-excel [file-name] | |
(let [wb (HSSFWorkbook.) | |
s (.createSheet wb)] | |
(.setSheetName wb 0 "HSSF Test") | |
(dorun (for [idx (range 100)] | |
(let [row (.createRow s idx)] | |
(dorun (for [col (range 100)] | |
(let [c (.createCell row col)] | |
(.setCellValue c (double (* idx col))))))))) | |
(with-open [out (FileOutputStream. file-name)] | |
(.write wb out)) | |
)) | |
(defn update-excel [file-name] | |
(let [wb (HSSFWorkbook. (FileInputStream. file-name)) | |
s (.getSheetAt wb 0) | |
last-row (.getLastRowNum s) | |
row (.createRow s (inc last-row)) ; .getRow | |
cell (.createCell row 0) ; .getCell | |
date-style (doto (.createCellStyle wb) | |
(.setDataFormat (-> wb .getCreationHelper .createDataFormat (.getFormat "yyyy-m-d h:mm"))))] | |
;Java: date-style.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-m-d h:mm")); | |
(doto cell | |
(.setCellStyle date-style) | |
(.setCellValue (java.util.Date.))) | |
(.setCellValue (.createCell row 1) "I was here") | |
(with-open [out (FileOutputStream. file-name)] | |
(.write wb out)))) | |
(defn -main [] | |
(make-excel "testing.xls") | |
(update-excel "update.xls") | |
) |
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
(defproject excel "1.0.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:dependencies [[org.clojure/clojure "1.2.1"]] | |
; you need poi-3.7-20101029.jar (1.5 MB) from the Apache poi project | |
:main excel.core) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment