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.

TimesheetDemo.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.ss;
  16. import java.io.FileOutputStream;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  20. import org.apache.poi.ss.usermodel.BorderStyle;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.usermodel.FillPatternType;
  24. import org.apache.poi.ss.usermodel.Font;
  25. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  26. import org.apache.poi.ss.usermodel.IndexedColors;
  27. import org.apache.poi.ss.usermodel.PrintSetup;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.VerticalAlignment;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.ss.util.CellRangeAddress;
  33. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  34. /**
  35. * A weekly timesheet created using Apache POI.
  36. * Usage:
  37. * TimesheetDemo -xls|xlsx
  38. *
  39. * @author Yegor Kozlov
  40. */
  41. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  42. public final class TimesheetDemo {
  43. private static final String[] titles = {
  44. "Person", "ID", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
  45. "Total\nHrs", "Overtime\nHrs", "Regular\nHrs"
  46. };
  47. private static final Object[][] sample_data = {
  48. {"Yegor Kozlov", "YK", 5.0, 8.0, 10.0, 5.0, 5.0, 7.0, 6.0},
  49. {"Gisella Bronzetti", "GB", 4.0, 3.0, 1.0, 3.5, null, null, 4.0},
  50. };
  51. private TimesheetDemo() {}
  52. public static void main(String[] args) throws Exception {
  53. Workbook wb;
  54. if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
  55. else wb = new XSSFWorkbook();
  56. Map<String, CellStyle> styles = createStyles(wb);
  57. Sheet sheet = wb.createSheet("Timesheet");
  58. PrintSetup printSetup = sheet.getPrintSetup();
  59. printSetup.setLandscape(true);
  60. sheet.setFitToPage(true);
  61. sheet.setHorizontallyCenter(true);
  62. //title row
  63. Row titleRow = sheet.createRow(0);
  64. titleRow.setHeightInPoints(45);
  65. Cell titleCell = titleRow.createCell(0);
  66. titleCell.setCellValue("Weekly Timesheet");
  67. titleCell.setCellStyle(styles.get("title"));
  68. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$L$1"));
  69. //header row
  70. Row headerRow = sheet.createRow(1);
  71. headerRow.setHeightInPoints(40);
  72. Cell headerCell;
  73. for (int i = 0; i < titles.length; i++) {
  74. headerCell = headerRow.createCell(i);
  75. headerCell.setCellValue(titles[i]);
  76. headerCell.setCellStyle(styles.get("header"));
  77. }
  78. int rownum = 2;
  79. for (int i = 0; i < 10; i++) {
  80. Row row = sheet.createRow(rownum++);
  81. for (int j = 0; j < titles.length; j++) {
  82. Cell cell = row.createCell(j);
  83. if(j == 9){
  84. //the 10th cell contains sum over week days, e.g. SUM(C3:I3)
  85. String ref = "C" +rownum+ ":I" + rownum;
  86. cell.setCellFormula("SUM("+ref+")");
  87. cell.setCellStyle(styles.get("formula"));
  88. } else if (j == 11){
  89. cell.setCellFormula("J" +rownum+ "-K" + rownum);
  90. cell.setCellStyle(styles.get("formula"));
  91. } else {
  92. cell.setCellStyle(styles.get("cell"));
  93. }
  94. }
  95. }
  96. //row with totals below
  97. Row sumRow = sheet.createRow(rownum++);
  98. sumRow.setHeightInPoints(35);
  99. Cell cell;
  100. cell = sumRow.createCell(0);
  101. cell.setCellStyle(styles.get("formula"));
  102. cell = sumRow.createCell(1);
  103. cell.setCellValue("Total Hrs:");
  104. cell.setCellStyle(styles.get("formula"));
  105. for (int j = 2; j < 12; j++) {
  106. cell = sumRow.createCell(j);
  107. String ref = (char)('A' + j) + "3:" + (char)('A' + j) + "12";
  108. cell.setCellFormula("SUM(" + ref + ")");
  109. if(j >= 9) cell.setCellStyle(styles.get("formula_2"));
  110. else cell.setCellStyle(styles.get("formula"));
  111. }
  112. rownum++;
  113. sumRow = sheet.createRow(rownum++);
  114. sumRow.setHeightInPoints(25);
  115. cell = sumRow.createCell(0);
  116. cell.setCellValue("Total Regular Hours");
  117. cell.setCellStyle(styles.get("formula"));
  118. cell = sumRow.createCell(1);
  119. cell.setCellFormula("L13");
  120. cell.setCellStyle(styles.get("formula_2"));
  121. sumRow = sheet.createRow(rownum++);
  122. sumRow.setHeightInPoints(25);
  123. cell = sumRow.createCell(0);
  124. cell.setCellValue("Total Overtime Hours");
  125. cell.setCellStyle(styles.get("formula"));
  126. cell = sumRow.createCell(1);
  127. cell.setCellFormula("K13");
  128. cell.setCellStyle(styles.get("formula_2"));
  129. //set sample data
  130. for (int i = 0; i < sample_data.length; i++) {
  131. Row row = sheet.getRow(2 + i);
  132. for (int j = 0; j < sample_data[i].length; j++) {
  133. if(sample_data[i][j] == null) continue;
  134. if(sample_data[i][j] instanceof String) {
  135. row.getCell(j).setCellValue((String)sample_data[i][j]);
  136. } else {
  137. row.getCell(j).setCellValue((Double)sample_data[i][j]);
  138. }
  139. }
  140. }
  141. //finally set column widths, the width is measured in units of 1/256th of a character width
  142. sheet.setColumnWidth(0, 30*256); //30 characters wide
  143. for (int i = 2; i < 9; i++) {
  144. sheet.setColumnWidth(i, 6*256); //6 characters wide
  145. }
  146. sheet.setColumnWidth(10, 10*256); //10 characters wide
  147. // Write the output to a file
  148. String file = "timesheet.xls";
  149. if(wb instanceof XSSFWorkbook) file += "x";
  150. FileOutputStream out = new FileOutputStream(file);
  151. wb.write(out);
  152. out.close();
  153. }
  154. /**
  155. * Create a library of cell styles
  156. */
  157. private static Map<String, CellStyle> createStyles(Workbook wb){
  158. Map<String, CellStyle> styles = new HashMap<>();
  159. CellStyle style;
  160. Font titleFont = wb.createFont();
  161. titleFont.setFontHeightInPoints((short)18);
  162. titleFont.setBold(true);
  163. style = wb.createCellStyle();
  164. style.setAlignment(HorizontalAlignment.CENTER);
  165. style.setVerticalAlignment(VerticalAlignment.CENTER);
  166. style.setFont(titleFont);
  167. styles.put("title", style);
  168. Font monthFont = wb.createFont();
  169. monthFont.setFontHeightInPoints((short)11);
  170. monthFont.setColor(IndexedColors.WHITE.getIndex());
  171. style = wb.createCellStyle();
  172. style.setAlignment(HorizontalAlignment.CENTER);
  173. style.setVerticalAlignment(VerticalAlignment.CENTER);
  174. style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  175. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  176. style.setFont(monthFont);
  177. style.setWrapText(true);
  178. styles.put("header", style);
  179. style = wb.createCellStyle();
  180. style.setAlignment(HorizontalAlignment.CENTER);
  181. style.setWrapText(true);
  182. style.setBorderRight(BorderStyle.THIN);
  183. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  184. style.setBorderLeft(BorderStyle.THIN);
  185. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  186. style.setBorderTop(BorderStyle.THIN);
  187. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  188. style.setBorderBottom(BorderStyle.THIN);
  189. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  190. styles.put("cell", style);
  191. style = wb.createCellStyle();
  192. style.setAlignment(HorizontalAlignment.CENTER);
  193. style.setVerticalAlignment(VerticalAlignment.CENTER);
  194. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  195. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  196. style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
  197. styles.put("formula", style);
  198. style = wb.createCellStyle();
  199. style.setAlignment(HorizontalAlignment.CENTER);
  200. style.setVerticalAlignment(VerticalAlignment.CENTER);
  201. style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
  202. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  203. style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
  204. styles.put("formula_2", style);
  205. return styles;
  206. }
  207. }