diff --git a/src/examples/clojure/SpreadSheetDemo.clj b/src/examples/clojure/SpreadSheetDemo.clj new file mode 100644 index 000000000..fcbcf4316 --- /dev/null +++ b/src/examples/clojure/SpreadSheetDemo.clj @@ -0,0 +1,19 @@ +(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)))) diff --git a/src/examples/groovy/SpreadSheetDemo.groovy b/src/examples/groovy/SpreadSheetDemo.groovy new file mode 100644 index 000000000..379cb9f7d --- /dev/null +++ b/src/examples/groovy/SpreadSheetDemo.groovy @@ -0,0 +1,17 @@ +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)}" + } +} + diff --git a/src/examples/groovy/build.gradle b/src/examples/groovy/build.gradle new file mode 100644 index 000000000..8da271199 --- /dev/null +++ b/src/examples/groovy/build.gradle @@ -0,0 +1,26 @@ +// 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' diff --git a/src/examples/scala/XSSFMain.scala b/src/examples/scala/XSSFMain.scala new file mode 100644 index 000000000..5b00f1a8c --- /dev/null +++ b/src/examples/scala/XSSFMain.scala @@ -0,0 +1,39 @@ +// 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() +} diff --git a/src/examples/scala/build.sbt b/src/examples/scala/build.sbt new file mode 100644 index 000000000..33d0e1bc0 --- /dev/null +++ b/src/examples/scala/build.sbt @@ -0,0 +1,6 @@ +// 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", +)