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.

BusinessPlan.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.text.SimpleDateFormat;
  18. import java.util.Calendar;
  19. import java.util.HashMap;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.ss.usermodel.BorderStyle;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.CellStyle;
  26. import org.apache.poi.ss.usermodel.DataFormat;
  27. import org.apache.poi.ss.usermodel.FillPatternType;
  28. import org.apache.poi.ss.usermodel.Font;
  29. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  30. import org.apache.poi.ss.usermodel.IndexedColors;
  31. import org.apache.poi.ss.usermodel.PrintSetup;
  32. import org.apache.poi.ss.usermodel.Row;
  33. import org.apache.poi.ss.usermodel.Sheet;
  34. import org.apache.poi.ss.usermodel.Workbook;
  35. import org.apache.poi.util.LocaleUtil;
  36. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  37. /**
  38. * A business plan demo
  39. * Usage:
  40. * BusinessPlan -xls|xlsx
  41. *
  42. * @author Yegor Kozlov
  43. */
  44. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  45. public final class BusinessPlan {
  46. private static final String[] titles = {
  47. "ID", "Project Name", "Owner", "Days", "Start", "End"};
  48. //sample data to fill the sheet.
  49. private static final String[][] data = {
  50. {"1.0", "Marketing Research Tactical Plan", "J. Dow", "70", "9-Jul", null,
  51. "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"},
  52. null,
  53. {"1.1", "Scope Definition Phase", "J. Dow", "10", "9-Jul", null,
  54. "x", "x", null, null, null, null, null, null, null, null, null},
  55. {"1.1.1", "Define research objectives", "J. Dow", "3", "9-Jul", null,
  56. "x", null, null, null, null, null, null, null, null, null, null},
  57. {"1.1.2", "Define research requirements", "S. Jones", "7", "10-Jul", null,
  58. "x", "x", null, null, null, null, null, null, null, null, null},
  59. {"1.1.3", "Determine in-house resource or hire vendor", "J. Dow", "2", "15-Jul", null,
  60. "x", "x", null, null, null, null, null, null, null, null, null},
  61. null,
  62. {"1.2", "Vendor Selection Phase", "J. Dow", "19", "19-Jul", null,
  63. null, "x", "x", "x", "x", null, null, null, null, null, null},
  64. {"1.2.1", "Define vendor selection criteria", "J. Dow", "3", "19-Jul", null,
  65. null, "x", null, null, null, null, null, null, null, null, null},
  66. {"1.2.2", "Develop vendor selection questionnaire", "S. Jones, T. Wates", "2", "22-Jul", null,
  67. null, "x", "x", null, null, null, null, null, null, null, null},
  68. {"1.2.3", "Develop Statement of Work", "S. Jones", "4", "26-Jul", null,
  69. null, null, "x", "x", null, null, null, null, null, null, null},
  70. {"1.2.4", "Evaluate proposal", "J. Dow, S. Jones", "4", "2-Aug", null,
  71. null, null, null, "x", "x", null, null, null, null, null, null},
  72. {"1.2.5", "Select vendor", "J. Dow", "1", "6-Aug", null,
  73. null, null, null, null, "x", null, null, null, null, null, null},
  74. null,
  75. {"1.3", "Research Phase", "G. Lee", "47", "9-Aug", null,
  76. null, null, null, null, "x", "x", "x", "x", "x", "x", "x"},
  77. {"1.3.1", "Develop market research information needs questionnaire", "G. Lee", "2", "9-Aug", null,
  78. null, null, null, null, "x", null, null, null, null, null, null},
  79. {"1.3.2", "Interview marketing group for market research needs", "G. Lee", "2", "11-Aug", null,
  80. null, null, null, null, "x", "x", null, null, null, null, null},
  81. {"1.3.3", "Document information needs", "G. Lee, S. Jones", "1", "13-Aug", null,
  82. null, null, null, null, null, "x", null, null, null, null, null},
  83. };
  84. private BusinessPlan() {}
  85. public static void main(String[] args) throws Exception {
  86. Workbook wb;
  87. if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
  88. else wb = new XSSFWorkbook();
  89. final SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM", Locale.ROOT);
  90. Map<String, CellStyle> styles = createStyles(wb);
  91. Sheet sheet = wb.createSheet("Business Plan");
  92. //turn off gridlines
  93. sheet.setDisplayGridlines(false);
  94. sheet.setPrintGridlines(false);
  95. sheet.setFitToPage(true);
  96. sheet.setHorizontallyCenter(true);
  97. PrintSetup printSetup = sheet.getPrintSetup();
  98. printSetup.setLandscape(true);
  99. //the following three statements are required only for HSSF
  100. sheet.setAutobreaks(true);
  101. printSetup.setFitHeight((short)1);
  102. printSetup.setFitWidth((short)1);
  103. //the header row: centered text in 48pt font
  104. Row headerRow = sheet.createRow(0);
  105. headerRow.setHeightInPoints(12.75f);
  106. for (int i = 0; i < titles.length; i++) {
  107. Cell cell = headerRow.createCell(i);
  108. cell.setCellValue(titles[i]);
  109. cell.setCellStyle(styles.get("header"));
  110. }
  111. //columns for 11 weeks starting from 9-Jul
  112. Calendar calendar = LocaleUtil.getLocaleCalendar();
  113. int year = calendar.get(Calendar.YEAR);
  114. calendar.setTime(fmt.parse("9-Jul"));
  115. calendar.set(Calendar.YEAR, year);
  116. for (int i = 0; i < 11; i++) {
  117. Cell cell = headerRow.createCell(titles.length + i);
  118. cell.setCellValue(calendar);
  119. cell.setCellStyle(styles.get("header_date"));
  120. calendar.roll(Calendar.WEEK_OF_YEAR, true);
  121. }
  122. //freeze the first row
  123. sheet.createFreezePane(0, 1);
  124. Row row;
  125. Cell cell;
  126. int rownum = 1;
  127. for (int i = 0; i < data.length; i++, rownum++) {
  128. row = sheet.createRow(rownum);
  129. if(data[i] == null) continue;
  130. for (int j = 0; j < data[i].length; j++) {
  131. cell = row.createCell(j);
  132. String styleName;
  133. boolean isHeader = i == 0 || data[i-1] == null;
  134. switch(j){
  135. case 0:
  136. if(isHeader) {
  137. styleName = "cell_b";
  138. cell.setCellValue(Double.parseDouble(data[i][j]));
  139. } else {
  140. styleName = "cell_normal";
  141. cell.setCellValue(data[i][j]);
  142. }
  143. break;
  144. case 1:
  145. if(isHeader) {
  146. styleName = i == 0 ? "cell_h" : "cell_bb";
  147. } else {
  148. styleName = "cell_indented";
  149. }
  150. cell.setCellValue(data[i][j]);
  151. break;
  152. case 2:
  153. styleName = isHeader ? "cell_b" : "cell_normal";
  154. cell.setCellValue(data[i][j]);
  155. break;
  156. case 3:
  157. styleName = isHeader ? "cell_b_centered" : "cell_normal_centered";
  158. cell.setCellValue(Integer.parseInt(data[i][j]));
  159. break;
  160. case 4: {
  161. calendar.setTime(fmt.parse(data[i][j]));
  162. calendar.set(Calendar.YEAR, year);
  163. cell.setCellValue(calendar);
  164. styleName = isHeader ? "cell_b_date" : "cell_normal_date";
  165. break;
  166. }
  167. case 5: {
  168. int r = rownum + 1;
  169. String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")";
  170. cell.setCellFormula(fmla);
  171. styleName = isHeader ? "cell_bg" : "cell_g";
  172. break;
  173. }
  174. default:
  175. styleName = data[i][j] != null ? "cell_blue" : "cell_normal";
  176. }
  177. cell.setCellStyle(styles.get(styleName));
  178. }
  179. }
  180. //group rows for each phase, row numbers are 0-based
  181. sheet.groupRow(4, 6);
  182. sheet.groupRow(9, 13);
  183. sheet.groupRow(16, 18);
  184. //set column widths, the width is measured in units of 1/256th of a character width
  185. sheet.setColumnWidth(0, 256*6);
  186. sheet.setColumnWidth(1, 256*33);
  187. sheet.setColumnWidth(2, 256*20);
  188. sheet.setZoom(75); //75% scale
  189. // Write the output to a file
  190. String file = "businessplan.xls";
  191. if(wb instanceof XSSFWorkbook) file += "x";
  192. FileOutputStream out = new FileOutputStream(file);
  193. wb.write(out);
  194. out.close();
  195. wb.close();
  196. }
  197. /**
  198. * create a library of cell styles
  199. */
  200. private static Map<String, CellStyle> createStyles(Workbook wb){
  201. Map<String, CellStyle> styles = new HashMap<>();
  202. DataFormat df = wb.createDataFormat();
  203. CellStyle style;
  204. Font headerFont = wb.createFont();
  205. headerFont.setBold(true);
  206. style = createBorderedStyle(wb);
  207. style.setAlignment(HorizontalAlignment.CENTER);
  208. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  209. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  210. style.setFont(headerFont);
  211. styles.put("header", style);
  212. style = createBorderedStyle(wb);
  213. style.setAlignment(HorizontalAlignment.CENTER);
  214. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  215. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  216. style.setFont(headerFont);
  217. style.setDataFormat(df.getFormat("d-mmm"));
  218. styles.put("header_date", style);
  219. Font font1 = wb.createFont();
  220. font1.setBold(true);
  221. style = createBorderedStyle(wb);
  222. style.setAlignment(HorizontalAlignment.LEFT);
  223. style.setFont(font1);
  224. styles.put("cell_b", style);
  225. style = createBorderedStyle(wb);
  226. style.setAlignment(HorizontalAlignment.CENTER);
  227. style.setFont(font1);
  228. styles.put("cell_b_centered", style);
  229. style = createBorderedStyle(wb);
  230. style.setAlignment(HorizontalAlignment.RIGHT);
  231. style.setFont(font1);
  232. style.setDataFormat(df.getFormat("d-mmm"));
  233. styles.put("cell_b_date", style);
  234. style = createBorderedStyle(wb);
  235. style.setAlignment(HorizontalAlignment.RIGHT);
  236. style.setFont(font1);
  237. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  238. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  239. style.setDataFormat(df.getFormat("d-mmm"));
  240. styles.put("cell_g", style);
  241. Font font2 = wb.createFont();
  242. font2.setColor(IndexedColors.BLUE.getIndex());
  243. font2.setBold(true);
  244. style = createBorderedStyle(wb);
  245. style.setAlignment(HorizontalAlignment.LEFT);
  246. style.setFont(font2);
  247. styles.put("cell_bb", style);
  248. style = createBorderedStyle(wb);
  249. style.setAlignment(HorizontalAlignment.RIGHT);
  250. style.setFont(font1);
  251. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  252. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  253. style.setDataFormat(df.getFormat("d-mmm"));
  254. styles.put("cell_bg", style);
  255. Font font3 = wb.createFont();
  256. font3.setFontHeightInPoints((short)14);
  257. font3.setColor(IndexedColors.DARK_BLUE.getIndex());
  258. font3.setBold(true);
  259. style = createBorderedStyle(wb);
  260. style.setAlignment(HorizontalAlignment.LEFT);
  261. style.setFont(font3);
  262. style.setWrapText(true);
  263. styles.put("cell_h", style);
  264. style = createBorderedStyle(wb);
  265. style.setAlignment(HorizontalAlignment.LEFT);
  266. style.setWrapText(true);
  267. styles.put("cell_normal", style);
  268. style = createBorderedStyle(wb);
  269. style.setAlignment(HorizontalAlignment.CENTER);
  270. style.setWrapText(true);
  271. styles.put("cell_normal_centered", style);
  272. style = createBorderedStyle(wb);
  273. style.setAlignment(HorizontalAlignment.RIGHT);
  274. style.setWrapText(true);
  275. style.setDataFormat(df.getFormat("d-mmm"));
  276. styles.put("cell_normal_date", style);
  277. style = createBorderedStyle(wb);
  278. style.setAlignment(HorizontalAlignment.LEFT);
  279. style.setIndention((short)1);
  280. style.setWrapText(true);
  281. styles.put("cell_indented", style);
  282. style = createBorderedStyle(wb);
  283. style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
  284. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  285. styles.put("cell_blue", style);
  286. return styles;
  287. }
  288. private static CellStyle createBorderedStyle(Workbook wb){
  289. BorderStyle thin = BorderStyle.THIN;
  290. short black = IndexedColors.BLACK.getIndex();
  291. CellStyle style = wb.createCellStyle();
  292. style.setBorderRight(thin);
  293. style.setRightBorderColor(black);
  294. style.setBorderBottom(thin);
  295. style.setBottomBorderColor(black);
  296. style.setBorderLeft(thin);
  297. style.setLeftBorderColor(black);
  298. style.setBorderTop(thin);
  299. style.setTopBorderColor(black);
  300. return style;
  301. }
  302. }