From 31eb52b8ab1f35493fc2a5d7173eb2ffde0c2b2c Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 15 Feb 2018 21:45:47 +0000 Subject: [PATCH] Commit examples from the JVM Languages page git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824371 13f79535-47bb-0310-9956-ffa450edef68 --- src/examples/clojure/SpreadSheetDemo.clj | 19 +++++++++++ src/examples/groovy/SpreadSheetDemo.groovy | 17 ++++++++++ src/examples/groovy/build.gradle | 26 +++++++++++++++ src/examples/scala/XSSFMain.scala | 39 ++++++++++++++++++++++ src/examples/scala/build.sbt | 6 ++++ 5 files changed, 107 insertions(+) create mode 100644 src/examples/clojure/SpreadSheetDemo.clj create mode 100644 src/examples/groovy/SpreadSheetDemo.groovy create mode 100644 src/examples/groovy/build.gradle create mode 100644 src/examples/scala/XSSFMain.scala create mode 100644 src/examples/scala/build.sbt diff --git a/src/examples/clojure/SpreadSheetDemo.clj b/src/examples/clojure/SpreadSheetDemo.clj new file mode 100644 index 0000000000..fcbcf43169 --- /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 0000000000..379cb9f7d3 --- /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 0000000000..8da2711991 --- /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 0000000000..5b00f1a8c8 --- /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 0000000000..33d0e1bc03 --- /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", +)