From: Yegor Kozlov Date: Fri, 14 Nov 2008 11:56:41 +0000 (+0000) Subject: 1. fixed XSSFSheet.groupRow and ungroupRow to operate on 0-based arguments, was 1... X-Git-Tag: REL_3_5_BETA4~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c67c1d5ca0545d59ca3b8515bd78e7f02328c118;p=poi.git 1. fixed XSSFSheet.groupRow and ungroupRow to operate on 0-based arguments, was 1-based2. repackaged common xssh-hssf examples, created a page in the site for them 3. converted broken non-ascii characters to unicode in TestMetaDataIPI and TestWriteWellKnown git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713981 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 469e516a5a..8257ad5ef1 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,11 +37,11 @@ - 46174 - Fixed HSSFName to handle general formulas (not just area references) + 46174 - Fixed HSSFName to handle general formulas (not just area references) 46189 - added chart records: CHARTFRTINFO, STARTBLOCK, ENDBLOCK, STARTOBJECT, ENDOBJECT, and CATLAB - 46199 - More tweaks to EmbeddedObjectRefSubRecord + 46199 - More tweaks to EmbeddedObjectRefSubRecord Changes to formula evaluation allowing for reduced memory usage - 45290 - Support odd files where the POIFS header block comes after the data blocks, and is on the data blocks list + 45290 - Support odd files where the POIFS header block comes after the data blocks, and is on the data blocks list 46184 - More odd escaped date formats Include the sheet number in the output of XLS2CSVmra 46043 - correctly write out HPSF properties with HWPF diff --git a/src/documentation/content/xdocs/spreadsheet/book.xml b/src/documentation/content/xdocs/spreadsheet/book.xml index 7e8a59d7d8..55d1470b00 100644 --- a/src/documentation/content/xdocs/spreadsheet/book.xml +++ b/src/documentation/content/xdocs/spreadsheet/book.xml @@ -34,7 +34,8 @@ - + + diff --git a/src/documentation/content/xdocs/spreadsheet/examples.xml b/src/documentation/content/xdocs/spreadsheet/examples.xml new file mode 100755 index 0000000000..f683738a3a --- /dev/null +++ b/src/documentation/content/xdocs/spreadsheet/examples.xml @@ -0,0 +1,75 @@ + + + + + +
+ HSSF and XSSF Examples + + + +
+ +
HSSF and XSSF examples +

POI comes with a number of examples that demonstrate how you can use POI API to create documents from "real life". + The examples are based on common XSSF-HSSF interfaces so that you can generate either *.xls or *.xlsx output just by setting a command-line argument: +

+ + BusinessPlan -xls + or + BusinessPlan -xlsx + +

All sample source is available in SVN

+
+
BusinessPlan +

The BusinessPlan + application creates a sample business plan with three phases, weekly iterations and time highlighting. Demonstrates advanced cell formatting + (number and date formats, alignmnets, fills, borders) and various settings for organizing data in a sheet (freezed panes, groupped rows). +

+

+ business plan demo +

+
+
Calendar +

The Calendar + demo creates a multi sheet calendar. Each month is on a separate sheet. +

+

+ calendar demo +

+
+
LoanCalculator +

The LoanCalculator + demo creates a simple loan calculator. Demonstrates advance usage of cell formulas and named ranges. +

+

+ loan calculator demo +

+
+
TimesheetDemo +

The TimesheetDemo + demo creates a weekly timesheet with automatic calculation of total hours. Demonstrates advance usage of cell formulas. +

+

+ timesheet demo +

+
+ +
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5ec9d4401a..d3f1ed911b 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,11 +34,11 @@ - 46174 - Fixed HSSFName to handle general formulas (not just area references) + 46174 - Fixed HSSFName to handle general formulas (not just area references) 46189 - added chart records: CHARTFRTINFO, STARTBLOCK, ENDBLOCK, STARTOBJECT, ENDOBJECT, and CATLAB - 46199 - More tweaks to EmbeddedObjectRefSubRecord + 46199 - More tweaks to EmbeddedObjectRefSubRecord Changes to formula evaluation allowing for reduced memory usage - 45290 - Support odd files where the POIFS header block comes after the data blocks, and is on the data blocks list + 45290 - Support odd files where the POIFS header block comes after the data blocks, and is on the data blocks list 46184 - More odd escaped date formats Include the sheet number in the output of XLS2CSVmra 46043 - correctly write out HPSF properties with HWPF diff --git a/src/documentation/resources/images/businessplan.jpg b/src/documentation/resources/images/businessplan.jpg new file mode 100755 index 0000000000..9bfaf4c7b2 Binary files /dev/null and b/src/documentation/resources/images/businessplan.jpg differ diff --git a/src/documentation/resources/images/calendar.jpg b/src/documentation/resources/images/calendar.jpg new file mode 100755 index 0000000000..4f11878290 Binary files /dev/null and b/src/documentation/resources/images/calendar.jpg differ diff --git a/src/documentation/resources/images/loancalc.jpg b/src/documentation/resources/images/loancalc.jpg new file mode 100755 index 0000000000..fdc62e82dd Binary files /dev/null and b/src/documentation/resources/images/loancalc.jpg differ diff --git a/src/documentation/resources/images/timesheet.jpg b/src/documentation/resources/images/timesheet.jpg new file mode 100755 index 0000000000..1ff512b1f9 Binary files /dev/null and b/src/documentation/resources/images/timesheet.jpg differ diff --git a/src/examples/src/org/apache/poi/hslf/usermodel/examples/SoundFinder.java b/src/examples/src/org/apache/poi/hslf/usermodel/examples/SoundFinder.java deleted file mode 100644 index 800a4952c8..0000000000 --- a/src/examples/src/org/apache/poi/hslf/usermodel/examples/SoundFinder.java +++ /dev/null @@ -1,80 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ -package org.apache.poi.hslf.usermodel.examples; -import org.apache.poi.ddf.*; -import org.apache.poi.hslf.model.*; -import org.apache.poi.hslf.record.InteractiveInfo; -import org.apache.poi.hslf.record.InteractiveInfoAtom; -import org.apache.poi.hslf.record.Record; -import org.apache.poi.hslf.usermodel.*; -import java.io.FileInputStream; -import java.util.Iterator; -import java.util.List; - -/** - * For each slide iterate over shapes and found associated sound data. - * - * @author Yegor Kozlov - */ -public class SoundFinder { - public static void main(String[] args) throws Exception { - SlideShow ppt = new SlideShow(new FileInputStream(args[0])); - SoundData[] sounds = ppt.getSoundData(); - - Slide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - Shape[] shape = slide[i].getShapes(); - for (int j = 0; j < shape.length; j++) { - int soundRef = getSoundReference(shape[j]); - if(soundRef != -1) { - System.out.println("Slide["+i+"], shape["+j+"], soundRef: "+soundRef); - System.out.println(" " + sounds[soundRef].getSoundName()); - System.out.println(" " + sounds[soundRef].getSoundType()); - } - } - } - } - - /** - * Check if a given shape is associated with a sound. - * @return 0-based reference to a sound in the sound collection - * or -1 if the shape is not associated with a sound - */ - protected static int getSoundReference(Shape shape){ - int soundRef = -1; - //dive into the shape container and search for InteractiveInfoAtom - EscherContainerRecord spContainer = shape.getSpContainer(); - List spchild = spContainer.getChildRecords(); - for (Iterator it = spchild.iterator(); it.hasNext();) { - EscherRecord obj = (EscherRecord) it.next(); - if (obj.getRecordId() == EscherClientDataRecord.RECORD_ID) { - byte[] data = obj.serialize(); - Record[] records = Record.findChildRecords(data, 8, -data.length - 8); - for (int j = 0; j < records.length; j++) { - if (records[j] instanceof InteractiveInfo) { - InteractiveInfoAtom info = ((InteractiveInfo)records[j]).getInteractiveInfoAtom(); - if (info.getAction() == InteractiveInfoAtom.ACTION_MEDIA) { - soundRef = info.getSoundRef(); - } - } - } - } - } - return soundRef; - } -} diff --git a/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java b/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java new file mode 100755 index 0000000000..8d3fcf8224 --- /dev/null +++ b/src/examples/src/org/apache/poi/ss/examples/BusinessPlan.java @@ -0,0 +1,324 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.ss.examples; + +import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import java.util.Map; +import java.util.HashMap; +import java.util.Calendar; +import java.io.FileOutputStream; +import java.text.SimpleDateFormat; + +/** + * A business plan demo + * Usage: + * BusinessPlan -xls|xlsx + * + * @author Yegor Kozlov + */ +public class BusinessPlan { + + private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM"); + + private static final String[] titles = { + "ID", "Project Name", "Owner", "Days", "Start", "End"}; + + //sample data to fill the sheet. + private static final String[][] data = { + {"1.0", "Marketing Research Tactical Plan", "J. Dow", "70", "9-Jul", null, + "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"}, + null, + {"1.1", "Scope Definition Phase", "J. Dow", "10", "9-Jul", null, + "x", "x", null, null, null, null, null, null, null, null, null}, + {"1.1.1", "Define research objectives", "J. Dow", "3", "9-Jul", null, + "x", null, null, null, null, null, null, null, null, null, null}, + {"1.1.2", "Define research requirements", "S. Jones", "7", "10-Jul", null, + "x", "x", null, null, null, null, null, null, null, null, null}, + {"1.1.3", "Determine in-house resource or hire vendor", "J. Dow", "2", "15-Jul", null, + "x", "x", null, null, null, null, null, null, null, null, null}, + null, + {"1.2", "Vendor Selection Phase", "J. Dow", "19", "19-Jul", null, + null, "x", "x", "x", "x", null, null, null, null, null, null}, + {"1.2.1", "Define vendor selection criteria", "J. Dow", "3", "19-Jul", null, + null, "x", null, null, null, null, null, null, null, null, null}, + {"1.2.2", "Develop vendor selection questionnaire", "S. Jones, T. Wates", "2", "22-Jul", null, + null, "x", "x", null, null, null, null, null, null, null, null}, + {"1.2.3", "Develop Statement of Work", "S. Jones", "4", "26-Jul", null, + null, null, "x", "x", null, null, null, null, null, null, null}, + {"1.2.4", "Evaluate proposal", "J. Dow, S. Jones", "4", "2-Aug", null, + null, null, null, "x", "x", null, null, null, null, null, null}, + {"1.2.5", "Select vendor", "J. Dow", "1", "6-Aug", null, + null, null, null, null, "x", null, null, null, null, null, null}, + null, + {"1.3", "Research Phase", "G. Lee", "47", "9-Aug", null, + null, null, null, null, "x", "x", "x", "x", "x", "x", "x"}, + {"1.3.1", "Develop market research information needs questionnaire", "G. Lee", "2", "9-Aug", null, + null, null, null, null, "x", null, null, null, null, null, null}, + {"1.3.2", "Interview marketing group for market research needs", "G. Lee", "2", "11-Aug", null, + null, null, null, null, "x", "x", null, null, null, null, null}, + {"1.3.3", "Document information needs", "G. Lee, S. Jones", "1", "13-Aug", null, + null, null, null, null, null, "x", null, null, null, null, null}, + }; + + public static void main(String[] args) throws Exception { + Workbook wb; + + if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook(); + else wb = new XSSFWorkbook(); + + Map styles = createStyles(wb); + + Sheet sheet = wb.createSheet("Business Plan"); + + //turn off gridlines + sheet.setDisplayGridlines(false); + sheet.setPrintGridlines(false); + sheet.setFitToPage(true); + sheet.setHorizontallyCenter(true); + PrintSetup printSetup = sheet.getPrintSetup(); + printSetup.setLandscape(true); + + //the following three statements are required only for HSSF + sheet.setAutobreaks(true); + printSetup.setFitHeight((short)1); + printSetup.setFitWidth((short)1); + + //the header row: centered text in 48pt font + Row headerRow = sheet.createRow(0); + headerRow.setHeightInPoints(12.75f); + for (int i = 0; i < titles.length; i++) { + Cell cell = headerRow.createCell(i); + cell.setCellValue(titles[i]); + cell.setCellStyle(styles.get("header")); + } + //columns for 11 weeks starting from 9-Jul + Calendar calendar = Calendar.getInstance(); + int year = calendar.get(Calendar.YEAR); + + calendar.setTime(fmt.parse("9-Jul")); + calendar.set(Calendar.YEAR, year); + for (int i = 0; i < 11; i++) { + Cell cell = headerRow.createCell(titles.length + i); + cell.setCellValue(calendar); + cell.setCellStyle(styles.get("header_date")); + calendar.roll(Calendar.WEEK_OF_YEAR, true); + } + //freeze the first row + sheet.createFreezePane(0, 1); + + Row row; + Cell cell; + int rownum = 1; + for (int i = 0; i < data.length; i++, rownum++) { + row = sheet.createRow(rownum); + if(data[i] == null) continue; + + for (int j = 0; j < data[i].length; j++) { + cell = row.createCell(j); + String styleName; + boolean isHeader = i == 0 || data[i-1] == null; + switch(j){ + case 0: + if(isHeader) { + styleName = "cell_b"; + cell.setCellValue(Double.parseDouble(data[i][j])); + } else { + styleName = "cell_normal"; + cell.setCellValue(data[i][j]); + } + break; + case 1: + if(isHeader) { + styleName = i == 0 ? "cell_h" : "cell_bb"; + } else { + styleName = "cell_indented"; + } + cell.setCellValue(data[i][j]); + break; + case 2: + styleName = isHeader ? "cell_b" : "cell_normal"; + cell.setCellValue(data[i][j]); + break; + case 3: + styleName = isHeader ? "cell_b_centered" : "cell_normal_centered"; + cell.setCellValue(Integer.parseInt(data[i][j])); + break; + case 4: { + calendar.setTime(fmt.parse(data[i][j])); + calendar.set(Calendar.YEAR, year); + cell.setCellValue(calendar); + styleName = isHeader ? "cell_b_date" : "cell_normal_date"; + break; + } + case 5: { + int r = rownum + 1; + String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")"; + cell.setCellFormula(fmla); + styleName = isHeader ? "cell_bg" : "cell_g"; + break; + } + default: + styleName = data[i][j] != null ? "cell_blue" : "cell_normal"; + } + + cell.setCellStyle(styles.get(styleName)); + } + } + + //group rows for each phase, row numbers are 0-based + sheet.groupRow(4, 6); + sheet.groupRow(9, 13); + sheet.groupRow(16, 18); + + //set column widths, the width is measured in units of 1/256th of a character width + sheet.setColumnWidth(0, 256*6); + sheet.setColumnWidth(1, 256*33); + sheet.setColumnWidth(2, 256*20); + sheet.setZoom(3, 4); + + + // Write the output to a file + String file = "businessplan.xls"; + if(wb instanceof XSSFWorkbook) file += "x"; + FileOutputStream out = new FileOutputStream(file); + wb.write(out); + out.close(); + } + + /** + * create a library of cell styles + */ + private static Map createStyles(Workbook wb){ + Map styles = new HashMap(); + DataFormat df = wb.createDataFormat(); + + CellStyle style; + Font headerFont = wb.createFont(); + headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFont(headerFont); + styles.put("header", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFont(headerFont); + style.setDataFormat(df.getFormat("d-mmm")); + styles.put("header_date", style); + + Font font1 = wb.createFont(); + font1.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setFont(font1); + styles.put("cell_b", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setFont(font1); + styles.put("cell_b_centered", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(font1); + style.setDataFormat(df.getFormat("d-mmm")); + styles.put("cell_b_date", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(font1); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setDataFormat(df.getFormat("d-mmm")); + styles.put("cell_g", style); + + Font font2 = wb.createFont(); + font2.setColor(IndexedColors.BLUE.getIndex()); + font2.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setFont(font2); + styles.put("cell_bb", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(font1); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setDataFormat(df.getFormat("d-mmm")); + styles.put("cell_bg", style); + + Font font3 = wb.createFont(); + font3.setFontHeightInPoints((short)14); + font3.setColor(IndexedColors.DARK_BLUE.getIndex()); + font3.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setFont(font3); + style.setWrapText(true); + styles.put("cell_h", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setWrapText(true); + styles.put("cell_normal", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setWrapText(true); + styles.put("cell_normal_centered", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setWrapText(true); + style.setDataFormat(df.getFormat("d-mmm")); + styles.put("cell_normal_date", style); + + style = createBorderedStyle(wb); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setIndention((short)1); + style.setWrapText(true); + styles.put("cell_indented", style); + + style = createBorderedStyle(wb); + style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + styles.put("cell_blue", style); + + return styles; + } + + private static CellStyle createBorderedStyle(Workbook wb){ + CellStyle style = wb.createCellStyle(); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderTop(CellStyle.BORDER_THIN); + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); + return style; + } +} diff --git a/src/examples/src/org/apache/poi/ss/examples/CalendarDemo.java b/src/examples/src/org/apache/poi/ss/examples/CalendarDemo.java new file mode 100755 index 0000000000..5cdf794841 --- /dev/null +++ b/src/examples/src/org/apache/poi/ss/examples/CalendarDemo.java @@ -0,0 +1,242 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.ss.examples; + +import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import java.io.FileOutputStream; +import java.util.Calendar; +import java.util.Map; +import java.util.HashMap; + +/** + * A monthly calendar created using Apache POI. Each month is on a separate sheet. + *
+ * Usage:
+ * CalendarDemo -xls|xlsx 
+ * 
+ * + * @author Yegor Kozlov + */ +public class CalendarDemo { + + private static final String[] days = { + "Sunday", "Monday", "Tuesday", + "Wednesday", "Thursday", "Friday", "Saturday"}; + + private static final String[] months = { + "January", "February", "March","April", "May", "June","July", "August", + "September","October", "November", "December"}; + + public static void main(String[] args) throws Exception { + + Calendar calendar = Calendar.getInstance(); + boolean xlsx = true; + for (int i = 0; i < args.length; i++) { + if(args[i].charAt(0) == '-'){ + xlsx = args[i].equals("-xlsx"); + } else { + calendar.set(Calendar.YEAR, Integer.parseInt(args[i])); + } + } + int year = calendar.get(Calendar.YEAR); + + Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook(); + + Map styles = createStyles(wb); + + for (int month = 0; month < 12; month++) { + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, 1); + //create a sheet for each month + Sheet sheet = wb.createSheet(months[month]); + + //turn off gridlines + sheet.setDisplayGridlines(false); + sheet.setPrintGridlines(false); + sheet.setFitToPage(true); + sheet.setHorizontallyCenter(true); + PrintSetup printSetup = sheet.getPrintSetup(); + printSetup.setLandscape(true); + + //the following three statements are required only for HSSF + sheet.setAutobreaks(true); + printSetup.setFitHeight((short)1); + printSetup.setFitWidth((short)1); + + //the header row: centered text in 48pt font + Row headerRow = sheet.createRow(0); + headerRow.setHeightInPoints(80); + Cell titleCell = headerRow.createCell(0); + titleCell.setCellValue(months[month] + " " + year); + titleCell.setCellStyle(styles.get("title")); + sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1")); + + //header with month titles + Row monthRow = sheet.createRow(1); + for (int i = 0; i < days.length; i++) { + //set column widths, the width is measured in units of 1/256th of a character width + sheet.setColumnWidth(i*2, 5*256); //the column is 5 characters wide + sheet.setColumnWidth(i*2 + 1, 13*256); //the column is 13 characters wide + sheet.addMergedRegion(new CellRangeAddress(1, 1, i*2, i*2+1)); + Cell monthCell = monthRow.createCell(i*2); + monthCell.setCellValue(days[i]); + monthCell.setCellStyle(styles.get("month")); + } + + int cnt = 1, day=1; + int rownum = 2; + for (int j = 0; j < 6; j++) { + Row row = sheet.createRow(rownum++); + row.setHeightInPoints(100); + for (int i = 0; i < days.length; i++) { + Cell dayCell_1 = row.createCell(i*2); + Cell dayCell_2 = row.createCell(i*2 + 1); + + int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); + if(cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) { + dayCell_1.setCellValue(day); + calendar.set(Calendar.DAY_OF_MONTH, ++day); + + if(i == 0 || i == days.length-1) { + dayCell_1.setCellStyle(styles.get("weekend_left")); + dayCell_2.setCellStyle(styles.get("weekend_right")); + } else { + dayCell_1.setCellStyle(styles.get("workday_left")); + dayCell_2.setCellStyle(styles.get("workday_right")); + } + } else { + dayCell_1.setCellStyle(styles.get("grey_left")); + dayCell_2.setCellStyle(styles.get("grey_right")); + } + cnt++; + } + if(calendar.get(Calendar.MONTH) > month) break; + } + } + + // Write the output to a file + String file = "calendar.xls"; + if(wb instanceof XSSFWorkbook) file += "x"; + FileOutputStream out = new FileOutputStream(file); + wb.write(out); + out.close(); + } + + /** + * cell styles used for formatting calendar sheets + */ + private static Map createStyles(Workbook wb){ + Map styles = new HashMap(); + + short borderColor = IndexedColors.GREY_50_PERCENT.getIndex(); + + CellStyle style; + Font titleFont = wb.createFont(); + titleFont.setFontHeightInPoints((short)48); + titleFont.setColor(IndexedColors.DARK_BLUE.getIndex()); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFont(titleFont); + styles.put("title", style); + + Font monthFont = wb.createFont(); + monthFont.setFontHeightInPoints((short)12); + monthFont.setColor(IndexedColors.WHITE.getIndex()); + monthFont.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFont(monthFont); + styles.put("month", style); + + Font dayFont = wb.createFont(); + dayFont.setFontHeightInPoints((short)14); + dayFont.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setVerticalAlignment(CellStyle.VERTICAL_TOP); + style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setLeftBorderColor(borderColor); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + style.setFont(dayFont); + styles.put("weekend_left", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_TOP); + style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(borderColor); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + styles.put("weekend_right", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setVerticalAlignment(CellStyle.VERTICAL_TOP); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setLeftBorderColor(borderColor); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + style.setFont(dayFont); + styles.put("workday_left", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_TOP); + style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(borderColor); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + styles.put("workday_right", style); + + style = wb.createCellStyle(); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + styles.put("grey_left", style); + + style = wb.createCellStyle(); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(borderColor); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(borderColor); + styles.put("grey_right", style); + + return styles; + } +} diff --git a/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java b/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java new file mode 100755 index 0000000000..96bb5f1652 --- /dev/null +++ b/src/examples/src/org/apache/poi/ss/examples/LoanCalculator.java @@ -0,0 +1,304 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.ss.examples; + +import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import java.util.Map; +import java.util.HashMap; +import java.io.FileOutputStream; + +/** + * Simple Loan Calculator. Demonstrates advance usage of cell formulas and named ranges. + * + * Usage: + * LoanCalculator -xls|xlsx + * + * @author Yegor Kozlov + */ +public class LoanCalculator { + + public static void main(String[] args) throws Exception { + Workbook wb; + + if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook(); + else wb = new XSSFWorkbook(); + + Map styles = createStyles(wb); + Sheet sheet = wb.createSheet("Loan Calculator"); + sheet.setPrintGridlines(false); + sheet.setDisplayGridlines(false); + + PrintSetup printSetup = sheet.getPrintSetup(); + printSetup.setLandscape(true); + sheet.setFitToPage(true); + sheet.setHorizontallyCenter(true); + + sheet.setColumnWidth(0, 3*256); + sheet.setColumnWidth(1, 3*256); + sheet.setColumnWidth(2, 11*256); + sheet.setColumnWidth(3, 14*256); + sheet.setColumnWidth(4, 14*256); + sheet.setColumnWidth(5, 14*256); + sheet.setColumnWidth(6, 14*256); + + createNames(wb); + + Row titleRow = sheet.createRow(0); + titleRow.setHeightInPoints(35); + for (int i = 1; i <= 7; i++) { + titleRow.createCell(i).setCellStyle(styles.get("title")); + } + Cell titleCell = titleRow.getCell(2); + titleCell.setCellValue("Simple Loan Calculator"); + sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$H$1")); + + Row row = sheet.createRow(2); + Cell cell = row.createCell(4); + cell.setCellValue("Enter values"); + cell.setCellStyle(styles.get("item_right")); + + row = sheet.createRow(3); + cell = row.createCell(2); + cell.setCellValue("Loan amount"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellStyle(styles.get("input_$")); + cell.setAsActiveCell(); + + row = sheet.createRow(4); + cell = row.createCell(2); + cell.setCellValue("Annual interest rate"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellStyle(styles.get("input_%")); + + row = sheet.createRow(5); + cell = row.createCell(2); + cell.setCellValue("Loan period in years"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellStyle(styles.get("input_i")); + + row = sheet.createRow(6); + cell = row.createCell(2); + cell.setCellValue("Start date of loan"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellStyle(styles.get("input_d")); + + row = sheet.createRow(8); + cell = row.createCell(2); + cell.setCellValue("Monthly payment"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellFormula("IF(Values_Entered,Monthly_Payment,\"\")"); + cell.setCellStyle(styles.get("formula_$")); + + row = sheet.createRow(9); + cell = row.createCell(2); + cell.setCellValue("Number of payments"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellFormula("IF(Values_Entered,Loan_Years*12,\"\")"); + cell.setCellStyle(styles.get("formula_i")); + + row = sheet.createRow(10); + cell = row.createCell(2); + cell.setCellValue("Total interest"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellFormula("IF(Values_Entered,Total_Cost-Loan_Amount,\"\")"); + cell.setCellStyle(styles.get("formula_$")); + + row = sheet.createRow(11); + cell = row.createCell(2); + cell.setCellValue("Total cost of loan"); + cell.setCellStyle(styles.get("item_left")); + cell = row.createCell(4); + cell.setCellFormula("IF(Values_Entered,Monthly_Payment*Number_of_Payments,\"\")"); + cell.setCellStyle(styles.get("formula_$")); + + + // Write the output to a file + String file = "loan-calculator.xls"; + if(wb instanceof XSSFWorkbook) file += "x"; + FileOutputStream out = new FileOutputStream(file); + wb.write(out); + out.close(); + } + + /** + * cell styles used for formatting calendar sheets + */ + private static Map createStyles(Workbook wb){ + Map styles = new HashMap(); + + CellStyle style; + Font titleFont = wb.createFont(); + titleFont.setFontHeightInPoints((short)14); + titleFont.setFontName("Trebuchet MS"); + style = wb.createCellStyle(); + style.setFont(titleFont); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + styles.put("title", style); + + Font itemFont = wb.createFont(); + itemFont.setFontHeightInPoints((short)9); + itemFont.setFontName("Trebuchet MS"); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_LEFT); + style.setFont(itemFont); + styles.put("item_left", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + styles.put("item_right", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + style.setBorderRight(CellStyle.BORDER_DOTTED); + style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderLeft(CellStyle.BORDER_DOTTED); + style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderTop(CellStyle.BORDER_DOTTED); + style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setDataFormat(wb.createDataFormat().getFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)")); + styles.put("input_$", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + style.setBorderRight(CellStyle.BORDER_DOTTED); + style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderLeft(CellStyle.BORDER_DOTTED); + style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderTop(CellStyle.BORDER_DOTTED); + style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setDataFormat(wb.createDataFormat().getFormat("0.000%")); + styles.put("input_%", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + style.setBorderRight(CellStyle.BORDER_DOTTED); + style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderLeft(CellStyle.BORDER_DOTTED); + style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderTop(CellStyle.BORDER_DOTTED); + style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setDataFormat(wb.createDataFormat().getFormat("0")); + styles.put("input_i", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setFont(itemFont); + style.setDataFormat(wb.createDataFormat().getFormat("m/d/yy")); + styles.put("input_d", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + style.setBorderRight(CellStyle.BORDER_DOTTED); + style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderLeft(CellStyle.BORDER_DOTTED); + style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderTop(CellStyle.BORDER_DOTTED); + style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setDataFormat(wb.createDataFormat().getFormat("$##,##0.00")); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + styles.put("formula_$", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_RIGHT); + style.setFont(itemFont); + style.setBorderRight(CellStyle.BORDER_DOTTED); + style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderLeft(CellStyle.BORDER_DOTTED); + style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setBorderTop(CellStyle.BORDER_DOTTED); + style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setDataFormat(wb.createDataFormat().getFormat("0")); + style.setBorderBottom(CellStyle.BORDER_DOTTED); + style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + styles.put("formula_i", style); + + return styles; + } + + //define named ranges for the inputs and formulas + public static void createNames(Workbook wb){ + Name name; + + name = wb.createName(); + name.setNameName("Interest_Rate"); + name.setReference("'Loan Calculator'!$E$5"); + + name = wb.createName(); + name.setNameName("Loan_Amount"); + name.setReference("'Loan Calculator'!$E$4"); + + name = wb.createName(); + name.setNameName("Loan_Start"); + name.setReference("'Loan Calculator'!$E$7"); + + name = wb.createName(); + name.setNameName("Loan_Years"); + name.setReference("'Loan Calculator'!$E$6"); + + name = wb.createName(); + name.setNameName("Number_of_Payments"); + name.setReference("'Loan Calculator'!$E$10"); + + name = wb.createName(); + name.setNameName("Monthly_Payment"); + name.setReference("-PMT(Interest_Rate/12,Number_of_Payments,Loan_Amount)"); + + name = wb.createName(); + name.setNameName("Total_Cost"); + name.setReference("'Loan Calculator'!$E$12"); + + name = wb.createName(); + name.setNameName("Total_Interest"); + name.setReference("'Loan Calculator'!$E$11"); + + name = wb.createName(); + name.setNameName("Values_Entered"); + name.setReference("IF(Loan_Amount*Interest_Rate*Loan_Years*Loan_Start>0,1,0)"); + } +} diff --git a/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java b/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java new file mode 100755 index 0000000000..aa036f6747 --- /dev/null +++ b/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java @@ -0,0 +1,219 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.ss.examples; + +import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + +import java.util.Map; +import java.util.HashMap; +import java.io.FileOutputStream; + +/** + * A weekly timesheet created using Apache POI. + * Usage: + * TimesheetDemo -xls|xlsx + * + * @author Yegor Kozlov + */ +public class TimesheetDemo { + private static final String[] titles = { + "Person", "ID", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", + "Total\nHrs", "Overtime\nHrs", "Regular\nHrs" + }; + + private static Object[][] sample_data = { + {"Yegor Kozlov", "YK", 5.0, 8.0, 10.0, 5.0, 5.0, 7.0, 6.0}, + {"Gisella Bronzetti", "GB", 4.0, 3.0, 1.0, 3.5, null, null, 4.0}, + }; + + public static void main(String[] args) throws Exception { + Workbook wb; + + if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook(); + else wb = new XSSFWorkbook(); + + Map styles = createStyles(wb); + + Sheet sheet = wb.createSheet("Timesheet"); + PrintSetup printSetup = sheet.getPrintSetup(); + printSetup.setLandscape(true); + sheet.setFitToPage(true); + sheet.setHorizontallyCenter(true); + + //title row + Row titleRow = sheet.createRow(0); + titleRow.setHeightInPoints(45); + Cell titleCell = titleRow.createCell(0); + titleCell.setCellValue("Weekly Timesheet"); + titleCell.setCellStyle(styles.get("title")); + sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$L$1")); + + //header row + Row headerRow = sheet.createRow(1); + headerRow.setHeightInPoints(40); + Cell headerCell; + for (int i = 0; i < titles.length; i++) { + headerCell = headerRow.createCell(i); + headerCell.setCellValue(titles[i]); + headerCell.setCellStyle(styles.get("header")); + } + + int rownum = 2; + for (int i = 0; i < 10; i++) { + Row row = sheet.createRow(rownum++); + for (int j = 0; j < titles.length; j++) { + Cell cell = row.createCell(j); + if(j == 9){ + //the 10th cell contains sum over week days, e.g. SUM(C3:I3) + String ref = "C" +rownum+ ":I" + rownum; + cell.setCellFormula("SUM("+ref+")"); + cell.setCellStyle(styles.get("formula")); + } else if (j == 11){ + cell.setCellFormula("J" +rownum+ "-K" + rownum); + cell.setCellStyle(styles.get("formula")); + } else { + cell.setCellStyle(styles.get("cell")); + } + } + } + + //row with totals below + Row sumRow = sheet.createRow(rownum++); + sumRow.setHeightInPoints(35); + Cell cell; + cell = sumRow.createCell(0); + cell.setCellStyle(styles.get("formula")); + cell = sumRow.createCell(1); + cell.setCellValue("Total Hrs:"); + cell.setCellStyle(styles.get("formula")); + + for (int j = 2; j < 12; j++) { + cell = sumRow.createCell(j); + String ref = (char)('A' + j) + "3:" + (char)('A' + j) + "12"; + cell.setCellFormula("SUM(" + ref + ")"); + if(j >= 9) cell.setCellStyle(styles.get("formula_2")); + else cell.setCellStyle(styles.get("formula")); + } + rownum++; + sumRow = sheet.createRow(rownum++); + sumRow.setHeightInPoints(25); + cell = sumRow.createCell(0); + cell.setCellValue("Total Regular Hours"); + cell.setCellStyle(styles.get("formula")); + cell = sumRow.createCell(1); + cell.setCellFormula("L13"); + cell.setCellStyle(styles.get("formula_2")); + sumRow = sheet.createRow(rownum++); + sumRow.setHeightInPoints(25); + cell = sumRow.createCell(0); + cell.setCellValue("Total Overtime Hours"); + cell.setCellStyle(styles.get("formula")); + cell = sumRow.createCell(1); + cell.setCellFormula("K13"); + cell.setCellStyle(styles.get("formula_2")); + + //set sample data + for (int i = 0; i < sample_data.length; i++) { + Row row = sheet.getRow(2 + i); + for (int j = 0; j < sample_data[i].length; j++) { + if(sample_data[i][j] == null) continue; + + if(sample_data[i][j] instanceof String) { + row.getCell(j).setCellValue((String)sample_data[i][j]); + } else { + row.getCell(j).setCellValue((Double)sample_data[i][j]); + } + } + } + + //finally set column widths, the width is measured in units of 1/256th of a character width + sheet.setColumnWidth(0, 30*256); //30 characters wide + for (int i = 2; i < 9; i++) { + sheet.setColumnWidth(i, 6*256); //6 characters wide + } + sheet.setColumnWidth(10, 10*256); //10 characters wide + + // Write the output to a file + String file = "timesheet.xls"; + if(wb instanceof XSSFWorkbook) file += "x"; + FileOutputStream out = new FileOutputStream(file); + wb.write(out); + out.close(); + } + + /** + * Create a library of cell styles + */ + private static Map createStyles(Workbook wb){ + Map styles = new HashMap(); + CellStyle style; + Font titleFont = wb.createFont(); + titleFont.setFontHeightInPoints((short)18); + titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFont(titleFont); + styles.put("title", style); + + Font monthFont = wb.createFont(); + monthFont.setFontHeightInPoints((short)11); + monthFont.setColor(IndexedColors.WHITE.getIndex()); + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFont(monthFont); + style.setWrapText(true); + styles.put("header", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setWrapText(true); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderTop(CellStyle.BORDER_THIN); + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); + styles.put("cell", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setDataFormat(wb.createDataFormat().getFormat("0.00")); + styles.put("formula", style); + + style = wb.createCellStyle(); + style.setAlignment(CellStyle.ALIGN_CENTER); + style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); + style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setDataFormat(wb.createDataFormat().getFormat("0.00")); + styles.put("formula_2", style); + + return styles; + } +} diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BusinessPlan.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/BusinessPlan.java deleted file mode 100755 index b562417ac6..0000000000 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/BusinessPlan.java +++ /dev/null @@ -1,310 +0,0 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ -package org.apache.poi.xssf.usermodel.examples; - -import org.apache.poi.xssf.usermodel.*; -import org.apache.poi.ss.usermodel.*; - -import java.util.Map; -import java.util.HashMap; -import java.util.Date; -import java.util.Calendar; -import java.io.FileOutputStream; -import java.text.SimpleDateFormat; - -/** - * A business plan demo - * - * @author Yegor Kozlov - */ -public class BusinessPlan { - - private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM"); - - private static final String[] days = { - "ID", "Project Name", "Owner", "Days", "Start", "End"}; - - //sample data to fill the sheet. - private static final String[][] data = { - {"1.0", "Marketing Research Tactical Plan", "R. Ihrig", "70", "9-Jul", null, - "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"}, - null, - {"1.1", "Scope Definition Phase", "R. Ihrig", "10", "9-Jul", null, - "x", "x", null, null, null, null, null, null, null, null, null}, - {"1.1.1", "Define research objectives", "R. Ihrig", "3", "9-Jul", null, - "x", null, null, null, null, null, null, null, null, null, null}, - {"1.1.2", "Define research requirements", "S. Abbas", "7", "10-Jul", null, - "x", "x", null, null, null, null, null, null, null, null, null}, - {"1.1.3", "Determine in-house resource or hire vendor", "R. Ihrig", "2", "15-Jul", null, - "x", "x", null, null, null, null, null, null, null, null, null}, - null, - {"1.2", "Vendor Selection Phase", "R. Ihrig", "19", "19-Jul", null, - null, "x", "x", "x", "x", null, null, null, null, null, null}, - {"1.2.1", "Define vendor selection criteria", "R. Ihrig", "3", "19-Jul", null, - null, "x", null, null, null, null, null, null, null, null, null}, - {"1.2.2", "Develop vendor selection questionnaire", "S. Abbas, T. Wang", "2", "22-Jul", null, - null, "x", "x", null, null, null, null, null, null, null, null}, - {"1.2.3", "Develop Statement of Work", "S. Abbas", "4", "26-Jul", null, - null, null, "x", "x", null, null, null, null, null, null, null}, - {"1.2.4", "Evaluate proposal", "R. Ihrig, S. Abbas", "4", "2-Aug", null, - null, null, null, "x", "x", null, null, null, null, null, null}, - {"1.2.5", "Select vendor", "R. Ihrig", "1", "6-Aug", null, - null, null, null, null, "x", null, null, null, null, null, null}, - null, - {"1.3", "Research Phase", "Y. Li", "47", "9-Aug", null, - null, null, null, null, "x", "x", "x", "x", "x", "x", "x"}, - {"1.3.1", "Develop market research information needs questionnaire", "Y. Li", "2", "9-Aug", null, - null, null, null, null, "x", null, null, null, null, null, null}, - {"1.3.2", "Interview marketing group for market research needs", "Y. Li", "2", "11-Aug", null, - null, null, null, null, "x", "x", null, null, null, null, null}, - {"1.3.3", "Document information needs", "Y. Li, S. Abbas", "1", "13-Aug", null, - null, null, null, null, null, "x", null, null, null, null, null}, - }; - - public static void main(String[] args) throws Exception { - Calendar calendar = Calendar.getInstance(); - int year = calendar.get(Calendar.YEAR); - - XSSFWorkbook wb = new XSSFWorkbook(); - Map styles = createStyles(wb); - - XSSFSheet sheet = wb.createSheet("Plan"); - - //turn off gridlines - sheet.setDisplayGridlines(false); - sheet.setPrintGridlines(false); - XSSFPrintSetup printSetup = sheet.getPrintSetup(); - printSetup.setOrientation(PrintOrientation.LANDSCAPE); - sheet.setFitToPage(true); - sheet.setHorizontallyCenter(true); - - //the header row: centered text in 48pt font - XSSFRow headerRow = sheet.createRow(0); - headerRow.setHeightInPoints(12.75f); - for (int i = 0; i < days.length; i++) { - XSSFCell cell = headerRow.createCell(i); - cell.setCellValue(days[i]); - cell.setCellStyle(styles.get("header")); - } - //columns for 11 weeks starting from 9-Jul - calendar.setTime(fmt.parse("9-Jul")); - calendar.set(Calendar.YEAR, year); - for (int i = 0; i < 11; i++) { - XSSFCell cell = headerRow.createCell(days.length + i); - cell.setCellValue(calendar); - cell.setCellStyle(styles.get("header_date")); - calendar.roll(Calendar.WEEK_OF_YEAR, true); - } - - sheet.createFreezePane(0, 1); - XSSFRow row; - XSSFCell cell; - - int rownum = 1; - for (int i = 0; i < data.length; i++, rownum++) { - row = sheet.createRow(rownum); - if(data[i] == null) continue; - - for (int j = 0; j < data[i].length; j++) { - cell = row.createCell(j); - String styleName; - boolean isHeader = i == 0 || data[i-1] == null; - switch(j){ - case 0: - if(isHeader) { - styleName = "cell_b"; - cell.setCellValue(Double.parseDouble(data[i][j])); - } else { - styleName = "cell_normal"; - cell.setCellValue(data[i][j]); - } - break; - case 1: - if(isHeader) { - styleName = i == 0 ? "cell_h" : "cell_bb"; - } else { - styleName = "cell_indented"; - } - cell.setCellValue(data[i][j]); - break; - case 2: - styleName = isHeader ? "cell_b" : "cell_normal"; - cell.setCellValue(data[i][j]); - break; - case 3: - styleName = isHeader ? "cell_b_centered" : "cell_normal_centered"; - cell.setCellValue(Integer.parseInt(data[i][j])); - break; - case 4: { - calendar.setTime(fmt.parse(data[i][j])); - calendar.set(Calendar.YEAR, year); - cell.setCellValue(calendar); - styleName = isHeader ? "cell_b_date" : "cell_normal_date"; - break; - } - case 5: { - int r = rownum + 1; - String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")"; - cell.setCellFormula(fmla); - styleName = isHeader ? "cell_bg" : "cell_g"; - break; - } - default: - styleName = data[i][j] != null ? "cell_blue" : "cell_normal"; - } - - cell.setCellStyle(styles.get(styleName)); - } - } - - - sheet.groupRow(5, 7); - sheet.groupRow(10, 14); - sheet.groupRow(17, 19); - - sheet.setColumnWidth(0, 256*6); - sheet.setColumnWidth(1, 256*33); - sheet.setColumnWidth(2, 256*20); - sheet.setZoom(75); - - - // Write the output to a file - FileOutputStream out = new FileOutputStream("xssf-plan.xlsx"); - wb.write(out); - out.close(); - } - - /** - * cell styles used for formatting calendar sheets - */ - private static Map createStyles(XSSFWorkbook wb){ - Map styles = new HashMap(); - XSSFDataFormat df = wb.createDataFormat(); - - XSSFCellStyle style; - XSSFFont headerFont = wb.createFont(); - headerFont.setBold(true); - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.CENTER); - style.setFillForegroundColor(new XSSFColor(new java.awt.Color(204, 204, 255))); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setFont(headerFont); - styles.put("header", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.CENTER); - style.setFillForegroundColor(new XSSFColor(new java.awt.Color(204, 204, 255))); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setFont(headerFont); - style.setDataFormat(df.getFormat("d-mmm")); - styles.put("header_date", style); - - XSSFFont font1 = wb.createFont(); - font1.setBold(true); - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.LEFT); - style.setFont(font1); - styles.put("cell_b", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.CENTER); - style.setFont(font1); - styles.put("cell_b_centered", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.RIGHT); - style.setFont(font1); - style.setDataFormat(df.getFormat("d-mmm")); - styles.put("cell_b_date", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.RIGHT); - style.setFont(font1); - style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 228, 228))); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setDataFormat(df.getFormat("d-mmm")); - styles.put("cell_g", style); - - XSSFFont font2 = wb.createFont(); - font2.setColor(IndexedColors.BLUE.getIndex()); - font2.setBold(true); - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.LEFT); - style.setFont(font2); - styles.put("cell_bb", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.RIGHT); - style.setFont(font1); - style.setFillForegroundColor(new XSSFColor(new java.awt.Color(228, 228, 228))); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - style.setDataFormat(df.getFormat("d-mmm")); - styles.put("cell_bg", style); - - XSSFFont font3 = wb.createFont(); - font3.setFontHeightInPoints((short)14); - font3.setColor(IndexedColors.DARK_BLUE.getIndex()); - font3.setBold(true); - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.LEFT); - style.setFont(font3); - style.setWrapText(true); - styles.put("cell_h", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.LEFT); - style.setWrapText(true); - styles.put("cell_normal", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.CENTER); - style.setWrapText(true); - styles.put("cell_normal_centered", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.RIGHT); - style.setWrapText(true); - style.setDataFormat(df.getFormat("d-mmm")); - styles.put("cell_normal_date", style); - - style = createBorderedStyle(wb); - style.setAlignment(HorizontalAlignment.LEFT); - style.setIndention((short)1); - style.setWrapText(true); - styles.put("cell_indented", style); - - style = createBorderedStyle(wb); - style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - styles.put("cell_blue", style); - - return styles; - } - - private static XSSFCellStyle createBorderedStyle(XSSFWorkbook wb){ - XSSFCellStyle style = wb.createCellStyle(); - style.setBorderRight(BorderStyle.THIN); - style.setRightBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderBottom(BorderStyle.THIN); - style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderLeft(BorderStyle.THIN); - style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderTop(BorderStyle.THIN); - style.setTopBorderColor(IndexedColors.BLACK.getIndex()); - return style; - } -} diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java index 52dcaed517..ffdcf37351 100755 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java @@ -27,6 +27,9 @@ import java.util.HashMap; /** * A monthly calendar created using Apache POI. Each month is on a separate sheet. + * This is a version of org.apache.poi.ss.examples.CalendarDemo that demonstrates + * some XSSF features not avaiable when using common HSSF-XSSF interfaces. + * *
  * Usage:
  * CalendarDemo 
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoanCalculator.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoanCalculator.java
deleted file mode 100755
index 187ab579cd..0000000000
--- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LoanCalculator.java
+++ /dev/null
@@ -1,312 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-package org.apache.poi.xssf.usermodel.examples;
-
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-
-import java.util.Map;
-import java.util.HashMap;
-import java.io.FileOutputStream;
-
-/**
- * Simple Loan Calculator
- *
- * @author Yegor Kozlov
- */
-public class LoanCalculator {
-
-    public static void main(String[] args) throws Exception {
-        XSSFWorkbook wb = new XSSFWorkbook();
-        Map styles = createStyles(wb);
-        XSSFSheet sheet = wb.createSheet("Loan Calculator");
-        sheet.setPrintGridlines(false);
-        sheet.setDisplayGridlines(false);
-
-        XSSFPrintSetup printSetup = sheet.getPrintSetup();
-        printSetup.setOrientation(PrintOrientation.LANDSCAPE);
-        sheet.setFitToPage(true);
-        sheet.setHorizontallyCenter(true);
-
-        sheet.setColumnWidth(0, 3*256);
-        sheet.setColumnWidth(1, 3*256);
-        sheet.setColumnWidth(2, 11*256);
-        sheet.setColumnWidth(3, 14*256);
-        sheet.setColumnWidth(4, 14*256);
-        sheet.setColumnWidth(5, 14*256);
-        sheet.setColumnWidth(6, 14*256);
-
-        createNames(wb);
-
-        XSSFRow titleRow = sheet.createRow(0);
-        titleRow.setHeightInPoints(35);
-        for (int i = 1; i <= 7; i++) {
-            titleRow.createCell(i).setCellStyle(styles.get("title"));
-        }
-        XSSFCell titleCell = titleRow.getCell(2);
-        titleCell.setCellValue("Simple Loan Calculator");
-        sheet.addMergedRegion(CellRangeAddress.valueOf("$C$1:$H$1"));
-
-        XSSFRow row = sheet.createRow(2);
-        XSSFCell cell = row.createCell(4);
-        cell.setCellValue("Enter values");
-        cell.setCellStyle(styles.get("item_right"));
-
-        row = sheet.createRow(3);
-        cell = row.createCell(2);
-        cell.setCellValue("Loan amount");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellStyle(styles.get("input_$"));
-
-        row = sheet.createRow(4);
-        cell = row.createCell(2);
-        cell.setCellValue("Annual interest rate");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellStyle(styles.get("input_%"));
-
-        row = sheet.createRow(5);
-        cell = row.createCell(2);
-        cell.setCellValue("Loan period in years");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellStyle(styles.get("input_i"));
-
-        row = sheet.createRow(6);
-        cell = row.createCell(2);
-        cell.setCellValue("Start date of loan");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellStyle(styles.get("input_d"));
-
-        row = sheet.createRow(8);
-        cell = row.createCell(2);
-        cell.setCellValue("Monthly payment");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellFormula("IF(Values_Entered,Monthly_Payment,\"\")");
-        cell.setCellStyle(styles.get("formula_$"));
-
-        row = sheet.createRow(9);
-        cell = row.createCell(2);
-        cell.setCellValue("Number of payments");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellFormula("IF(Values_Entered,Loan_Years*12,\"\")");
-        cell.setCellStyle(styles.get("formula_i"));
-
-        row = sheet.createRow(10);
-        cell = row.createCell(2);
-        cell.setCellValue("Total interest");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellFormula("IF(Values_Entered,Total_Cost-Loan_Amount,\"\")");
-        cell.setCellStyle(styles.get("formula_$"));
-
-        row = sheet.createRow(11);
-        cell = row.createCell(2);
-        cell.setCellValue("Total cost of loan");
-        cell.setCellStyle(styles.get("item_left"));
-        cell = row.createCell(4);
-        cell.setCellFormula("IF(Values_Entered,Monthly_Payment*Number_of_Payments,\"\")");
-        cell.setCellStyle(styles.get("formula_$"));
-
-        sheet.setActiveCell("E4");
-
-        // Write the output to a file
-        FileOutputStream out = new FileOutputStream("loan-calculator.xlsx");
-        wb.write(out);
-        out.close();
-    }
-
-    /**
-     * cell styles used for formatting calendar sheets
-     */
-    private static Map createStyles(XSSFWorkbook wb){
-        Map styles = new HashMap();
-
-        XSSFCellStyle style;
-        XSSFFont titleFont = wb.createFont();
-        titleFont.setFontHeightInPoints((short)14);
-        titleFont.setFontName("Trebuchet MS");
-        style = wb.createCellStyle();
-        style.setFont(titleFont);
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        styles.put("title", style);
-
-        XSSFFont itemFont = wb.createFont();
-        itemFont.setFontHeightInPoints((short)9);
-        itemFont.setFontName("Trebuchet MS");
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.LEFT);
-        style.setFont(itemFont);
-        styles.put("item_left", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        styles.put("item_right", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        style.setBorderRight(BorderStyle.DOTTED);
-        style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderLeft(BorderStyle.DOTTED);
-        style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderTop(BorderStyle.DOTTED);
-        style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setDataFormat(wb.createDataFormat().getFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"));
-        styles.put("input_$", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        style.setBorderRight(BorderStyle.DOTTED);
-        style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderLeft(BorderStyle.DOTTED);
-        style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderTop(BorderStyle.DOTTED);
-        style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setDataFormat(wb.createDataFormat().getFormat("0.000%"));
-        styles.put("input_%", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        style.setBorderRight(BorderStyle.DOTTED);
-        style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderLeft(BorderStyle.DOTTED);
-        style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderTop(BorderStyle.DOTTED);
-        style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setDataFormat(wb.createDataFormat().getFormat("0"));
-        styles.put("input_i", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setFont(itemFont);
-        style.setDataFormat(wb.createDataFormat().getFormat("m/d/yy"));
-        styles.put("input_d", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        style.setBorderRight(BorderStyle.DOTTED);
-        style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderLeft(BorderStyle.DOTTED);
-        style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderTop(BorderStyle.DOTTED);
-        style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setDataFormat(wb.createDataFormat().getFormat("$##,##0.00"));
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        styles.put("formula_$", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.RIGHT);
-        style.setFont(itemFont);
-        style.setBorderRight(BorderStyle.DOTTED);
-        style.setRightBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderLeft(BorderStyle.DOTTED);
-        style.setLeftBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setBorderTop(BorderStyle.DOTTED);
-        style.setTopBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setDataFormat(wb.createDataFormat().getFormat("0"));
-        style.setBorderBottom(BorderStyle.DOTTED);
-        style.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex());
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        styles.put("formula_i", style);
-
-        return styles;
-    }
-
-    //define named ranges for the inputs and formulas
-    public static void createNames(XSSFWorkbook wb){
-        XSSFName name;
-
-        name = wb.createName();
-        name.setNameName("Header_Row");
-        name.setReference("ROW('Loan Calculator'!#REF!)");
-
-        name = wb.createName();
-        name.setNameName("Interest_Rate");
-        name.setReference("'Loan Calculator'!$E$5");
-
-        name = wb.createName();
-        name.setNameName("Loan_Amount");
-        name.setReference("'Loan Calculator'!$E$4");
-
-        name = wb.createName();
-        name.setNameName("Loan_Not_Paid");
-        name.setReference("F(Payment_Number<=Number_of_Payments,1,0)");
-
-        name = wb.createName();
-        name.setNameName("Loan_Start");
-        name.setReference("'Loan Calculator'!$E$7");
-
-        name = wb.createName();
-        name.setNameName("Loan_Years");
-        name.setReference("'Loan Calculator'!$E$6");
-
-        name = wb.createName();
-        name.setNameName("Monthly_Payment");
-        name.setReference("-PMT(Interest_Rate/12,Number_of_Payments,Loan_Amount)");
-
-        name = wb.createName();
-        name.setNameName("Number_of_Payments");
-        name.setReference("'Loan Calculator'!$E$10");
-
-        name = wb.createName();
-        name.setNameName("Payment_Number");
-        name.setReference("ROW()-Header_Row");
-
-        name = wb.createName();
-        name.setNameName("Principal");
-        name.setReference("-PPMT(Interest_Rate/12,Payment_Number,Number_of_Payments,Loan_Amount)");
-
-        name = wb.createName();
-        name.setNameName("Total_Cost");
-        name.setReference("'Loan Calculator'!$E$12");
-
-        name = wb.createName();
-        name.setNameName("Total_Interest");
-        name.setReference("'Loan Calculator'!$E$11");
-
-        name = wb.createName();
-        name.setNameName("Values_Entered");
-        name.setReference("IF(Loan_Amount*Interest_Rate*Loan_Years*Loan_Start>0,1,0)");
-
-
-    }
-}
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/TimesheetDemo.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/TimesheetDemo.java
deleted file mode 100755
index 9c28e33aff..0000000000
--- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/TimesheetDemo.java
+++ /dev/null
@@ -1,209 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-package org.apache.poi.xssf.usermodel.examples;
-
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.ss.usermodel.*;
-
-import java.util.Map;
-import java.util.HashMap;
-import java.io.FileOutputStream;
-
-/**
- * A weekly timesheet created using Apache POI.
- *
- * @author Yegor Kozlov
- */
-public class TimesheetDemo {
-    private static final String[] titles = {
-            "Person",	"ID", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
-            "Total\nHrs", "Overtime\nHrs", "Regular\nHrs"
-    };
-
-    private static Object[][] sample_data = {
-            {"Yegor Kozlov", "YK", 5.0, 8.0, 10.0, 5.0, 5.0, 7.0, 6.0},
-            {"Gisella Bronsetti", "GB", 4.0, 3.0, 1.0, 3.5, null, null, 4.0},
-    };
-
-    public static void main(String[] args) throws Exception {
-
-        XSSFWorkbook wb = new XSSFWorkbook();
-        Map styles = createStyles(wb);
-
-        XSSFSheet sheet = wb.createSheet("Timesheet");
-        XSSFPrintSetup printSetup = sheet.getPrintSetup();
-        printSetup.setOrientation(PrintOrientation.LANDSCAPE);
-        sheet.setFitToPage(true);
-        sheet.setHorizontallyCenter(true);
-
-        //title row
-        XSSFRow titleRow = sheet.createRow(0);
-        titleRow.setHeightInPoints(45);
-        XSSFCell titleCell = titleRow.createCell(0);
-        titleCell.setCellValue("Weekly Timesheet");
-        titleCell.setCellStyle(styles.get("title"));
-        sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$L$1"));
-
-        //header row
-        XSSFRow headerRow = sheet.createRow(1);
-        headerRow.setHeightInPoints(40);
-        XSSFCell headerCell;
-        for (int i = 0; i < titles.length; i++) {
-            headerCell = headerRow.createCell(i);
-            headerCell.setCellValue(titles[i]);
-            headerCell.setCellStyle(styles.get("header"));
-        }
-
-        int rownum = 2;
-        for (int i = 0; i < 10; i++) {
-            XSSFRow row = sheet.createRow(rownum++);
-            for (int j = 0; j < titles.length; j++) {
-                XSSFCell cell = row.createCell(j);
-                if(j == 9){
-                    //the 10th cell contains sum over week days, e.g. SUM(C3:I3)
-                    String ref = "C" +rownum+ ":I" + rownum;
-                    cell.setCellFormula("SUM("+ref+")");
-                    cell.setCellStyle(styles.get("formula"));
-                } else if (j == 11){
-                    cell.setCellFormula("J" +rownum+ "-K" + rownum);
-                    cell.setCellStyle(styles.get("formula"));
-                } else {
-                    cell.setCellStyle(styles.get("cell"));
-                }
-            }
-        }
-
-        //row with totals below
-        XSSFRow sumRow = sheet.createRow(rownum++);
-        sumRow.setHeightInPoints(35);
-        XSSFCell cell;
-        cell = sumRow.createCell(0);
-        cell.setCellStyle(styles.get("formula"));
-        cell = sumRow.createCell(1);
-        cell.setCellValue("Total Hrs:");
-        cell.setCellStyle(styles.get("formula"));
-
-        for (int j = 2; j < 12; j++) {
-            cell = sumRow.createCell(j);
-            String ref = (char)('A' + j) + "3:" + (char)('A' + j) + "12";
-            cell.setCellFormula("SUM(" + ref + ")");
-            if(j >= 9) cell.setCellStyle(styles.get("formula_2"));
-            else cell.setCellStyle(styles.get("formula"));
-        }
-        rownum++;
-        sumRow = sheet.createRow(rownum++);
-        sumRow.setHeightInPoints(25);
-        cell = sumRow.createCell(0);
-        cell.setCellValue("Total Regular Hours");
-        cell.setCellStyle(styles.get("formula"));
-        cell = sumRow.createCell(1);
-        cell.setCellFormula("L13");
-        cell.setCellStyle(styles.get("formula_2"));
-        sumRow = sheet.createRow(rownum++);
-        sumRow.setHeightInPoints(25);
-        cell = sumRow.createCell(0);
-        cell.setCellValue("Total Overtime Hours");
-        cell.setCellStyle(styles.get("formula"));
-        cell = sumRow.createCell(1);
-        cell.setCellFormula("K13");
-        cell.setCellStyle(styles.get("formula_2"));
-
-        //set sample data
-        for (int i = 0; i < sample_data.length; i++) {
-            XSSFRow row = sheet.getRow(2 + i);
-            for (int j = 0; j < sample_data[i].length; j++) {
-                if(sample_data[i][j] == null) continue;
-
-                if(sample_data[i][j] instanceof String) {
-                    row.getCell(j).setCellValue((String)sample_data[i][j]);
-                } else {
-                    row.getCell(j).setCellValue((Double)sample_data[i][j]);
-                }
-            }
-        }
-
-        //finally set column widths
-        sheet.setColumnWidth(0, 30*256);
-        for (int i = 2; i < 9; i++) {
-            sheet.setColumnWidth(i, 6*256);
-        }
-
-        // Write the output to a file
-        FileOutputStream out = new FileOutputStream("ooxml-timesheet.xlsx");
-        wb.write(out);
-        out.close();
-    }
-
-    private static Map createStyles(XSSFWorkbook wb){
-        Map styles = new HashMap();
-        XSSFCellStyle style;
-        XSSFFont titleFont = wb.createFont();
-        titleFont.setFontHeightInPoints((short)18);
-        titleFont.setBold(true);
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        style.setFont(titleFont);
-        styles.put("title", style);
-
-        XSSFFont monthFont = wb.createFont();
-        monthFont.setFontHeightInPoints((short)11);
-        monthFont.setColor(new XSSFColor(new java.awt.Color(255, 255, 255)));
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(102, 102, 102)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        style.setFont(monthFont);
-        style.setWrapText(true);
-        styles.put("header", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setWrapText(true);
-        style.setBorderRight(BorderStyle.THIN);
-        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
-        style.setBorderLeft(BorderStyle.THIN);
-        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
-        style.setBorderTop(BorderStyle.THIN);
-        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
-        style.setBorderBottom(BorderStyle.THIN);
-        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
-        styles.put("cell", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(234, 234, 234)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
-        styles.put("formula", style);
-
-        style = wb.createCellStyle();
-        style.setAlignment(HorizontalAlignment.CENTER);
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
-        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(192, 192, 192)));
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        style.setDataFormat(wb.createDataFormat().getFormat("0.00"));
-        styles.put("formula_2", style);
-
-        return styles;
-    }
-}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 82277661ae..ae50f43765 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -509,8 +509,7 @@ public class HSSFCell implements Cell {
     }
 
     /**
-     * set a string value for the cell. Please note that if you are using
-     * full 16 bit unicode you should call setEncoding() first.
+     * Set a string value for the cell.
      *
      * @param value  value to set the cell to.  For formulas we'll set the formula
      * string, for String cells we'll set its value.  For other types we will
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 5733bbcf37..e6e9ac9b46 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -1626,6 +1626,12 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
         sheet.groupColumnRange(fromColumn, toColumn, false);
     }
 
+    /**
+     * Tie a range of cell together so that they can be collapsed or expanded
+     *
+     * @param fromRow   start row (0-based)
+     * @param toRow     end row (0-based)
+     */
     public void groupRow(int fromRow, int toRow)
     {
         sheet.groupRowRange( fromRow, toRow, true );
diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
index 4e132dafbd..a315493781 100644
--- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
+++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
@@ -145,21 +145,33 @@ public interface Cell {
     void setCellValue(Calendar value);
 
     /**
-     * set a string value for the cell. Please note that if you are using
-     * full 16 bit unicode you should call setEncoding() first.
+     * Set a rich string value for the cell.
      *
      * @param value  value to set the cell to.  For formulas we'll set the formula
      * string, for String cells we'll set its value.  For other types we will
      * change the cell to a string cell and set its value.
      * If value is null then we will change the cell to a Blank cell.
      */
-
     void setCellValue(RichTextString value);
 
+    /**
+     * Set a string value for the cell.
+     *
+     * @param value  value to set the cell to.  For formulas we'll set the formula
+     * string, for String cells we'll set its value.  For other types we will
+     * change the cell to a string cell and set its value.
+     * If value is null then we will change the cell to a Blank cell.
+     */
     void setCellValue(String value);
 
+    /**
+     * Set a formula value for the cell.
+     */
     void setCellFormula(String formula);
 
+    /**
+     * Get the formula value of the cell.
+     */
     String getCellFormula();
 
     /**
diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java
index 12c6a6a0c1..bd500d56ae 100644
--- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java
+++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java
@@ -409,7 +409,6 @@ public interface Sheet extends Iterable {
      * Additionally shifts merged regions that are completely defined in these
      * rows (ie. merged 2 cells on a row to be shifted).
      * 

- * TODO Might want to add bounds checking here * @param startRow the row to start shifting * @param endRow the row to end shifting * @param n the number of rows to shift @@ -541,14 +540,6 @@ public interface Sheet extends Iterable { */ void removeColumnBreak(short column); - /** - * Creates the toplevel drawing patriarch. This will have the effect of - * removing any existing drawings on this sheet. - * - * @return The new patriarch. - */ - //Patriarch createDrawingPatriarch(); - /** * Expands or collapses a column group. * @@ -567,10 +558,28 @@ public interface Sheet extends Iterable { void ungroupColumn(short fromColumn, short toColumn); + /** + * Tie a range of rows together so that they can be collapsed or expanded + * + * @param fromRow start row (0-based) + * @param toRow end row (0-based) + */ void groupRow(int fromRow, int toRow); + /** + * Ungroup a range of rows that were previously groupped + * + * @param fromRow start row (0-based) + * @param toRow end row (0-based) + */ void ungroupRow(int fromRow, int toRow); + /** + * Set view state of a groupped range of rows + * + * @param row start row of a groupped range of rows (0-based) + * @param collapse whether to expand/collapse the detail rows + */ void setRowGroupCollapsed(int row, boolean collapse); /** @@ -599,6 +608,11 @@ public interface Sheet extends Iterable { */ Comment getCellComment(int row, int column); + /** + * Creates the top-level drawing patriarch. + * + * @return The new drawing patriarch. + */ Drawing createDrawingPatriarch(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java index 148a02fcb2..10a4b4755e 100755 --- a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java +++ b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java @@ -32,7 +32,7 @@ public class XSSFSave { XSSFWorkbook wb = new XSSFWorkbook(args[i]); int sep = args[i].lastIndexOf('.'); - String outfile = args[i].substring(0, sep) + "-save.xlsx"; + String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x"); FileOutputStream out = new FileOutputStream(outfile); wb.write(out); out.close(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 24a3f252f0..7613c87cf1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -952,20 +952,26 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { setSheetFormatPrOutlineLevelCol(); } + /** + * Tie a range of cell together so that they can be collapsed or expanded + * + * @param fromRow start row (0-based) + * @param toRow end row (0-based) + */ public void groupRow(int fromRow, int toRow) { - for(int i=fromRow;i<=toRow;i++){ - XSSFRow xrow = getRow(i-1); - if(xrow == null){//create a new Row - xrow = createRow(i-1); + for (int i = fromRow; i <= toRow; i++) { + XSSFRow xrow = getRow(i); + if (xrow == null) { + xrow = createRow(i); } - CTRow ctrow=xrow.getCTRow(); - short outlineLevel=ctrow.getOutlineLevel(); - ctrow.setOutlineLevel((short)(outlineLevel+1)); - } - setSheetFormatPrOutlineLevelRow(); + CTRow ctrow = xrow.getCTRow(); + short outlineLevel = ctrow.getOutlineLevel(); + ctrow.setOutlineLevel((short) (outlineLevel + 1)); + } + setSheetFormatPrOutlineLevelRow(); } - private short getMaxOutlineLevelRows(){ + private short getMaxOutlineLevelRows(){ short outlineLevel=0; for(Row r : rows.values()){ XSSFRow xrow=(XSSFRow)r; @@ -1478,9 +1484,15 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { setSheetFormatPrOutlineLevelCol(); } + /** + * Ungroup a range of rows that were previously groupped + * + * @param fromRow start row (0-based) + * @param toRow end row (0-based) + */ public void ungroupRow(int fromRow, int toRow) { for (int i = fromRow; i <= toRow; i++) { - XSSFRow xrow = getRow(i - 1); + XSSFRow xrow = getRow(i); if (xrow != null) { CTRow ctrow = xrow.getCTRow(); short outlinelevel = ctrow.getOutlineLevel(); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 62d2b7d989..dd0446c9dd 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -789,19 +789,19 @@ public class TestXSSFSheet extends TestCase { //one level sheet.groupRow(9,10); assertEquals(2,sheet.getPhysicalNumberOfRows()); - CTRow ctrow = sheet.getRow(8).getCTRow(); + CTRow ctrow = sheet.getRow(9).getCTRow(); assertNotNull(ctrow); - assertEquals(9,ctrow.getR()); + assertEquals(10,ctrow.getR()); assertEquals(1, ctrow.getOutlineLevel()); assertEquals(1,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow()); //two level sheet.groupRow(10,13); assertEquals(5,sheet.getPhysicalNumberOfRows()); - ctrow = sheet.getRow(9).getCTRow(); + ctrow = sheet.getRow(10).getCTRow(); assertNotNull(ctrow); - assertEquals(10,ctrow.getR()); + assertEquals(11,ctrow.getR()); assertEquals(2, ctrow.getOutlineLevel()); assertEquals(2,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow()); diff --git a/src/scratchpad/examples/src/org/apache/poi/hslf/examples/SoundFinder.java b/src/scratchpad/examples/src/org/apache/poi/hslf/examples/SoundFinder.java new file mode 100644 index 0000000000..b31019db94 --- /dev/null +++ b/src/scratchpad/examples/src/org/apache/poi/hslf/examples/SoundFinder.java @@ -0,0 +1,80 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.hslf.examples; +import org.apache.poi.ddf.*; +import org.apache.poi.hslf.model.*; +import org.apache.poi.hslf.record.InteractiveInfo; +import org.apache.poi.hslf.record.InteractiveInfoAtom; +import org.apache.poi.hslf.record.Record; +import org.apache.poi.hslf.usermodel.*; +import java.io.FileInputStream; +import java.util.Iterator; +import java.util.List; + +/** + * For each slide iterate over shapes and found associated sound data. + * + * @author Yegor Kozlov + */ +public class SoundFinder { + public static void main(String[] args) throws Exception { + SlideShow ppt = new SlideShow(new FileInputStream(args[0])); + SoundData[] sounds = ppt.getSoundData(); + + Slide[] slide = ppt.getSlides(); + for (int i = 0; i < slide.length; i++) { + Shape[] shape = slide[i].getShapes(); + for (int j = 0; j < shape.length; j++) { + int soundRef = getSoundReference(shape[j]); + if(soundRef != -1) { + System.out.println("Slide["+i+"], shape["+j+"], soundRef: "+soundRef); + System.out.println(" " + sounds[soundRef].getSoundName()); + System.out.println(" " + sounds[soundRef].getSoundType()); + } + } + } + } + + /** + * Check if a given shape is associated with a sound. + * @return 0-based reference to a sound in the sound collection + * or -1 if the shape is not associated with a sound + */ + protected static int getSoundReference(Shape shape){ + int soundRef = -1; + //dive into the shape container and search for InteractiveInfoAtom + EscherContainerRecord spContainer = shape.getSpContainer(); + List spchild = spContainer.getChildRecords(); + for (Iterator it = spchild.iterator(); it.hasNext();) { + EscherRecord obj = (EscherRecord) it.next(); + if (obj.getRecordId() == EscherClientDataRecord.RECORD_ID) { + byte[] data = obj.serialize(); + Record[] records = Record.findChildRecords(data, 8, +data.length - 8); + for (int j = 0; j < records.length; j++) { + if (records[j] instanceof InteractiveInfo) { + InteractiveInfoAtom info = ((InteractiveInfo)records[j]).getInteractiveInfoAtom(); + if (info.getAction() == InteractiveInfoAtom.ACTION_MEDIA) { + soundRef = info.getSoundRef(); + } + } + } + } + } + return soundRef; + } +} diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java b/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java index fa7a093c0e..111561bcc1 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestMetaDataIPI.java @@ -286,7 +286,7 @@ public class TestMetaDataIPI extends TestCase{ /* Insert some custom properties into the container. */ customProperties.put("Key1", "Value1"); - customProperties.put("Schl�ssel2", "Wert2"); + customProperties.put("Schl\u00fcssel2", "Wert2"); customProperties.put("Sample Integer", new Integer(12345)); customProperties.put("Sample Boolean", new Boolean(true)); Date date=new Date(); @@ -325,8 +325,8 @@ public class TestMetaDataIPI extends TestCase{ /* Insert some custom properties into the container. */ String a1=(String) customProperties.get("Key1"); assertEquals("Key1","Value1",a1); - String a2=(String) customProperties.get("Schl�ssel2"); - assertEquals("Schl�ssel2","Wert2",a2); + String a2=(String) customProperties.get("Schl\u00fcssel2"); + assertEquals("Schl\u00fcssel2","Wert2",a2); Integer a3=(Integer) customProperties.get("Sample Integer"); assertEquals("Sample Number",new Integer(12345),a3); Boolean a4=(Boolean) customProperties.get("Sample Boolean"); @@ -431,7 +431,7 @@ public void testTwo(){ String a1=(String) customProperties.get(k1); assertEquals("Key1",p1,a1); String a2=(String) customProperties.get(k2); - assertEquals("Schl�ssel2",p2,a2); + assertEquals("Schl\u00fcssel2",p2,a2); Integer a3=(Integer) customProperties.get("Sample Number"); assertEquals("Sample Number",new Integer(12345),a3); Boolean a4=(Boolean) customProperties.get("Sample Boolean"); @@ -450,7 +450,7 @@ public void testTwo(){ */ public String strangize(String s){ StringBuffer sb=new StringBuffer(); - String[] umlaute= {"�","�","�","�","$","�","�","�","�","@","�","&"}; + String[] umlaute= {"\u00e4","\u00fc","\u00f6","\u00dc","$","\u00d6","\u00dc","\u00c9","\u00d6","@","\u00e7","&"}; char j=0; Random rand=new Random(); for (int i=0;i<5;i++){ @@ -544,7 +544,7 @@ public void testThree(){ String a1=(String) customProperties.get(k1); assertEquals("Key1",p1,a1); String a2=(String) customProperties.get(k2); - assertEquals("Schl�ssel2",p2,a2); + assertEquals("Schl\u00fcssel2",p2,a2); Integer a3=(Integer) customProperties.get("Sample Number"); assertEquals("Sample Number",new Integer(12345),a3); Boolean a4=(Boolean) customProperties.get("Sample Boolean"); @@ -577,7 +577,7 @@ public void testThree(){ public String strangizeU(String s){ StringBuffer sb=new StringBuffer(); - String[] umlaute= {"�","�","�","�","$","�","�","�","�","@","�","&"}; + String[] umlaute= {"\u00e4","\u00fc","\u00f6","\u00dc","$","\u00d6","\u00dc","\u00c9","\u00d6","@","\u00e7","&"}; char j=0; Random rand=new Random(); for (int i=0;i<5;i++){ @@ -592,7 +592,7 @@ public void testThree(){ sb.append(umlaute[rand.nextInt(umlaute.length)]); sb.append("<"); } - sb.append("���\uD840\uDC00"); + sb.append("\u00e4\u00f6\u00fc\uD840\uDC00"); return sb.toString(); } /** @@ -668,7 +668,7 @@ public void testThree(){ String a1=(String) customProperties.get(k1); assertEquals("Key1",p1,a1); String a2=(String) customProperties.get(k2); - assertEquals("Schl�ssel2",p2,a2); + assertEquals("Schl\u00fcssel2",p2,a2); Integer a3=(Integer) customProperties.get("Sample Number"); assertEquals("Sample Number",new Integer(12345),a3); Boolean a4=(Boolean) customProperties.get("Sample Boolean"); diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java index 8d22157a98..375cc904ef 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java @@ -120,7 +120,6 @@ public class TestWriteWellKnown extends TestCase for (int i = 0; i < docs.length; i++) { final File doc = docs[i]; - System.out.println("Reading file " + doc); /* Read a test document doc into a POI filesystem. */ final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(doc)); @@ -351,10 +350,10 @@ public class TestWriteWellKnown extends TestCase CustomProperties customProperties = dsi.getCustomProperties(); if (customProperties == null) customProperties = new CustomProperties(); - customProperties.put("Schl\u00fcssel �", "Wert �"); - customProperties.put("Schl\u00fcssel ��", "Wert ��"); - customProperties.put("Schl\u00fcssel ���", "Wert ���"); - customProperties.put("Schl\u00fcssel ����", "Wert ����"); + customProperties.put("Schl\u00fcssel \u00e4", "Wert \u00e4"); + customProperties.put("Schl\u00fcssel \u00e4\u00f6", "Wert \u00e4\u00f6"); + customProperties.put("Schl\u00fcssel \u00e4\u00f6\u00fc", "Wert \u00e4\u00f6\u00fc"); + customProperties.put("Schl\u00fcssel \u00e4\u00f6\u00fc\u00d6", "Wert \u00e4\u00f6\u00fc\u00d6"); customProperties.put("positive_Integer", POSITIVE_INTEGER); customProperties.put("positive_Long", POSITIVE_LONG); customProperties.put("positive_Double", POSITIVE_DOUBLE); @@ -440,10 +439,10 @@ public class TestWriteWellKnown extends TestCase final CustomProperties cps = dsi.getCustomProperties(); assertEquals(customProperties, cps); assertNull(cps.get("No value available")); - assertEquals("Wert �", cps.get("Schl\u00fcssel �")); - assertEquals("Wert ��", cps.get("Schl\u00fcssel ��")); - assertEquals("Wert ���", cps.get("Schl\u00fcssel ���")); - assertEquals("Wert ����", cps.get("Schl\u00fcssel ����")); + assertEquals("Wert \u00e4", cps.get("Schl\u00fcssel \u00e4")); + assertEquals("Wert \u00e4\u00f6", cps.get("Schl\u00fcssel \u00e4\u00f6")); + assertEquals("Wert \u00e4\u00f6\u00fc", cps.get("Schl\u00fcssel \u00e4\u00f6\u00fc")); + assertEquals("Wert \u00e4\u00f6\u00fc\u00d6", cps.get("Schl\u00fcssel \u00e4\u00f6\u00fc\u00d6")); assertEquals(POSITIVE_INTEGER, cps.get("positive_Integer")); assertEquals(POSITIVE_LONG, cps.get("positive_Long")); assertEquals(POSITIVE_DOUBLE, cps.get("positive_Double")); @@ -665,7 +664,7 @@ public class TestWriteWellKnown extends TestCase */ public void testCustomerProperties() { - final String KEY = "Schl\u00fcssel �"; + final String KEY = "Schl\u00fcssel \u00e4"; final String VALUE_1 = "Wert 1"; final String VALUE_2 = "Wert 2"; @@ -705,7 +704,7 @@ public class TestWriteWellKnown extends TestCase { final int ID_1 = 2; final int ID_2 = 3; - final String NAME_1 = "Schl\u00fcssel �"; + final String NAME_1 = "Schl\u00fcssel \u00e4"; final String VALUE_1 = "Wert 1"; final Map dictionary = new HashMap();