]> source.dussan.org Git - poi.git/commitdiff
Commit examples from the JVM Languages page
authorNick Burch <nick@apache.org>
Thu, 15 Feb 2018 21:45:47 +0000 (21:45 +0000)
committerNick Burch <nick@apache.org>
Thu, 15 Feb 2018 21:45:47 +0000 (21:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824371 13f79535-47bb-0310-9956-ffa450edef68

src/examples/clojure/SpreadSheetDemo.clj [new file with mode: 0644]
src/examples/groovy/SpreadSheetDemo.groovy [new file with mode: 0644]
src/examples/groovy/build.gradle [new file with mode: 0644]
src/examples/scala/XSSFMain.scala [new file with mode: 0644]
src/examples/scala/build.sbt [new file with mode: 0644]

diff --git a/src/examples/clojure/SpreadSheetDemo.clj b/src/examples/clojure/SpreadSheetDemo.clj
new file mode 100644 (file)
index 0000000..fcbcf43
--- /dev/null
@@ -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 (file)
index 0000000..379cb9f
--- /dev/null
@@ -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 (file)
index 0000000..8da2711
--- /dev/null
@@ -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 (file)
index 0000000..5b00f1a
--- /dev/null
@@ -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 (file)
index 0000000..33d0e1b
--- /dev/null
@@ -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",
+)