You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpreadSheetDemo.groovy 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. import org.apache.poi.ss.usermodel.*
  16. import org.apache.poi.ss.util.*
  17. import java.io.File
  18. if (args.length == 0) {
  19. println "Use:"
  20. println " SpreadSheetDemo <excel-file> [output-file]"
  21. return 1
  22. }
  23. File f = new File(args[0])
  24. DataFormatter formatter = new DataFormatter()
  25. WorkbookFactory.create(f,null,true).withCloseable { workbook ->
  26. println "Has ${workbook.getNumberOfSheets()} sheets"
  27. // Dump the contents of the spreadsheet
  28. (0..<workbook.getNumberOfSheets()).each { sheetNum ->
  29. println "Sheet ${sheetNum} is called ${workbook.getSheetName(sheetNum)}"
  30. def sheet = workbook.getSheetAt(sheetNum)
  31. sheet.each { row ->
  32. def nonEmptyCells = row.grep { c -> c.getCellType() != Cell.CELL_TYPE_BLANK }
  33. println " Row ${row.getRowNum()} has ${nonEmptyCells.size()} non-empty cells:"
  34. nonEmptyCells.each { c ->
  35. def cRef = [c] as CellReference
  36. println " * ${cRef.formatAsString()} = ${formatter.formatCellValue(c)}"
  37. }
  38. }
  39. }
  40. // Add two new sheets and populate
  41. CellStyle headerStyle = makeHeaderStyle(workbook)
  42. Sheet ns1 = workbook.createSheet("Generated 1")
  43. exportHeader(ns1, headerStyle, null, ["ID","Title","Num"] as String[])
  44. ns1.createRow(1).createCell(0).setCellValue("TODO - Populate with data")
  45. Sheet ns2 = workbook.createSheet("Generated 2")
  46. exportHeader(ns2, headerStyle, "This is a demo sheet",
  47. ["ID","Title","Date","Author","Num"] as String[])
  48. ns2.createRow(2).createCell(0).setCellValue(1)
  49. ns2.createRow(3).createCell(0).setCellValue(4)
  50. ns2.createRow(4).createCell(0).setCellValue(1)
  51. // Save
  52. File output = File.createTempFile("output-", (f.getName() =~ /(\.\w+$)/)[0][0])
  53. output.withOutputStream { os -> workbook.write(os) }
  54. println "Saved as ${output}"
  55. }
  56. CellStyle makeHeaderStyle(Workbook wb) {
  57. int HEADER_HEIGHT = 18
  58. CellStyle style = wb.createCellStyle()
  59. style.setFillForegroundColor(IndexedColors.AQUA.getIndex())
  60. style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
  61. Font font = wb.createFont()
  62. font.setFontHeightInPoints((short)HEADER_HEIGHT)
  63. font.setBold(true)
  64. style.setFont(font)
  65. return style
  66. }
  67. void exportHeader(Sheet s, CellStyle headerStyle, String info, String[] headers) {
  68. Row r
  69. int rn = 0
  70. int HEADER_HEIGHT = 18
  71. // Do they want an info row at the top?
  72. if (info != null && !info.isEmpty()) {
  73. r = s.createRow(rn)
  74. r.setHeightInPoints(HEADER_HEIGHT+1)
  75. rn++
  76. Cell c = r.createCell(0)
  77. c.setCellValue(info)
  78. c.setCellStyle(headerStyle)
  79. s.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1))
  80. }
  81. // Create the header row, of the right size
  82. r = s.createRow(rn)
  83. r.setHeightInPoints(HEADER_HEIGHT+1)
  84. // Add the column headings
  85. headers.eachWithIndex { col, idx ->
  86. Cell c = r.createCell(idx)
  87. c.setCellValue(col)
  88. c.setCellStyle(headerStyle)
  89. s.autoSizeColumn(idx)
  90. }
  91. // Make all the columns filterable
  92. s.setAutoFilter(new CellRangeAddress(rn, rn, 0, headers.length-1))
  93. }