poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1824379 - in /poi/site: publish/poi-jvm-languages.html src/documentation/content/xdocs/poi-jvm-languages.xml
Date Thu, 15 Feb 2018 22:23:24 GMT
Author: nick
Date: Thu Feb 15 22:23:24 2018
New Revision: 1824379

URL: http://svn.apache.org/viewvc?rev=1824379&view=rev
Log:
Update the groovy example

Modified:
    poi/site/publish/poi-jvm-languages.html
    poi/site/src/documentation/content/xdocs/poi-jvm-languages.xml

Modified: poi/site/publish/poi-jvm-languages.html
URL: http://svn.apache.org/viewvc/poi/site/publish/poi-jvm-languages.html?rev=1824379&r1=1824378&r2=1824379&view=diff
==============================================================================
--- poi/site/publish/poi-jvm-languages.html (original)
+++ poi/site/publish/poi-jvm-languages.html Thu Feb 15 22:23:24 2018
@@ -539,20 +539,95 @@ dependencies {
       
 <pre class="code"> 
 import org.apache.poi.ss.usermodel.*
+import org.apache.poi.ss.util.*
 import java.io.File
 
 if (args.length == 0) {
    println "Use:"
-   println "   SpreadSheetDemo [excel-file]"
+   println "   SpreadSheetDemo &lt;excel-file&gt; [output-file]"
    return 1
 }
 
-File f = new File(args[0]);
+File f = new File(args[0])
+DataFormatter formatter = new DataFormatter()
 WorkbookFactory.create(f,null,true).withCloseable { workbook -&gt;
    println "Has ${workbook.getNumberOfSheets()} sheets"
-   0.step workbook.getNumberOfSheets(), 1, { sheetNum -&gt;
-     println "Sheet ${sheetNum} is called ${workbook.getSheetName(sheetNum)}"
+
+   // Dump the contents of the spreadsheet
+   (0..&lt;workbook.getNumberOfSheets()).each { sheetNum -&gt;
+      println "Sheet ${sheetNum} is called ${workbook.getSheetName(sheetNum)}"
+
+      def sheet = workbook.getSheetAt(sheetNum)
+      sheet.each { row -&gt;
+         def nonEmptyCells = row.grep { c -&gt; c.getCellType() != Cell.CELL_TYPE_BLANK
}
+         println " Row ${row.getRowNum()} has ${nonEmptyCells.size()} non-empty cells:"
+         nonEmptyCells.each { c -&gt;
+            def cRef = [c] as CellReference
+            println "  * ${cRef.formatAsString()} = ${formatter.formatCellValue(c)}"
+         }
+      }
+   }
+
+   // Add two new sheets and populate
+   CellStyle headerStyle = makeHeaderStyle(workbook)
+   Sheet ns1 = workbook.createSheet("Generated 1")
+   exportHeader(ns1, headerStyle, null, ["ID","Title","Num"] as String[])
+   ns1.createRow(1).createCell(0).setCellValue("TODO - Populate with data")
+
+   Sheet ns2 = workbook.createSheet("Generated 2")
+   exportHeader(ns2, headerStyle, "This is a demo sheet", 
+                ["ID","Title","Date","Author","Num"] as String[])
+   ns2.createRow(2).createCell(0).setCellValue(1)
+   ns2.createRow(3).createCell(0).setCellValue(4)
+   ns2.createRow(4).createCell(0).setCellValue(1)
+
+   // Save
+   File output = File.createTempFile("output-", (f.getName() =~ /(\.\w+$)/)[0][0])
+   output.withOutputStream { os -&gt; workbook.write(os) }
+   println "Saved as ${output}"
+}
+
+CellStyle makeHeaderStyle(Workbook wb) {
+   int HEADER_HEIGHT = 18
+   CellStyle style = wb.createCellStyle()
+
+   style.setFillForegroundColor(IndexedColors.AQUA.getIndex())
+   style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
+
+   Font font = wb.createFont()
+   font.setFontHeightInPoints((short)HEADER_HEIGHT)
+   font.setBold(true)
+   style.setFont(font)
+
+   return style
+}
+void exportHeader(Sheet s, CellStyle headerStyle, String info, String[] headers) {
+   Row r
+   int rn = 0
+   int HEADER_HEIGHT = 18
+   // Do they want an info row at the top?
+   if (info != null &amp;&amp; !info.isEmpty()) {
+      r = s.createRow(rn)
+      r.setHeightInPoints(HEADER_HEIGHT+1)
+      rn++
+
+      Cell c = r.createCell(0)
+      c.setCellValue(info)
+      c.setCellStyle(headerStyle)
+      s.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1))
+   }
+   // Create the header row, of the right size
+   r = s.createRow(rn)
+   r.setHeightInPoints(HEADER_HEIGHT+1)
+   // Add the column headings
+   headers.eachWithIndex { col, idx -&gt;
+      Cell c = r.createCell(idx)
+      c.setCellValue(col)
+      c.setCellStyle(headerStyle)
+      s.autoSizeColumn(idx)
    }
+   // Make all the columns filterable
+   s.setAutoFilter(new CellRangeAddress(rn, rn, 0, headers.length-1))
 }
       </pre>
     

Modified: poi/site/src/documentation/content/xdocs/poi-jvm-languages.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/poi-jvm-languages.xml?rev=1824379&r1=1824378&r2=1824379&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/poi-jvm-languages.xml (original)
+++ poi/site/src/documentation/content/xdocs/poi-jvm-languages.xml Thu Feb 15 22:23:24 2018
@@ -219,20 +219,95 @@ dependencies {
     <section><title>SpreadSheetDemo.groovy</title>
       <source> <!-- lang="groovy" -->
 import org.apache.poi.ss.usermodel.*
+import org.apache.poi.ss.util.*
 import java.io.File
 
 if (args.length == 0) {
    println "Use:"
-   println "   SpreadSheetDemo [excel-file]"
+   println "   SpreadSheetDemo &lt;excel-file&gt; [output-file]"
    return 1
 }
 
-File f = new File(args[0]);
+File f = new File(args[0])
+DataFormatter formatter = new DataFormatter()
 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)}"
+
+   // Dump the contents of the spreadsheet
+   (0..&lt;workbook.getNumberOfSheets()).each { sheetNum ->
+      println "Sheet ${sheetNum} is called ${workbook.getSheetName(sheetNum)}"
+
+      def sheet = workbook.getSheetAt(sheetNum)
+      sheet.each { row ->
+         def nonEmptyCells = row.grep { c -> c.getCellType() != Cell.CELL_TYPE_BLANK }
+         println " Row ${row.getRowNum()} has ${nonEmptyCells.size()} non-empty cells:"
+         nonEmptyCells.each { c ->
+            def cRef = [c] as CellReference
+            println "  * ${cRef.formatAsString()} = ${formatter.formatCellValue(c)}"
+         }
+      }
+   }
+
+   // Add two new sheets and populate
+   CellStyle headerStyle = makeHeaderStyle(workbook)
+   Sheet ns1 = workbook.createSheet("Generated 1")
+   exportHeader(ns1, headerStyle, null, ["ID","Title","Num"] as String[])
+   ns1.createRow(1).createCell(0).setCellValue("TODO - Populate with data")
+
+   Sheet ns2 = workbook.createSheet("Generated 2")
+   exportHeader(ns2, headerStyle, "This is a demo sheet", 
+                ["ID","Title","Date","Author","Num"] as String[])
+   ns2.createRow(2).createCell(0).setCellValue(1)
+   ns2.createRow(3).createCell(0).setCellValue(4)
+   ns2.createRow(4).createCell(0).setCellValue(1)
+
+   // Save
+   File output = File.createTempFile("output-", (f.getName() =~ /(\.\w+$)/)[0][0])
+   output.withOutputStream { os -> workbook.write(os) }
+   println "Saved as ${output}"
+}
+
+CellStyle makeHeaderStyle(Workbook wb) {
+   int HEADER_HEIGHT = 18
+   CellStyle style = wb.createCellStyle()
+
+   style.setFillForegroundColor(IndexedColors.AQUA.getIndex())
+   style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
+
+   Font font = wb.createFont()
+   font.setFontHeightInPoints((short)HEADER_HEIGHT)
+   font.setBold(true)
+   style.setFont(font)
+
+   return style
+}
+void exportHeader(Sheet s, CellStyle headerStyle, String info, String[] headers) {
+   Row r
+   int rn = 0
+   int HEADER_HEIGHT = 18
+   // Do they want an info row at the top?
+   if (info != null &amp;&amp; !info.isEmpty()) {
+      r = s.createRow(rn)
+      r.setHeightInPoints(HEADER_HEIGHT+1)
+      rn++
+
+      Cell c = r.createCell(0)
+      c.setCellValue(info)
+      c.setCellStyle(headerStyle)
+      s.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1))
+   }
+   // Create the header row, of the right size
+   r = s.createRow(rn)
+   r.setHeightInPoints(HEADER_HEIGHT+1)
+   // Add the column headings
+   headers.eachWithIndex { col, idx ->
+      Cell c = r.createCell(idx)
+      c.setCellValue(col)
+      c.setCellStyle(headerStyle)
+      s.autoSizeColumn(idx)
    }
+   // Make all the columns filterable
+   s.setAutoFilter(new CellRangeAddress(rn, rn, 0, headers.length-1))
 }
       </source>
     </section>



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


Mime
View raw message