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.

BigExample.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.FileOutputStream;
  17. import java.io.IOException;
  18. import org.apache.poi.hssf.usermodel.HSSFCell;
  19. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  20. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  21. import org.apache.poi.hssf.usermodel.HSSFFont;
  22. import org.apache.poi.hssf.usermodel.HSSFRow;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  26. import org.apache.poi.ss.usermodel.BorderStyle;
  27. import org.apache.poi.ss.usermodel.FillPatternType;
  28. /**
  29. * Demonstrates many features of the user API at once. Used in the HOW-TO guide.
  30. */
  31. public class BigExample {
  32. public static void main(String[] args) throws IOException {
  33. int rownum;
  34. // create a new workbook
  35. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  36. // create a new sheet
  37. HSSFSheet s = wb.createSheet();
  38. // declare a row object reference
  39. HSSFRow r;
  40. // declare a cell object reference
  41. HSSFCell c;
  42. // create 3 cell styles
  43. HSSFCellStyle cs = wb.createCellStyle();
  44. HSSFCellStyle cs2 = wb.createCellStyle();
  45. HSSFCellStyle cs3 = wb.createCellStyle();
  46. // create 2 fonts objects
  47. HSSFFont f = wb.createFont();
  48. HSSFFont f2 = wb.createFont();
  49. //set font 1 to 12 point type
  50. f.setFontHeightInPoints((short) 12);
  51. //make it red
  52. f.setColor(HSSFColorPredefined.RED.getIndex());
  53. // make it bold
  54. //arial is the default font
  55. f.setBold(true);
  56. //set font 2 to 10 point type
  57. f2.setFontHeightInPoints((short) 10);
  58. //make it the color at palette index 0xf (white)
  59. f2.setColor(HSSFColorPredefined.WHITE.getIndex());
  60. //make it bold
  61. f2.setBold(true);
  62. //set cell style
  63. cs.setFont(f);
  64. //set the cell format see HSSFDataFormat for a full list
  65. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
  66. //set a thin border
  67. cs2.setBorderBottom(BorderStyle.THIN);
  68. //fill w fg fill color
  69. cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  70. // set foreground fill to red
  71. cs2.setFillForegroundColor(HSSFColorPredefined.RED.getIndex());
  72. // set the font
  73. cs2.setFont(f2);
  74. // set the sheet name to HSSF Test
  75. wb.setSheetName(0, "HSSF Test");
  76. // create a sheet with 300 rows (0-299)
  77. for (rownum = 0; rownum < 300; rownum++) {
  78. // create a row
  79. r = s.createRow(rownum);
  80. // on every other row
  81. if ((rownum % 2) == 0) {
  82. // make the row height bigger (in twips - 1/20 of a point)
  83. r.setHeight((short) 0x249);
  84. }
  85. // create 50 cells (0-49) (the += 2 becomes apparent later
  86. for (int cellnum = 0; cellnum < 50; cellnum += 2) {
  87. // create a numeric cell
  88. c = r.createCell(cellnum);
  89. // do some goofy math to demonstrate decimals
  90. c.setCellValue((rownum * 10000.0) + cellnum
  91. + (rownum / 1000.0)
  92. + (cellnum / 10000.0));
  93. // on every other row
  94. if ((rownum % 2) == 0) {
  95. // set this cell to the first cell style we defined
  96. c.setCellStyle(cs);
  97. }
  98. // create a string cell (see why += 2 in the
  99. c = r.createCell(cellnum + 1);
  100. // set the cell's string value to "TEST"
  101. c.setCellValue("TEST");
  102. // make this column a bit wider
  103. s.setColumnWidth(cellnum + 1, (int) ((50 * 8) / ((double) 1 / 20)));
  104. // on every other row
  105. if ((rownum % 2) == 0) {
  106. // set this to the white on red cell style
  107. // we defined above
  108. c.setCellStyle(cs2);
  109. }
  110. }
  111. }
  112. //draw a thick black border on the row at the bottom using BLANKS
  113. // advance 2 rows
  114. rownum++;
  115. rownum++;
  116. r = s.createRow(rownum);
  117. // define the third style to be the default
  118. // except with a thick black border at the bottom
  119. cs3.setBorderBottom(BorderStyle.THICK);
  120. //create 50 cells
  121. for (int cellnum = 0; cellnum < 50; cellnum++) {
  122. //create a blank type cell (no value)
  123. c = r.createCell(cellnum);
  124. // set it to the thick black border style
  125. c.setCellStyle(cs3);
  126. }
  127. //end draw thick black border
  128. // demonstrate adding/naming and deleting a sheet
  129. // create a sheet, set its title then delete it
  130. wb.createSheet();
  131. wb.setSheetName(1, "DeletedSheet");
  132. wb.removeSheetAt(1);
  133. //end deleted sheet
  134. // create a new file
  135. try (FileOutputStream out = new FileOutputStream("workbook.xls")) {
  136. // write the workbook to the output stream
  137. // close our file (don't blow out our file handles
  138. wb.write(out);
  139. }
  140. }
  141. }
  142. }