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.

HSSFReadWrite.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. package org.apache.poi.examples.hssf.usermodel;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import org.apache.poi.hssf.usermodel.HSSFCell;
  20. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  21. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  22. import org.apache.poi.hssf.usermodel.HSSFFont;
  23. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  24. import org.apache.poi.hssf.usermodel.HSSFRow;
  25. import org.apache.poi.hssf.usermodel.HSSFSheet;
  26. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27. import org.apache.poi.ss.usermodel.BorderStyle;
  28. import org.apache.poi.ss.usermodel.FillPatternType;
  29. import org.apache.poi.ss.util.CellRangeAddress;
  30. /**
  31. * File for HSSF testing/examples
  32. *
  33. * THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
  34. * It does contain sample API usage that may be educational to regular API
  35. * users.
  36. */
  37. @SuppressWarnings({"java:S106","java:S4823"})
  38. public final class HSSFReadWrite {
  39. private HSSFReadWrite() {}
  40. /**
  41. * creates an {@link HSSFWorkbook} with the specified OS filename.
  42. */
  43. private static HSSFWorkbook readFile(String filename) throws IOException {
  44. try (FileInputStream fis = new FileInputStream(filename)) {
  45. return new HSSFWorkbook(fis); // NOSONAR - should not be closed here
  46. }
  47. }
  48. /**
  49. * given a filename this outputs a sample sheet with just a set of
  50. * rows/cells.
  51. */
  52. private static void testCreateSampleSheet(String outputFilename) throws IOException {
  53. try (HSSFWorkbook wb = new HSSFWorkbook();
  54. FileOutputStream out = new FileOutputStream(outputFilename)) {
  55. HSSFSheet s = wb.createSheet();
  56. HSSFCellStyle cs = wb.createCellStyle();
  57. HSSFCellStyle cs2 = wb.createCellStyle();
  58. HSSFCellStyle cs3 = wb.createCellStyle();
  59. HSSFFont f = wb.createFont();
  60. HSSFFont f2 = wb.createFont();
  61. f.setFontHeightInPoints((short) 12);
  62. f.setColor((short) 0xA);
  63. f.setBold(true);
  64. f2.setFontHeightInPoints((short) 10);
  65. f2.setColor((short) 0xf);
  66. f2.setBold(true);
  67. cs.setFont(f);
  68. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
  69. cs2.setBorderBottom(BorderStyle.THIN);
  70. cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  71. cs2.setFillForegroundColor((short) 0xA);
  72. cs2.setFont(f2);
  73. wb.setSheetName(0, "HSSF Test");
  74. int rownum;
  75. for (rownum = 0; rownum < 300; rownum++) {
  76. HSSFRow r = s.createRow(rownum);
  77. if ((rownum % 2) == 0) {
  78. r.setHeight((short) 0x249);
  79. }
  80. for (int cellnum = 0; cellnum < 50; cellnum += 2) {
  81. HSSFCell c = r.createCell(cellnum);
  82. c.setCellValue((rownum * 10000.0) + cellnum
  83. + (rownum / 1000.0) + (cellnum / 10000.0));
  84. if ((rownum % 2) == 0) {
  85. c.setCellStyle(cs);
  86. }
  87. c = r.createCell(cellnum + 1);
  88. c.setCellValue(new HSSFRichTextString("TEST"));
  89. // 50 characters divided by 1/20th of a point
  90. s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
  91. if ((rownum % 2) == 0) {
  92. c.setCellStyle(cs2);
  93. }
  94. }
  95. }
  96. // draw a thick black border on the row at the bottom using BLANKS
  97. rownum++;
  98. rownum++;
  99. HSSFRow r = s.createRow(rownum);
  100. cs3.setBorderBottom(BorderStyle.THICK);
  101. for (int cellnum = 0; cellnum < 50; cellnum++) {
  102. HSSFCell c = r.createCell(cellnum);
  103. c.setCellStyle(cs3);
  104. }
  105. s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
  106. s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
  107. // end draw thick black border
  108. // create a sheet, set its title then delete it
  109. wb.createSheet();
  110. wb.setSheetName(1, "DeletedSheet");
  111. wb.removeSheetAt(1);
  112. // end deleted sheet
  113. wb.write(out);
  114. }
  115. }
  116. /**
  117. * Method main
  118. *
  119. * Given 1 argument takes that as the filename, inputs it and dumps the
  120. * cell values/types out to sys.out.<br>
  121. *
  122. * given 2 arguments where the second argument is the word "write" and the
  123. * first is the filename - writes out a sample (test) spreadsheet
  124. * see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br>
  125. *
  126. * given 2 arguments where the first is an input filename and the second
  127. * an output filename (not write), attempts to fully read in the
  128. * spreadsheet and fully write it out.<br>
  129. *
  130. * given 3 arguments where the first is an input filename and the second an
  131. * output filename (not write) and the third is "modify1", attempts to read in the
  132. * spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to
  133. * "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you
  134. * take the output from the write test, you'll have a valid scenario.
  135. */
  136. public static void main(String[] args) throws Exception {
  137. if (args.length < 1) {
  138. System.err.println("At least one argument expected");
  139. return;
  140. }
  141. String fileName = args[0];
  142. if (args.length < 2) {
  143. try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
  144. System.out.println("Data dump:\n");
  145. for (int k = 0; k < wb.getNumberOfSheets(); k++) {
  146. HSSFSheet sheet = wb.getSheetAt(k);
  147. int rows = sheet.getPhysicalNumberOfRows();
  148. System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
  149. for (int r = 0; r < rows; r++) {
  150. HSSFRow row = sheet.getRow(r);
  151. if (row == null) {
  152. continue;
  153. }
  154. System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells() + " cell(s).");
  155. for (int c = 0; c < row.getLastCellNum(); c++) {
  156. HSSFCell cell = row.getCell(c);
  157. String value;
  158. if (cell != null) {
  159. switch (cell.getCellType()) {
  160. case FORMULA:
  161. value = "FORMULA value=" + cell.getCellFormula();
  162. break;
  163. case NUMERIC:
  164. value = "NUMERIC value=" + cell.getNumericCellValue();
  165. break;
  166. case STRING:
  167. value = "STRING value=" + cell.getStringCellValue();
  168. break;
  169. case BLANK:
  170. value = "<BLANK>";
  171. break;
  172. case BOOLEAN:
  173. value = "BOOLEAN value-" + cell.getBooleanCellValue();
  174. break;
  175. case ERROR:
  176. value = "ERROR value=" + cell.getErrorCellValue();
  177. break;
  178. default:
  179. value = "UNKNOWN value of type " + cell.getCellType();
  180. }
  181. System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. } else if (args.length == 2) {
  188. if ("write".equalsIgnoreCase(args[1])) {
  189. System.out.println("Write mode");
  190. long time = System.currentTimeMillis();
  191. HSSFReadWrite.testCreateSampleSheet(fileName);
  192. System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time");
  193. } else {
  194. System.out.println("readwrite test");
  195. try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
  196. FileOutputStream stream = new FileOutputStream(args[1])) {
  197. wb.write(stream);
  198. }
  199. }
  200. } else if (args.length == 3 && "modify1".equalsIgnoreCase(args[2])) {
  201. // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
  202. try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
  203. FileOutputStream stream = new FileOutputStream(args[1])) {
  204. HSSFSheet sheet = wb.getSheetAt(0);
  205. for (int k = 0; k < 25; k++) {
  206. HSSFRow row = sheet.getRow(k);
  207. sheet.removeRow(row);
  208. }
  209. for (int k = 74; k < 100; k++) {
  210. HSSFRow row = sheet.getRow(k);
  211. sheet.removeRow(row);
  212. }
  213. HSSFRow row = sheet.getRow(39);
  214. HSSFCell cell = row.getCell(3);
  215. cell.setCellValue("MODIFIED CELL!!!!!");
  216. wb.write(stream);
  217. }
  218. }
  219. }
  220. }