poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1824371 - in /poi/trunk/src/examples: clojure/ clojure/SpreadSheetDemo.clj groovy/ groovy/SpreadSheetDemo.groovy groovy/build.gradle scala/ scala/XSSFMain.scala scala/build.sbt
Date Thu, 15 Feb 2018 21:45:48 GMT
Author: nick
Date: Thu Feb 15 21:45:47 2018
New Revision: 1824371

URL: http://svn.apache.org/viewvc?rev=1824371&view=rev
Log:
Commit examples from the JVM Languages page

Added:
    poi/trunk/src/examples/clojure/
    poi/trunk/src/examples/clojure/SpreadSheetDemo.clj
    poi/trunk/src/examples/groovy/   (with props)
    poi/trunk/src/examples/groovy/SpreadSheetDemo.groovy
    poi/trunk/src/examples/groovy/build.gradle
    poi/trunk/src/examples/scala/
    poi/trunk/src/examples/scala/XSSFMain.scala
    poi/trunk/src/examples/scala/build.sbt

Added: poi/trunk/src/examples/clojure/SpreadSheetDemo.clj
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/clojure/SpreadSheetDemo.clj?rev=1824371&view=auto
==============================================================================
--- poi/trunk/src/examples/clojure/SpreadSheetDemo.clj (added)
+++ poi/trunk/src/examples/clojure/SpreadSheetDemo.clj Thu Feb 15 21:45:47 2018
@@ -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))))

Propchange: poi/trunk/src/examples/groovy/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 15 21:45:47 2018
@@ -0,0 +1,2 @@
+.gradle
+build

Added: poi/trunk/src/examples/groovy/SpreadSheetDemo.groovy
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/groovy/SpreadSheetDemo.groovy?rev=1824371&view=auto
==============================================================================
--- poi/trunk/src/examples/groovy/SpreadSheetDemo.groovy (added)
+++ poi/trunk/src/examples/groovy/SpreadSheetDemo.groovy Thu Feb 15 21:45:47 2018
@@ -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)}"
+   }
+}
+

Added: poi/trunk/src/examples/groovy/build.gradle
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/groovy/build.gradle?rev=1824371&view=auto
==============================================================================
--- poi/trunk/src/examples/groovy/build.gradle (added)
+++ poi/trunk/src/examples/groovy/build.gradle Thu Feb 15 21:45:47 2018
@@ -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'

Added: poi/trunk/src/examples/scala/XSSFMain.scala
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/scala/XSSFMain.scala?rev=1824371&view=auto
==============================================================================
--- poi/trunk/src/examples/scala/XSSFMain.scala (added)
+++ poi/trunk/src/examples/scala/XSSFMain.scala Thu Feb 15 21:45:47 2018
@@ -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()
+}

Added: poi/trunk/src/examples/scala/build.sbt
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/scala/build.sbt?rev=1824371&view=auto
==============================================================================
--- poi/trunk/src/examples/scala/build.sbt (added)
+++ poi/trunk/src/examples/scala/build.sbt Thu Feb 15 21:45:47 2018
@@ -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",
+)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message