--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.usermodel.examples;\r
+\r
+import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.usermodel.*;\r
+\r
+import java.util.Map;\r
+import java.util.HashMap;\r
+import java.util.Date;\r
+import java.util.Calendar;\r
+import java.io.FileOutputStream;\r
+import java.text.SimpleDateFormat;\r
+\r
+/**\r
+ * A business plan demo\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class BusinessPlan {\r
+\r
+ private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM");\r
+\r
+ private static final String[] days = {\r
+ "ID", "Project Name", "Owner", "Days", "Start", "End"};\r
+\r
+ //sample data to fill the sheet.\r
+ private static final String[][] data = {\r
+ {"1.0", "Marketing Research Tactical Plan", "R. Ihrig", "70", "9-Jul", null,\r
+ "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"},\r
+ null,\r
+ {"1.1", "Scope Definition Phase", "R. Ihrig", "10", "9-Jul", null,\r
+ "x", "x", null, null, null, null, null, null, null, null, null},\r
+ {"1.1.1", "Define research objectives", "R. Ihrig", "3", "9-Jul", null,\r
+ "x", null, null, null, null, null, null, null, null, null, null},\r
+ {"1.1.2", "Define research requirements", "S. Abbas", "7", "10-Jul", null,\r
+ "x", "x", null, null, null, null, null, null, null, null, null},\r
+ {"1.1.3", "Determine in-house resource or hire vendor", "R. Ihrig", "2", "15-Jul", null,\r
+ "x", "x", null, null, null, null, null, null, null, null, null},\r
+ null,\r
+ {"1.2", "Vendor Selection Phase", "R. Ihrig", "19", "19-Jul", null,\r
+ null, "x", "x", "x", "x", null, null, null, null, null, null},\r
+ {"1.2.1", "Define vendor selection criteria", "R. Ihrig", "3", "19-Jul", null,\r
+ null, "x", null, null, null, null, null, null, null, null, null},\r
+ {"1.2.2", "Develop vendor selection questionnaire", "S. Abbas, T. Wang", "2", "22-Jul", null,\r
+ null, "x", "x", null, null, null, null, null, null, null, null},\r
+ {"1.2.3", "Develop Statement of Work", "S. Abbas", "4", "26-Jul", null,\r
+ null, null, "x", "x", null, null, null, null, null, null, null},\r
+ {"1.2.4", "Evaluate proposal", "R. Ihrig, S. Abbas", "4", "2-Aug", null,\r
+ null, null, null, "x", "x", null, null, null, null, null, null},\r
+ {"1.2.5", "Select vendor", "R. Ihrig", "1", "6-Aug", null,\r
+ null, null, null, null, "x", null, null, null, null, null, null},\r
+ null,\r
+ {"1.3", "Research Phase", "Y. Li", "47", "9-Aug", null,\r
+ null, null, null, null, "x", "x", "x", "x", "x", "x", "x"},\r
+ {"1.3.1", "Develop market research information needs questionnaire", "Y. Li", "2", "9-Aug", null,\r
+ null, null, null, null, "x", null, null, null, null, null, null},\r
+ {"1.3.2", "Interview marketing group for market research needs", "Y. Li", "2", "11-Aug", null,\r
+ null, null, null, null, "x", "x", null, null, null, null, null},\r
+ {"1.3.3", "Document information needs", "Y. Li, S. Abbas", "1", "13-Aug", null,\r
+ null, null, null, null, null, "x", null, null, null, null, null},\r
+ };\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ Calendar calendar = Calendar.getInstance();\r
+ int year = calendar.get(Calendar.YEAR);\r
+\r
+ XSSFWorkbook wb = new XSSFWorkbook();\r
+ Map<String, XSSFCellStyle> styles = createStyles(wb);\r
+\r
+ XSSFSheet sheet = wb.createSheet("Plan");\r
+\r
+ //turn off gridlines\r
+ sheet.setDisplayGridlines(false);\r
+ sheet.setPrintGridlines(false);\r
+ XSSFPrintSetup printSetup = sheet.getPrintSetup();\r
+ printSetup.setOrientation(PrintOrientation.LANDSCAPE);\r
+ sheet.setFitToPage(true);\r
+ sheet.setHorizontallyCenter(true);\r
+\r
+ //the header row: centered text in 48pt font\r
+ XSSFRow headerRow = sheet.createRow(0);\r
+ headerRow.setHeightInPoints(12.75f);\r
+ for (int i = 0; i < days.length; i++) {\r
+ XSSFCell cell = headerRow.createCell(i);\r
+ cell.setCellValue(days[i]);\r
+ cell.setCellStyle(styles.get("header"));\r
+ }\r
+ //columns for 11 weeks starting from 9-Jul\r
+ calendar.setTime(fmt.parse("9-Jul"));\r
+ calendar.set(Calendar.YEAR, year);\r
+ for (int i = 0; i < 11; i++) {\r
+ XSSFCell cell = headerRow.createCell(days.length + i);\r
+ cell.setCellValue(calendar);\r
+ cell.setCellStyle(styles.get("header_date"));\r
+ calendar.roll(Calendar.WEEK_OF_YEAR, true);\r
+ }\r
+\r
+ sheet.createFreezePane(0, 1);\r
+ XSSFRow row;\r
+ XSSFCell cell;\r
+\r
+ int rownum = 1;\r
+ for (int i = 0; i < data.length; i++, rownum++) {\r
+ row = sheet.createRow(rownum);\r
+ if(data[i] == null) continue;\r
+\r
+ for (int j = 0; j < data[i].length; j++) {\r
+ cell = row.createCell(j);\r
+ String styleName;\r
+ boolean isHeader = i == 0 || data[i-1] == null;\r
+ switch(j){\r
+ case 0:\r
+ if(isHeader) {\r
+ styleName = "cell_b";\r
+ cell.setCellValue(Double.parseDouble(data[i][j]));\r
+ } else {\r
+ styleName = "cell_normal";\r
+ cell.setCellValue(data[i][j]);\r
+ }\r
+ break;\r
+ case 1:\r
+ if(isHeader) {\r
+ styleName = i == 0 ? "cell_h" : "cell_bb";\r
+ } else {\r
+ styleName = "cell_indented";\r
+ }\r
+ cell.setCellValue(data[i][j]);\r
+ break;\r
+ case 2:\r
+ styleName = isHeader ? "cell_b" : "cell_normal";\r
+ cell.setCellValue(data[i][j]);\r
+ break;\r
+ case 3:\r
+ styleName = isHeader ? "cell_b_centered" : "cell_normal_centered";\r
+ cell.setCellValue(Integer.parseInt(data[i][j]));\r
+ break;\r
+ case 4: {\r
+ calendar.setTime(fmt.parse(data[i][j]));\r
+ calendar.set(Calendar.YEAR, year);\r
+ cell.setCellValue(calendar);\r
+ styleName = isHeader ? "cell_b_date" : "cell_normal_date";\r
+ break;\r
+ }\r
+ case 5: {\r
+ int r = rownum + 1;\r
+ String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")";\r
+ cell.setCellFormula(fmla);\r
+ styleName = isHeader ? "cell_bg" : "cell_g";\r
+ break;\r
+ }\r
+ default:\r
+ styleName = data[i][j] != null ? "cell_blue" : "cell_normal";\r
+ }\r
+\r
+ cell.setCellStyle(styles.get(styleName));\r
+ }\r
+ }\r
+\r
+\r
+ sheet.groupRow(5, 7);\r
+ sheet.groupRow(10, 14);\r
+ sheet.groupRow(17, 19);\r
+\r
+ sheet.setColumnWidth(0, 256*6);\r
+ sheet.setColumnWidth(1, 256*33);\r
+ sheet.setColumnWidth(2, 256*20);\r
+ sheet.setZoom(75);\r
+\r
+\r
+ // Write the output to a file\r
+ FileOutputStream out = new FileOutputStream("xssf-plan.xlsx");\r
+ wb.write(out);\r
+ out.close();\r
+ }\r
+\r
+ /**\r
+ * cell styles used for formatting calendar sheets\r
+ */\r
+ private static Map<String, XSSFCellStyle> createStyles(XSSFWorkbook wb){\r
+ Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();\r
+ XSSFDataFormat df = wb.createDataFormat();\r
+\r
+ XSSFCellStyle style;\r
+ XSSFFont headerFont = wb.createFont();\r
+ headerFont.setBold(true);\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.CENTER);\r
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(204, 204, 255)));\r
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
+ style.setFont(headerFont);\r
+ styles.put("header", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.CENTER);\r
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(204, 204, 255)));\r
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
+ style.setFont(headerFont);\r
+ style.setDataFormat(df.getFormat("d-mmm"));\r
+ styles.put("header_date", style);\r
+\r
+ XSSFFont font1 = wb.createFont();\r
+ font1.setBold(true);\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.LEFT);\r
+ style.setFont(font1);\r
+ styles.put("cell_b", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.CENTER);\r
+ style.setFont(font1);\r
+ styles.put("cell_b_centered", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.RIGHT);\r
+ style.setFont(font1);\r
+ style.setDataFormat(df.getFormat("d-mmm"));\r
+ styles.put("cell_b_date", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.RIGHT);\r
+ style.setFont(font1);\r
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 228, 228)));\r
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
+ style.setDataFormat(df.getFormat("d-mmm"));\r
+ styles.put("cell_g", style);\r
+\r
+ XSSFFont font2 = wb.createFont();\r
+ font2.setColor(IndexedColors.BLUE.getIndex());\r
+ font2.setBold(true);\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.LEFT);\r
+ style.setFont(font2);\r
+ styles.put("cell_bb", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.RIGHT);\r
+ style.setFont(font1);\r
+ style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 228, 228)));\r
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
+ style.setDataFormat(df.getFormat("d-mmm"));\r
+ styles.put("cell_bg", style);\r
+\r
+ XSSFFont font3 = wb.createFont();\r
+ font3.setFontHeightInPoints((short)14);\r
+ font3.setColor(IndexedColors.DARK_BLUE.getIndex());\r
+ font3.setBold(true);\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.LEFT);\r
+ style.setFont(font3);\r
+ style.setWrapText(true);\r
+ styles.put("cell_h", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.LEFT);\r
+ style.setWrapText(true);\r
+ styles.put("cell_normal", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.CENTER);\r
+ style.setWrapText(true);\r
+ styles.put("cell_normal_centered", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.RIGHT);\r
+ style.setWrapText(true);\r
+ style.setDataFormat(df.getFormat("d-mmm"));\r
+ styles.put("cell_normal_date", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setAlignment(HorizontalAlignment.LEFT);\r
+ style.setIndention((short)1);\r
+ style.setWrapText(true);\r
+ styles.put("cell_indented", style);\r
+\r
+ style = createBorderedStyle(wb);\r
+ style.setFillForegroundColor(IndexedColors.BLUE.getIndex());\r
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);\r
+ styles.put("cell_blue", style);\r
+\r
+ return styles;\r
+ }\r
+\r
+ private static XSSFCellStyle createBorderedStyle(XSSFWorkbook wb){\r
+ XSSFCellStyle style = wb.createCellStyle();\r
+ style.setBorderRight(BorderStyle.THIN);\r
+ style.setRightBorderColor(IndexedColors.BLACK.getIndex());\r
+ style.setBorderBottom(BorderStyle.THIN);\r
+ style.setBottomBorderColor(IndexedColors.BLACK.getIndex());\r
+ style.setBorderLeft(BorderStyle.THIN);\r
+ style.setLeftBorderColor(IndexedColors.BLACK.getIndex());\r
+ style.setBorderTop(BorderStyle.THIN);\r
+ style.setTopBorderColor(IndexedColors.BLACK.getIndex());\r
+ return style;\r
+ }\r
+}\r