--- /dev/null
+(ns poi.core
+ (:gen-class)
+ (:use [clojure.java.io :only [input-stream]])
+ (:import [org.apache.poi.ss.usermodel WorkbookFactory DataFormatter]))
+
+
+(defn sheets [wb] (map #(.getSheetAt wb %1) (range 0 (.getNumberOfSheets wb))))
+
+(defn print-all [wb]
+ (let [df (DataFormatter.)]
+ (doseq [sheet (sheets wb)]
+ (doseq [row (seq sheet)]
+ (doseq [cell (seq row)]
+ (println (.formatAsString (.getAddress cell)) ": " (.formatCellValue df cell)))))))
+
+(defn -main [& args]
+ (when-let [name (first args)]
+ (let [wb (WorkbookFactory/create (input-stream name))]
+ (print-all wb))))
--- /dev/null
+import org.apache.poi.ss.usermodel.*
+import java.io.File
+
+if (args.length == 0) {
+ println "Use:"
+ println " SpreadSheetDemo [excel-file]"
+ return 1
+}
+
+File f = new File(args[0]);
+WorkbookFactory.create(f,null,true).withCloseable { workbook ->
+ println "Has ${workbook.getNumberOfSheets()} sheets"
+ 0.step workbook.getNumberOfSheets(), 1, { sheetNum ->
+ println "Sheet ${sheetNum} is called ${workbook.getSheetName(sheetNum)}"
+ }
+}
+
--- /dev/null
+// Add the POI core and OOXML support dependencies into your gradle build,
+// along with all of Groovy so it can run as a standalone script
+apply plugin: 'groovy'
+repositories {
+ mavenCentral()
+}
+dependencies {
+ compile 'org.codehaus.groovy:groovy-all:2.4.13'
+ compile 'org.apache.poi:poi:3.17'
+ compile 'org.apache.poi:poi-ooxml:3.17'
+}
+
+// Our files are in the current directory
+sourceSets {
+ main { groovy { srcDirs = ['.'] } }
+}
+
+// Run out read demo by default
+tasks.withType(JavaExec) {
+ classpath = sourceSets.main.runtimeClasspath
+}
+task runScript(type: JavaExec) {
+ main = "SpreadSheetDemo"
+ args = ["../../../test-data/spreadsheet/Simple.xls"]
+}
+defaultTasks 'runScript'
--- /dev/null
+// Import the required classes
+import org.apache.poi.ss.usermodel.{WorkbookFactory, DataFormatter}
+import java.io.{File, FileOutputStream}
+
+object XSSFMain extends App {
+
+ // Automatically convert Java collections to Scala equivalents
+ import scala.collection.JavaConversions._
+
+ // Read the contents of the workbook
+ val workbook = WorkbookFactory.create(new File("SampleSS.xlsx"))
+ val formatter = new DataFormatter()
+ for {
+ // Iterate and print the sheets
+ (sheet, i) <- workbook.zipWithIndex
+ _ = println(s"Sheet $i of ${workbook.getNumberOfSheets}: ${sheet.getSheetName}")
+
+ // Iterate and print the rows
+ row <- sheet
+ _ = println(s"\tRow ${row.getRowNum}")
+
+ // Iterate and print the cells
+ cell <- row
+ } {
+ println(s"\t\t${cell.getCellAddress}: ${formatter.formatCellValue(cell)}")
+ }
+
+ // Add a sheet to the workbook
+ val sheet = workbook.createSheet("new sheet")
+ val row = sheet.createRow(7)
+ val cell = row.createCell(42)
+ cell.setAsActiveCell()
+ cell.setCellValue("The answer to life, the universe, and everything")
+
+ // Save the updated workbook as a new file
+ val fos = new FileOutputStream("SampleSS-updated.xlsx")
+ workbook.write(fos)
+ workbook.close()
+}
--- /dev/null
+// Add the POI core and OOXML support dependencies into your build.sbt
+libraryDependencies ++= Seq(
+ "org.apache.poi" % "poi" % "3.17",
+ "org.apache.poi" % "poi-ooxml" % "3.17",
+ "org.apache.poi" % "poi-ooxml-schemas" "3.17",
+)