Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java Fri Sep 22 21:16:09 2017 @@ -42,8 +42,7 @@ import org.xml.sax.helpers.XMLReaderFact */ public class FromHowTo { public void processFirstSheet(String filename) throws Exception { - OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ); - try { + try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) { XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); @@ -54,14 +53,11 @@ public class FromHowTo { InputSource sheetSource = new InputSource(sheet2); parser.parse(sheetSource); sheet2.close(); - } finally { - pkg.close(); } } public void processAllSheets(String filename) throws Exception { - OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ); - try { + try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) { XSSFReader r = new XSSFReader(pkg); SharedStringsTable sst = r.getSharedStringsTable(); @@ -76,8 +72,6 @@ public class FromHowTo { sheet.close(); System.out.println(""); } - } finally { - pkg.close(); } }
Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java Fri Sep 22 21:16:09 2017 @@ -31,26 +31,24 @@ public class Outlining { } private void collapseRow() throws IOException { - SXSSFWorkbook wb2 = new SXSSFWorkbook(100); - SXSSFSheet sheet2 = wb2.createSheet("new sheet"); + try (SXSSFWorkbook wb2 = new SXSSFWorkbook(100)) { + SXSSFSheet sheet2 = wb2.createSheet("new sheet"); - int rowCount = 20; - for (int i = 0; i < rowCount; i++) { - sheet2.createRow(i); - } + int rowCount = 20; + for (int i = 0; i < rowCount; i++) { + sheet2.createRow(i); + } - sheet2.groupRow(4, 9); - sheet2.groupRow(11, 19); + sheet2.groupRow(4, 9); + sheet2.groupRow(11, 19); - sheet2.setRowGroupCollapsed(4, true); + sheet2.setRowGroupCollapsed(4, true); - FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx"); - try { - wb2.write(fileOut); - } finally { - fileOut.close(); - wb2.dispose(); - wb2.close(); + try (FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx")) { + wb2.write(fileOut); + } finally { + wb2.dispose(); + } } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/AligningCells.java Fri Sep 22 21:16:09 2017 @@ -45,34 +45,33 @@ import org.openxmlformats.schemas.spread public class AligningCells { public static void main(String[] args) throws IOException { - XSSFWorkbook wb = new XSSFWorkbook(); + try (XSSFWorkbook wb = new XSSFWorkbook()) { - XSSFSheet sheet = wb.createSheet(); - XSSFRow row = sheet.createRow(2); - row.setHeightInPoints(30); - for (int i = 0; i < 8; i++) { - //column width is set in units of 1/256th of a character width - sheet.setColumnWidth(i, 256 * 15); + XSSFSheet sheet = wb.createSheet(); + XSSFRow row = sheet.createRow(2); + row.setHeightInPoints(30); + for (int i = 0; i < 8; i++) { + //column width is set in units of 1/256th of a character width + sheet.setColumnWidth(i, 256 * 15); + } + + createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM); + createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM); + createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER); + createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER); + createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY); + createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP); + createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP); + + //center text over B4, C4, D4 + row = sheet.createRow(3); + centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER); + + // Write the output to a file + try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) { + wb.write(fileOut); + } } - - createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM); - createCell(wb, row, 1, HorizontalAlignment.CENTER_SELECTION, VerticalAlignment.BOTTOM); - createCell(wb, row, 2, HorizontalAlignment.FILL, VerticalAlignment.CENTER); - createCell(wb, row, 3, HorizontalAlignment.GENERAL, VerticalAlignment.CENTER); - createCell(wb, row, 4, HorizontalAlignment.JUSTIFY, VerticalAlignment.JUSTIFY); - createCell(wb, row, 5, HorizontalAlignment.LEFT, VerticalAlignment.TOP); - createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP); - - //center text over B4, C4, D4 - row = sheet.createRow(3); - centerAcrossSelection(wb, row, 1, 3, VerticalAlignment.CENTER); - - // Write the output to a file - OutputStream fileOut = new FileOutputStream("xssf-align.xlsx"); - wb.write(fileOut); - fileOut.close(); - - wb.close(); } /** Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/BigGridDemo.java Fri Sep 22 21:16:09 2017 @@ -86,30 +86,29 @@ public class BigGridDemo { // Step 1. Create a template file. Setup sheets and workbook-level objects such as // cell styles, number formats, etc. - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sheet = wb.createSheet("Big Grid"); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = wb.createSheet("Big Grid"); - Map<String, XSSFCellStyle> styles = createStyles(wb); - //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml - String sheetRef = sheet.getPackagePart().getPartName().getName(); - - //save the template - FileOutputStream os = new FileOutputStream("template.xlsx"); - wb.write(os); - os.close(); - - //Step 2. Generate XML file. - File tmp = File.createTempFile("sheet", ".xml"); - Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING); - generate(fw, styles); - fw.close(); - - //Step 3. Substitute the template entry with the generated data - FileOutputStream out = new FileOutputStream("big-grid.xlsx"); - substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out); - out.close(); - - wb.close(); + Map<String, XSSFCellStyle> styles = createStyles(wb); + //name of the zip entry holding sheet data, e.g. /xl/worksheets/sheet1.xml + String sheetRef = sheet.getPackagePart().getPartName().getName(); + + //save the template + FileOutputStream os = new FileOutputStream("template.xlsx"); + wb.write(os); + os.close(); + + //Step 2. Generate XML file. + File tmp = File.createTempFile("sheet", ".xml"); + Writer fw = new OutputStreamWriter(new FileOutputStream(tmp), XML_ENCODING); + generate(fw, styles); + fw.close(); + + //Step 3. Substitute the template entry with the generated data + try (FileOutputStream out = new FileOutputStream("big-grid.xlsx")) { + substitute(new File("template.xlsx"), tmp, sheetRef.substring(1), out); + } + } } /** @@ -194,28 +193,23 @@ public class BigGridDemo { * @param out the stream to write the result to */ private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException { - ZipFile zip = ZipHelper.openZipFile(zipfile); - try { - ZipOutputStream zos = new ZipOutputStream(out); - - Enumeration<? extends ZipEntry> en = zip.entries(); - while (en.hasMoreElements()) { - ZipEntry ze = en.nextElement(); - if(!ze.getName().equals(entry)){ - zos.putNextEntry(new ZipEntry(ze.getName())); - InputStream is = zip.getInputStream(ze); + try (ZipFile zip = ZipHelper.openZipFile(zipfile)) { + try (ZipOutputStream zos = new ZipOutputStream(out)) { + Enumeration<? extends ZipEntry> en = zip.entries(); + while (en.hasMoreElements()) { + ZipEntry ze = en.nextElement(); + if (!ze.getName().equals(entry)) { + zos.putNextEntry(new ZipEntry(ze.getName())); + try (InputStream is = zip.getInputStream(ze)) { + copyStream(is, zos); + } + } + } + zos.putNextEntry(new ZipEntry(entry)); + try (InputStream is = new FileInputStream(tmpfile)) { copyStream(is, zos); - is.close(); } } - zos.putNextEntry(new ZipEntry(entry)); - InputStream is = new FileInputStream(tmpfile); - copyStream(is, zos); - is.close(); - - zos.close(); - } finally { - zip.close(); } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CalendarDemo.java Fri Sep 22 21:16:09 2017 @@ -55,80 +55,80 @@ public class CalendarDemo { int year = calendar.get(Calendar.YEAR); - XSSFWorkbook wb = new XSSFWorkbook(); - Map<String, XSSFCellStyle> styles = createStyles(wb); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + Map<String, XSSFCellStyle> 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 - XSSFSheet sheet = wb.createSheet(months[month]); - - //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(80); - XSSFCell 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 - XSSFRow monthRow = sheet.createRow(1); - for (int i = 0; i < days.length; i++) { - //for compatibility with HSSF we have to set column width 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)); - XSSFCell monthCell = monthRow.createCell(i*2); - monthCell.setCellValue(days[i]); - monthCell.setCellStyle(styles.get("month")); - } + 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 + XSSFSheet sheet = wb.createSheet(months[month]); + + //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(80); + XSSFCell titleCell = headerRow.createCell(0); + titleCell.setCellValue(months[month] + " " + year); + titleCell.setCellStyle(styles.get("title")); + sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1")); - int cnt = 1, day=1; - int rownum = 2; - for (int j = 0; j < 6; j++) { - XSSFRow row = sheet.createRow(rownum++); - row.setHeightInPoints(100); + //header with month titles + XSSFRow monthRow = sheet.createRow(1); for (int i = 0; i < days.length; i++) { - XSSFCell dayCell_1 = row.createCell(i*2); - XSSFCell dayCell_2 = row.createCell(i*2 + 1); + //for compatibility with HSSF we have to set column width 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)); + XSSFCell monthCell = monthRow.createCell(i * 2); + monthCell.setCellValue(days[i]); + monthCell.setCellStyle(styles.get("month")); + } - 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")); + int cnt = 1, day = 1; + int rownum = 2; + for (int j = 0; j < 6; j++) { + XSSFRow row = sheet.createRow(rownum++); + row.setHeightInPoints(100); + for (int i = 0; i < days.length; i++) { + XSSFCell dayCell_1 = row.createCell(i * 2); + XSSFCell 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("workday_left")); - dayCell_2.setCellStyle(styles.get("workday_right")); + dayCell_1.setCellStyle(styles.get("grey_left")); + dayCell_2.setCellStyle(styles.get("grey_right")); } - } else { - dayCell_1.setCellStyle(styles.get("grey_left")); - dayCell_2.setCellStyle(styles.get("grey_right")); + cnt++; } - cnt++; + if (calendar.get(Calendar.MONTH) > month) break; } - if(calendar.get(Calendar.MONTH) > month) break; } - } - // Write the output to a file - FileOutputStream out = new FileOutputStream("calendar-"+year+".xlsx"); - wb.write(out); - out.close(); - - wb.close(); + // Write the output to a file + try (FileOutputStream out = new FileOutputStream("calendar-" + year + ".xlsx")) { + wb.write(out); + } + + } } /** Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CellComments.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CellComments.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CellComments.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CellComments.java Fri Sep 22 21:16:09 2017 @@ -44,47 +44,45 @@ import org.apache.poi.xssf.usermodel.XSS */ public class CellComments { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); + try (Workbook wb = new XSSFWorkbook()) { - CreationHelper factory = wb.getCreationHelper(); + CreationHelper factory = wb.getCreationHelper(); - Sheet sheet = wb.createSheet(); + Sheet sheet = wb.createSheet(); - Cell cell1 = sheet.createRow(3).createCell(5); - cell1.setCellValue("F4"); + Cell cell1 = sheet.createRow(3).createCell(5); + cell1.setCellValue("F4"); - Drawing<?> drawing = sheet.createDrawingPatriarch(); - - ClientAnchor anchor = factory.createClientAnchor(); - - Comment comment1 = drawing.createCellComment(anchor); - RichTextString str1 = factory.createRichTextString("Hello, World!"); - comment1.setString(str1); - comment1.setAuthor("Apache POI"); - cell1.setCellComment(comment1); - - Cell cell2 = sheet.createRow(2).createCell(2); - cell2.setCellValue("C3"); - - Comment comment2 = drawing.createCellComment(anchor); - RichTextString str2 = factory.createRichTextString("XSSF can set cell comments"); - //apply custom font to the text in the comment - Font font = wb.createFont(); - font.setFontName("Arial"); - font.setFontHeightInPoints((short)14); - font.setBold(true); - font.setColor(IndexedColors.RED.getIndex()); - str2.applyFont(font); - - comment2.setString(str2); - comment2.setAuthor("Apache POI"); - comment2.setAddress(new CellAddress("C3")); - - String fname = "comments.xlsx"; - FileOutputStream out = new FileOutputStream(fname); - wb.write(out); - out.close(); - - wb.close(); + Drawing<?> drawing = sheet.createDrawingPatriarch(); + + ClientAnchor anchor = factory.createClientAnchor(); + + Comment comment1 = drawing.createCellComment(anchor); + RichTextString str1 = factory.createRichTextString("Hello, World!"); + comment1.setString(str1); + comment1.setAuthor("Apache POI"); + cell1.setCellComment(comment1); + + Cell cell2 = sheet.createRow(2).createCell(2); + cell2.setCellValue("C3"); + + Comment comment2 = drawing.createCellComment(anchor); + RichTextString str2 = factory.createRichTextString("XSSF can set cell comments"); + //apply custom font to the text in the comment + Font font = wb.createFont(); + font.setFontName("Arial"); + font.setFontHeightInPoints((short) 14); + font.setBold(true); + font.setColor(IndexedColors.RED.getIndex()); + str2.applyFont(font); + + comment2.setString(str2); + comment2.setAuthor("Apache POI"); + comment2.setAddress(new CellAddress("C3")); + + try (FileOutputStream out = new FileOutputStream("comments.xlsx")) { + wb.write(out); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java Fri Sep 22 21:16:09 2017 @@ -36,54 +36,53 @@ import org.apache.poi.xssf.usermodel.XSS */ public class CreateCell { - - public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - CreationHelper creationHelper = wb.getCreationHelper(); - Sheet sheet = wb.createSheet("new sheet"); - - // Create a row and put some cells in it. Rows are 0 based. - Row row = sheet.createRow((short)0); - // Create a cell and put a value in it. - Cell cell = row.createCell((short)0); - cell.setCellValue(1); - - //numeric value - row.createCell(1).setCellValue(1.2); - - //plain string value - row.createCell(2).setCellValue("This is a string cell"); - - //rich text string - RichTextString str = creationHelper.createRichTextString("Apache"); - Font font = wb.createFont(); - font.setItalic(true); - font.setUnderline(Font.U_SINGLE); - str.applyFont(font); - row.createCell(3).setCellValue(str); - - //boolean value - row.createCell(4).setCellValue(true); - - //formula - row.createCell(5).setCellFormula("SUM(A1:B1)"); - - //date - CellStyle style = wb.createCellStyle(); - style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm")); - cell = row.createCell(6); - cell.setCellValue(new Date()); - cell.setCellStyle(style); - - //hyperlink - row.createCell(7).setCellFormula("SUM(A1:B1)"); - cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")"); - - - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); - } + public static void main(String[] args) throws IOException { + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + CreationHelper creationHelper = wb.getCreationHelper(); + Sheet sheet = wb.createSheet("new sheet"); + + // Create a row and put some cells in it. Rows are 0 based. + Row row = sheet.createRow((short) 0); + // Create a cell and put a value in it. + Cell cell = row.createCell((short) 0); + cell.setCellValue(1); + + //numeric value + row.createCell(1).setCellValue(1.2); + + //plain string value + row.createCell(2).setCellValue("This is a string cell"); + + //rich text string + RichTextString str = creationHelper.createRichTextString("Apache"); + Font font = wb.createFont(); + font.setItalic(true); + font.setUnderline(Font.U_SINGLE); + str.applyFont(font); + row.createCell(3).setCellValue(str); + + //boolean value + row.createCell(4).setCellValue(true); + + //formula + row.createCell(5).setCellFormula("SUM(A1:B1)"); + + //date + CellStyle style = wb.createCellStyle(); + style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm")); + cell = row.createCell(6); + cell.setCellValue(new Date()); + cell.setCellStyle(style); + + //hyperlink + row.createCell(7).setCellFormula("SUM(A1:B1)"); + cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")"); + + + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx")) { + wb.write(fileOut); + } + } + } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable.java Fri Sep 22 21:16:09 2017 @@ -34,31 +34,31 @@ import org.apache.poi.xssf.usermodel.XSS public class CreatePivotTable { public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sheet = wb.createSheet(); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = wb.createSheet(); - //Create some data to build the pivot table on - setCellData(sheet); + //Create some data to build the pivot table on + setCellData(sheet); - AreaReference source = new AreaReference("A1:D4", SpreadsheetVersion.EXCEL2007); - CellReference position = new CellReference("H5"); - // Create a pivot table on this sheet, with H5 as the top-left cell.. - // The pivot table's data source is on the same sheet in A1:D4 - XSSFPivotTable pivotTable = sheet.createPivotTable(source, position); - //Configure the pivot table - //Use first column as row label - pivotTable.addRowLabel(0); - //Sum up the second column - pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); - //Set the third column as filter - pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2); - //Add filter on forth column - pivotTable.addReportFilter(3); + AreaReference source = new AreaReference("A1:D4", SpreadsheetVersion.EXCEL2007); + CellReference position = new CellReference("H5"); + // Create a pivot table on this sheet, with H5 as the top-left cell.. + // The pivot table's data source is on the same sheet in A1:D4 + XSSFPivotTable pivotTable = sheet.createPivotTable(source, position); + //Configure the pivot table + //Use first column as row label + pivotTable.addRowLabel(0); + //Sum up the second column + pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); + //Set the third column as filter + pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2); + //Add filter on forth column + pivotTable.addReportFilter(3); - FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + try (FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx")) { + wb.write(fileOut); + } + } } public static void setCellData(XSSFSheet sheet){ Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable2.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable2.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable2.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreatePivotTable2.java Fri Sep 22 21:16:09 2017 @@ -38,32 +38,32 @@ import org.apache.poi.xssf.usermodel.XSS public class CreatePivotTable2 { public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet sheet = wb.createSheet(); + try (XSSFWorkbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = wb.createSheet(); - //Create some data to build the pivot table on - setCellData(sheet); + //Create some data to build the pivot table on + setCellData(sheet); - AreaReference source = new AreaReference("A1:E7", SpreadsheetVersion.EXCEL2007); - CellReference position = new CellReference("H1"); - // Create a pivot table on this sheet, with H1 as the top-left cell.. - // The pivot table's data source is on the same sheet in A1:E7 - XSSFPivotTable pivotTable = sheet.createPivotTable(source, position); - //Configure the pivot table - //Use first column as row label - pivotTable.addRowLabel(0); - //Sum up the second column with column title and data format - pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1, "Values", "#,##0.00"); - //Use third column (month) as columns (side by side) - pivotTable.addColLabel(3, "DD.MM.YYYY"); - - //Add filter on forth column - pivotTable.addReportFilter(4); - - FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable2.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + AreaReference source = new AreaReference("A1:E7", SpreadsheetVersion.EXCEL2007); + CellReference position = new CellReference("H1"); + // Create a pivot table on this sheet, with H1 as the top-left cell.. + // The pivot table's data source is on the same sheet in A1:E7 + XSSFPivotTable pivotTable = sheet.createPivotTable(source, position); + //Configure the pivot table + //Use first column as row label + pivotTable.addRowLabel(0); + //Sum up the second column with column title and data format + pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1, "Values", "#,##0.00"); + //Use third column (month) as columns (side by side) + pivotTable.addColLabel(3, "DD.MM.YYYY"); + + //Add filter on forth column + pivotTable.addReportFilter(4); + + try (FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable2.xlsx")) { + wb.write(fileOut); + } + } } public static void setCellData(XSSFSheet sheet){ Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java Fri Sep 22 21:16:09 2017 @@ -36,58 +36,58 @@ public class CreateTable { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) wb.createSheet(); - - // Create - XSSFTable table = sheet.createTable(); - table.setName("Test"); - table.setDisplayName("Test_Table"); - - // For now, create the initial style in a low-level way - table.getCTTable().addNewTableStyleInfo(); - table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2"); - - // Style the table - XSSFTableStyleInfo style = (XSSFTableStyleInfo)table.getStyle(); - style.setName("TableStyleMedium2"); - style.setShowColumnStripes(false); - style.setShowRowStripes(true); - style.setFirstColumn(false); - style.setLastColumn(false); - style.setShowRowStripes(true); - style.setShowColumnStripes(true); - - // Set the values for the table - XSSFRow row; - XSSFCell cell; - for(int i=0; i<3; i++) { - // Create row - row = sheet.createRow(i); - for(int j=0; j<3; j++) { - // Create cell - cell = row.createCell(j); - if(i == 0) { - cell.setCellValue("Column"+(j+1)); - } else { - cell.setCellValue((i+1)*(j+1)); + try (Workbook wb = new XSSFWorkbook()) { + XSSFSheet sheet = (XSSFSheet) wb.createSheet(); + + // Create + XSSFTable table = sheet.createTable(); + table.setName("Test"); + table.setDisplayName("Test_Table"); + + // For now, create the initial style in a low-level way + table.getCTTable().addNewTableStyleInfo(); + table.getCTTable().getTableStyleInfo().setName("TableStyleMedium2"); + + // Style the table + XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle(); + style.setName("TableStyleMedium2"); + style.setShowColumnStripes(false); + style.setShowRowStripes(true); + style.setFirstColumn(false); + style.setLastColumn(false); + style.setShowRowStripes(true); + style.setShowColumnStripes(true); + + // Set the values for the table + XSSFRow row; + XSSFCell cell; + for (int i = 0; i < 3; i++) { + // Create row + row = sheet.createRow(i); + for (int j = 0; j < 3; j++) { + // Create cell + cell = row.createCell(j); + if (i == 0) { + cell.setCellValue("Column" + (j + 1)); + } else { + cell.setCellValue((i + 1) * (j + 1)); + } } } + // Create the columns + table.addColumn(); + table.addColumn(); + table.addColumn(); + + // Set which area the table should be placed in + AreaReference reference = wb.getCreationHelper().createAreaReference( + new CellReference(0, 0), new CellReference(2, 2)); + table.setCellReferences(reference); + + // Save + try (FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx")) { + wb.write(fileOut); + } } - // Create the columns - table.addColumn(); - table.addColumn(); - table.addColumn(); - - // Set which area the table should be placed in - AreaReference reference = wb.getCreationHelper().createAreaReference( - new CellReference(0, 0), new CellReference(2, 2)); - table.setCellReferences(reference); - - // Save - FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java Fri Sep 22 21:16:09 2017 @@ -35,34 +35,33 @@ public class CreateUserDefinedDataFormat public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("format sheet"); - CellStyle style; - DataFormat format = wb.createDataFormat(); - Row row; - Cell cell; - short rowNum = 0; - short colNum = 0; - - row = sheet.createRow(rowNum); - cell = row.createCell(colNum); - cell.setCellValue(11111.25); - style = wb.createCellStyle(); - style.setDataFormat(format.getFormat("0.0")); - cell.setCellStyle(style); - - row = sheet.createRow(++rowNum); - cell = row.createCell(colNum); - cell.setCellValue(11111.25); - style = wb.createCellStyle(); - style.setDataFormat(format.getFormat("#,##0.0000")); - cell.setCellStyle(style); - - FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx"); - wb.write(fileOut); - fileOut.close(); - - wb.close(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("format sheet"); + CellStyle style; + DataFormat format = wb.createDataFormat(); + Row row; + Cell cell; + short rowNum = 0; + short colNum = 0; + + row = sheet.createRow(rowNum); + cell = row.createCell(colNum); + cell.setCellValue(11111.25); + style = wb.createCellStyle(); + style.setDataFormat(format.getFormat("0.0")); + cell.setCellStyle(style); + + row = sheet.createRow(++rowNum); + cell = row.createCell(colNum); + cell.setCellValue(11111.25); + style = wb.createCellStyle(); + style.setDataFormat(format.getFormat("#,##0.0000")); + cell.setCellStyle(style); + + try (FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CustomXMLMapping.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CustomXMLMapping.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CustomXMLMapping.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CustomXMLMapping.java Fri Sep 22 21:16:09 2017 @@ -29,18 +29,16 @@ import java.io.ByteArrayOutputStream; public class CustomXMLMapping { public static void main(String[] args) throws Exception { - OPCPackage pkg = OPCPackage.open(args[0]); - XSSFWorkbook wb = new XSSFWorkbook(pkg); + try (OPCPackage pkg = OPCPackage.open(args[0]); + XSSFWorkbook wb = new XSSFWorkbook(pkg)) { + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); - for (XSSFMap map : wb.getCustomXMLMappings()) { - XSSFExportToXml exporter = new XSSFExportToXml(map); - - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xml = os.toString("UTF-8"); - System.out.println(xml); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xml = os.toString("UTF-8"); + System.out.println(xml); + } } - pkg.close(); - wb.close(); } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java Fri Sep 22 21:16:09 2017 @@ -32,36 +32,36 @@ import org.apache.poi.xwpf.usermodel.XWP */ public class EmbeddedObjects { public static void main(String[] args) throws Exception { - XSSFWorkbook workbook = new XSSFWorkbook(args[0]); - for (PackagePart pPart : workbook.getAllEmbedds()) { - String contentType = pPart.getContentType(); - InputStream is = pPart.getInputStream(); - Closeable document; - if (contentType.equals("application/vnd.ms-excel")) { - // Excel Workbook - either binary or OpenXML - document = new HSSFWorkbook(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { - // Excel Workbook - OpenXML file format - document = new XSSFWorkbook(is); - } else if (contentType.equals("application/msword")) { - // Word Document - binary (OLE2CDF) file format - document = new HWPFDocument(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { - // Word Document - OpenXML file format - document = new XWPFDocument(is); - } else if (contentType.equals("application/vnd.ms-powerpoint")) { - // PowerPoint Document - binary file format - document = new HSLFSlideShow(is); - } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { - // PowerPoint Document - OpenXML file format - document = new XMLSlideShow(is); - } else { - // Any other type of embedded object. - document = is; + try (XSSFWorkbook workbook = new XSSFWorkbook(args[0])) { + for (PackagePart pPart : workbook.getAllEmbedds()) { + String contentType = pPart.getContentType(); + try (InputStream is = pPart.getInputStream()) { + Closeable document; + if (contentType.equals("application/vnd.ms-excel")) { + // Excel Workbook - either binary or OpenXML + document = new HSSFWorkbook(is); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { + // Excel Workbook - OpenXML file format + document = new XSSFWorkbook(is); + } else if (contentType.equals("application/msword")) { + // Word Document - binary (OLE2CDF) file format + document = new HWPFDocument(is); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { + // Word Document - OpenXML file format + document = new XWPFDocument(is); + } else if (contentType.equals("application/vnd.ms-powerpoint")) { + // PowerPoint Document - binary file format + document = new HSLFSlideShow(is); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { + // PowerPoint Document - OpenXML file format + document = new XMLSlideShow(is); + } else { + // Any other type of embedded object. + document = is; + } + document.close(); + } } - document.close(); - is.close(); } - workbook.close(); } } \ No newline at end of file Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java Fri Sep 22 21:16:09 2017 @@ -34,32 +34,32 @@ import org.apache.poi.xssf.usermodel.XSS */ public class FillsAndColors { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("new sheet"); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); - // Create a row and put some cells in it. Rows are 0 based. - Row row = sheet.createRow(1); + // Create a row and put some cells in it. Rows are 0 based. + Row row = sheet.createRow(1); - // Aqua background - CellStyle style = wb.createCellStyle(); - style.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); - style.setFillPattern(FillPatternType.BIG_SPOTS); - Cell cell = row.createCell(1); - cell.setCellValue(new XSSFRichTextString("X")); - cell.setCellStyle(style); + // Aqua background + CellStyle style = wb.createCellStyle(); + style.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); + style.setFillPattern(FillPatternType.BIG_SPOTS); + Cell cell = row.createCell(1); + cell.setCellValue(new XSSFRichTextString("X")); + cell.setCellStyle(style); - // Orange "foreground", foreground being the fill foreground not the font color. - style = wb.createCellStyle(); - style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - cell = row.createCell(2); - cell.setCellValue(new XSSFRichTextString("X")); - cell.setCellStyle(style); + // Orange "foreground", foreground being the fill foreground not the font color. + style = wb.createCellStyle(); + style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + cell = row.createCell(2); + cell.setCellValue(new XSSFRichTextString("X")); + cell.setCellStyle(style); - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java Fri Sep 22 21:16:09 2017 @@ -27,20 +27,20 @@ import org.apache.poi.xssf.usermodel.XSS public class FitSheetToOnePage { public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("format sheet"); - PrintSetup ps = sheet.getPrintSetup(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("format sheet"); + PrintSetup ps = sheet.getPrintSetup(); - sheet.setAutobreaks(true); + sheet.setAutobreaks(true); - ps.setFitHeight((short) 1); - ps.setFitWidth((short) 1); + ps.setFitHeight((short) 1); + ps.setFitWidth((short) 1); - // Create various cells and rows for spreadsheet. + // Create various cells and rows for spreadsheet. - FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + try (FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java Fri Sep 22 21:16:09 2017 @@ -28,58 +28,57 @@ import org.apache.poi.xssf.usermodel.XSS public class HeadersAndFooters { - public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("first-header - format sheet"); - sheet.createRow(0).createCell(0).setCellValue(123); - - //set page numbers in the footer - Footer footer = sheet.getFooter(); - //&P == current page number - //&N == page numbers - footer.setRight("Page &P of &N"); - - - Header firstHeader=((XSSFSheet)sheet).getFirstHeader(); - //&F == workbook file name - firstHeader.setLeft("&F ......... first header"); - - for(int i=0;i<100;i=i+10){ - sheet.createRow(i).createCell(0).setCellValue(123); - } - - - XSSFSheet sheet2 = (XSSFSheet)wb.createSheet("odd header-even footer"); - Header oddHeader=sheet2.getOddHeader(); - //&B == bold - //&E == double underline - //&D == date - oddHeader.setCenter("&B &E oddHeader &D "); - - Footer evenFooter=sheet2.getEvenFooter(); - evenFooter.setRight("even footer &P"); - sheet2.createRow(10).createCell(0).setCellValue("Second sheet with an oddHeader and an evenFooter"); - - for(int i=0;i<200;i=i+10){ - sheet2.createRow(i).createCell(0).setCellValue(123); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("first-header - format sheet"); + sheet.createRow(0).createCell(0).setCellValue(123); + + //set page numbers in the footer + Footer footer = sheet.getFooter(); + //&P == current page number + //&N == page numbers + footer.setRight("Page &P of &N"); + + + Header firstHeader = ((XSSFSheet) sheet).getFirstHeader(); + //&F == workbook file name + firstHeader.setLeft("&F ......... first header"); + + for (int i = 0; i < 100; i = i + 10) { + sheet.createRow(i).createCell(0).setCellValue(123); + } + + + XSSFSheet sheet2 = (XSSFSheet) wb.createSheet("odd header-even footer"); + Header oddHeader = sheet2.getOddHeader(); + //&B == bold + //&E == double underline + //&D == date + oddHeader.setCenter("&B &E oddHeader &D "); + + Footer evenFooter = sheet2.getEvenFooter(); + evenFooter.setRight("even footer &P"); + sheet2.createRow(10).createCell(0).setCellValue("Second sheet with an oddHeader and an evenFooter"); + + for (int i = 0; i < 200; i = i + 10) { + sheet2.createRow(i).createCell(0).setCellValue(123); + } + + XSSFSheet sheet3 = (XSSFSheet) wb.createSheet("odd header- odd footer"); + sheet3.createRow(10).createCell(0).setCellValue("Third sheet with oddHeader and oddFooter"); + Header oddH = sheet3.getOddHeader(); + //&C == centered + oddH.setCenter("centered oddHeader"); + oddH.setLeft("left "); + oddH.setRight("right "); + + Footer oddF = sheet3.getOddFooter(); + oddF.setLeft("Page &P"); + oddF.setRight("Pages &N "); + + try (FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx")) { + wb.write(fileOut); + } } - - XSSFSheet sheet3 = (XSSFSheet)wb.createSheet("odd header- odd footer"); - sheet3.createRow(10).createCell(0).setCellValue("Third sheet with oddHeader and oddFooter"); - Header oddH=sheet3.getOddHeader(); - //&C == centered - oddH.setCenter("centered oddHeader"); - oddH.setLeft("left "); - oddH.setRight("right "); - - Footer oddF=sheet3.getOddFooter(); - oddF.setLeft("Page &P"); - oddF.setRight("Pages &N "); - - FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HyperlinkExample.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HyperlinkExample.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HyperlinkExample.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HyperlinkExample.java Fri Sep 22 21:16:09 2017 @@ -35,64 +35,62 @@ import org.apache.poi.xssf.usermodel.XSS */ public class HyperlinkExample { - public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - CreationHelper createHelper = wb.getCreationHelper(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + CreationHelper createHelper = wb.getCreationHelper(); - //cell style for hyperlinks - //by default hyperlinks are blue and underlined - CellStyle hlink_style = wb.createCellStyle(); - Font hlink_font = wb.createFont(); - hlink_font.setUnderline(Font.U_SINGLE); - hlink_font.setColor(IndexedColors.BLUE.getIndex()); - hlink_style.setFont(hlink_font); - - Cell cell; - Sheet sheet = wb.createSheet("Hyperlinks"); - //URL - cell = sheet.createRow(0).createCell(0); - cell.setCellValue("URL Link"); - - Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL); - link.setAddress("http://poi.apache.org/"); - cell.setHyperlink(link); - cell.setCellStyle(hlink_style); - - //link to a file in the current directory - cell = sheet.createRow(1).createCell(0); - cell.setCellValue("File Link"); - link = createHelper.createHyperlink(HyperlinkType.FILE); - link.setAddress("link1.xls"); - cell.setHyperlink(link); - cell.setCellStyle(hlink_style); - - //e-mail link - cell = sheet.createRow(2).createCell(0); - cell.setCellValue("Email Link"); - link = createHelper.createHyperlink(HyperlinkType.EMAIL); - //note, if subject contains white spaces, make sure they are url-encoded - link.setAddress("mailto:[email protected]?subject=Hyperlinks"); - cell.setHyperlink(link); - cell.setCellStyle(hlink_style); - - //link to a place in this workbook - - //create a target sheet and cell - Sheet sheet2 = wb.createSheet("Target Sheet"); - sheet2.createRow(0).createCell(0).setCellValue("Target Cell"); - - cell = sheet.createRow(3).createCell(0); - cell.setCellValue("Worksheet Link"); - Hyperlink link2 = createHelper.createHyperlink(HyperlinkType.DOCUMENT); - link2.setAddress("'Target Sheet'!A1"); - cell.setHyperlink(link2); - cell.setCellStyle(hlink_style); - - FileOutputStream out = new FileOutputStream("hyperinks.xlsx"); - wb.write(out); - out.close(); - - wb.close(); + //cell style for hyperlinks + //by default hyperlinks are blue and underlined + CellStyle hlink_style = wb.createCellStyle(); + Font hlink_font = wb.createFont(); + hlink_font.setUnderline(Font.U_SINGLE); + hlink_font.setColor(IndexedColors.BLUE.getIndex()); + hlink_style.setFont(hlink_font); + + Cell cell; + Sheet sheet = wb.createSheet("Hyperlinks"); + //URL + cell = sheet.createRow(0).createCell(0); + cell.setCellValue("URL Link"); + + Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL); + link.setAddress("http://poi.apache.org/"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a file in the current directory + cell = sheet.createRow(1).createCell(0); + cell.setCellValue("File Link"); + link = createHelper.createHyperlink(HyperlinkType.FILE); + link.setAddress("link1.xls"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //e-mail link + cell = sheet.createRow(2).createCell(0); + cell.setCellValue("Email Link"); + link = createHelper.createHyperlink(HyperlinkType.EMAIL); + //note, if subject contains white spaces, make sure they are url-encoded + link.setAddress("mailto:[email protected]?subject=Hyperlinks"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a place in this workbook + + //create a target sheet and cell + Sheet sheet2 = wb.createSheet("Target Sheet"); + sheet2.createRow(0).createCell(0).setCellValue("Target Cell"); + + cell = sheet.createRow(3).createCell(0); + cell.setCellValue("Worksheet Link"); + Hyperlink link2 = createHelper.createHyperlink(HyperlinkType.DOCUMENT); + link2.setAddress("'Target Sheet'!A1"); + cell.setHyperlink(link2); + cell.setCellStyle(hlink_style); + + try (FileOutputStream out = new FileOutputStream("hyperinks.xlsx")) { + wb.write(out); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java Fri Sep 22 21:16:09 2017 @@ -32,17 +32,17 @@ import org.apache.poi.xssf.usermodel.XSS public class IterateCells { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(new FileInputStream(args[0])); - for (int i = 0; i < wb.getNumberOfSheets(); i++) { - Sheet sheet = wb.getSheetAt(i); - System.out.println(wb.getSheetName(i)); - for (Row row : sheet) { - System.out.println("rownum: " + row.getRowNum()); - for (Cell cell : row) { - System.out.println(cell); + try (Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]))) { + for (int i = 0; i < wb.getNumberOfSheets(); i++) { + Sheet sheet = wb.getSheetAt(i); + System.out.println(wb.getSheetName(i)); + for (Row row : sheet) { + System.out.println("rownum: " + row.getRowNum()); + for (Cell cell : row) { + System.out.println(cell); + } } } } - wb.close(); } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java Fri Sep 22 21:16:09 2017 @@ -44,50 +44,50 @@ import org.apache.poi.xssf.usermodel.XSS public class LineChart { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); - Sheet sheet = wb.createSheet("linechart"); - final int NUM_OF_ROWS = 3; - final int NUM_OF_COLUMNS = 10; - - // Create a row and put some cells in it. Rows are 0 based. - Row row; - Cell cell; - for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { - row = sheet.createRow((short) rowIndex); - for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { - cell = row.createCell((short) colIndex); - cell.setCellValue(colIndex * (rowIndex + 1)); + try (Workbook wb = new XSSFWorkbook()) { + Sheet sheet = wb.createSheet("linechart"); + final int NUM_OF_ROWS = 3; + final int NUM_OF_COLUMNS = 10; + + // Create a row and put some cells in it. Rows are 0 based. + Row row; + Cell cell; + for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { + row = sheet.createRow((short) rowIndex); + for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { + cell = row.createCell((short) colIndex); + cell.setCellValue(colIndex * (rowIndex + 1)); + } } - } - Drawing<?> drawing = sheet.createDrawingPatriarch(); - ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); + Drawing<?> drawing = sheet.createDrawingPatriarch(); + ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); - Chart chart = drawing.createChart(anchor); - ChartLegend legend = chart.getOrCreateLegend(); - legend.setPosition(LegendPosition.TOP_RIGHT); + Chart chart = drawing.createChart(anchor); + ChartLegend legend = chart.getOrCreateLegend(); + legend.setPosition(LegendPosition.TOP_RIGHT); - LineChartData data = chart.getChartDataFactory().createLineChartData(); + LineChartData data = chart.getChartDataFactory().createLineChartData(); - // Use a category axis for the bottom axis. - ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); - ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); - leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); + // Use a category axis for the bottom axis. + ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); + ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); + leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); - ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); - ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); - ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); - data.addSeries(xs, ys1); - data.addSeries(xs, ys2); + data.addSeries(xs, ys1); + data.addSeries(xs, ys2); - chart.plot(data, bottomAxis, leftAxis); + chart.plot(data, bottomAxis, leftAxis); - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java Fri Sep 22 21:16:09 2017 @@ -33,19 +33,19 @@ import org.apache.poi.xssf.usermodel.XSS */ public class MergingCells { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("new sheet"); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); - Row row = sheet.createRow((short) 1); - Cell cell = row.createCell((short) 1); - cell.setCellValue(new XSSFRichTextString("This is a test of merging")); + Row row = sheet.createRow((short) 1); + Cell cell = row.createCell((short) 1); + cell.setCellValue(new XSSFRichTextString("This is a test of merging")); - sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); + sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java Fri Sep 22 21:16:09 2017 @@ -32,28 +32,28 @@ import org.apache.poi.xssf.usermodel.XSS public class NewLinesInCells { public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet(); - Row row = sheet.createRow(2); - Cell cell = row.createCell(2); - cell.setCellValue("Use \n with word wrap on to create a new line"); - - //to enable newlines you need set a cell styles with wrap=true - CellStyle cs = wb.createCellStyle(); - cs.setWrapText(true); - cell.setCellStyle(cs); - - //increase row height to accomodate two lines of text - row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); - - //adjust column width to fit the content - sheet.autoSizeColumn(2); - - FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + Row row = sheet.createRow(2); + Cell cell = row.createCell(2); + cell.setCellValue("Use \n with word wrap on to create a new line"); + + //to enable newlines you need set a cell styles with wrap=true + CellStyle cs = wb.createCellStyle(); + cs.setWrapText(true); + cell.setCellStyle(cs); + + //increase row height to accomodate two lines of text + row.setHeightInPoints((2 * sheet.getDefaultRowHeightInPoints())); + + //adjust column width to fit the content + sheet.autoSizeColumn(2); + + try (FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java Fri Sep 22 21:16:09 2017 @@ -35,50 +35,44 @@ public class Outlining { private void groupRowColumn() throws IOException { - Workbook wb = new XSSFWorkbook(); - Sheet sheet1 = wb.createSheet("new sheet"); + try (Workbook wb = new XSSFWorkbook()) { + Sheet sheet1 = wb.createSheet("new sheet"); - sheet1.groupRow( 5, 14 ); - sheet1.groupRow( 7, 14 ); - sheet1.groupRow( 16, 19 ); - - sheet1.groupColumn( (short)4, (short)7 ); - sheet1.groupColumn( (short)9, (short)12 ); - sheet1.groupColumn( (short)10, (short)11 ); - - OutputStream fileOut = new FileOutputStream("outlining.xlsx"); - try { - wb.write(fileOut); - } finally { - fileOut.close(); - wb.close(); + sheet1.groupRow(5, 14); + sheet1.groupRow(7, 14); + sheet1.groupRow(16, 19); + + sheet1.groupColumn((short) 4, (short) 7); + sheet1.groupColumn((short) 9, (short) 12); + sheet1.groupColumn((short) 10, (short) 11); + + try (OutputStream fileOut = new FileOutputStream("outlining.xlsx")) { + wb.write(fileOut); + } } } private void collapseExpandRowColumn() throws IOException { - Workbook wb2 = new XSSFWorkbook(); - Sheet sheet2 = wb2.createSheet("new sheet"); - sheet2.groupRow( 5, 14 ); - sheet2.groupRow( 7, 14 ); - sheet2.groupRow( 16, 19 ); - - sheet2.groupColumn( (short)4, (short)7 ); - sheet2.groupColumn( (short)9, (short)12 ); - sheet2.groupColumn( (short)10, (short)11 ); - - - sheet2.setRowGroupCollapsed( 7, true ); - //sheet1.setRowGroupCollapsed(7,false); - - sheet2.setColumnGroupCollapsed( (short)4, true ); - sheet2.setColumnGroupCollapsed( (short)4, false ); - - OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx"); - try { - wb2.write(fileOut); - } finally { - fileOut.close(); - wb2.close(); + try (Workbook wb2 = new XSSFWorkbook()) { + Sheet sheet2 = wb2.createSheet("new sheet"); + sheet2.groupRow(5, 14); + sheet2.groupRow(7, 14); + sheet2.groupRow(16, 19); + + sheet2.groupColumn((short) 4, (short) 7); + sheet2.groupColumn((short) 9, (short) 12); + sheet2.groupColumn((short) 10, (short) 11); + + + sheet2.setRowGroupCollapsed(7, true); + //sheet1.setRowGroupCollapsed(7,false); + + sheet2.setColumnGroupCollapsed((short) 4, true); + sheet2.setColumnGroupCollapsed((short) 4, false); + + try (OutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx")) { + wb2.write(fileOut); + } } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java Fri Sep 22 21:16:09 2017 @@ -46,49 +46,49 @@ import org.apache.poi.xssf.usermodel.XSS public class ScatterChart { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); - Sheet sheet = wb.createSheet("Sheet 1"); - final int NUM_OF_ROWS = 3; - final int NUM_OF_COLUMNS = 10; - - // Create a row and put some cells in it. Rows are 0 based. - Row row; - Cell cell; - for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { - row = sheet.createRow((short) rowIndex); - for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { - cell = row.createCell((short) colIndex); - cell.setCellValue(colIndex * (rowIndex + 1)); + try (Workbook wb = new XSSFWorkbook()) { + Sheet sheet = wb.createSheet("Sheet 1"); + final int NUM_OF_ROWS = 3; + final int NUM_OF_COLUMNS = 10; + + // Create a row and put some cells in it. Rows are 0 based. + Row row; + Cell cell; + for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) { + row = sheet.createRow((short) rowIndex); + for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) { + cell = row.createCell((short) colIndex); + cell.setCellValue(colIndex * (rowIndex + 1)); + } } - } - Drawing<?> drawing = sheet.createDrawingPatriarch(); - ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); + Drawing<?> drawing = sheet.createDrawingPatriarch(); + ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); - Chart chart = drawing.createChart(anchor); - ChartLegend legend = chart.getOrCreateLegend(); - legend.setPosition(LegendPosition.TOP_RIGHT); + Chart chart = drawing.createChart(anchor); + ChartLegend legend = chart.getOrCreateLegend(); + legend.setPosition(LegendPosition.TOP_RIGHT); - ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); + ScatterChartData data = chart.getChartDataFactory().createScatterChartData(); - ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); - ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); - leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); + ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM); + ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); + leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); - ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); - ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); - ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1)); + ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1)); - data.addSerie(xs, ys1); - data.addSerie(xs, ys2); + data.addSerie(xs, ys1); + data.addSerie(xs, ys2); - chart.plot(data, bottomAxis, leftAxis); + chart.plot(data, bottomAxis, leftAxis); - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SelectedSheet.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SelectedSheet.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SelectedSheet.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SelectedSheet.java Fri Sep 22 21:16:09 2017 @@ -26,21 +26,20 @@ import org.apache.poi.xssf.usermodel.XSS public abstract class SelectedSheet { public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); - wb.createSheet("row sheet"); - wb.createSheet("another sheet"); - Sheet sheet3 = wb.createSheet(" sheet 3 "); - sheet3.setSelected(true); - wb.setActiveSheet(2); + wb.createSheet("row sheet"); + wb.createSheet("another sheet"); + Sheet sheet3 = wb.createSheet(" sheet 3 "); + sheet3.setSelected(true); + wb.setActiveSheet(2); - // Create various cells and rows for spreadsheet. + // Create various cells and rows for spreadsheet. - FileOutputStream fileOut = new FileOutputStream("selectedSheet.xlsx"); - wb.write(fileOut); - fileOut.close(); - - wb.close(); + try (FileOutputStream fileOut = new FileOutputStream("selectedSheet.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java Fri Sep 22 21:16:09 2017 @@ -31,32 +31,30 @@ import org.apache.poi.xssf.usermodel.XSS public class ShiftRows { public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("Sheet1"); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); - Row row1 = sheet.createRow(1); - row1.createCell(0).setCellValue(1); + Row row1 = sheet.createRow(1); + row1.createCell(0).setCellValue(1); - Row row2 = sheet.createRow(4); - row2.createCell(1).setCellValue(2); + Row row2 = sheet.createRow(4); + row2.createCell(1).setCellValue(2); - Row row3 = sheet.createRow(5); - row3.createCell(2).setCellValue(3); + Row row3 = sheet.createRow(5); + row3.createCell(2).setCellValue(3); - Row row4 = sheet.createRow(6); - row4.createCell(3).setCellValue(4); + Row row4 = sheet.createRow(6); + row4.createCell(3).setCellValue(4); - Row row5 = sheet.createRow(9); - row5.createCell(4).setCellValue(5); + Row row5 = sheet.createRow(9); + row5.createCell(4).setCellValue(5); - // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) - sheet.shiftRows(5, 10, -4); + // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) + sheet.shiftRows(5, 10, -4); - FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + try (FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx")) { + wb.write(fileOut); + } + } } - - } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java Fri Sep 22 21:16:09 2017 @@ -29,24 +29,24 @@ import org.apache.poi.xssf.usermodel.XSS */ public class SplitAndFreezePanes { public static void main(String[]args) throws IOException { - Workbook wb = new XSSFWorkbook(); - Sheet sheet1 = wb.createSheet("new sheet"); - Sheet sheet2 = wb.createSheet("second sheet"); - Sheet sheet3 = wb.createSheet("third sheet"); - Sheet sheet4 = wb.createSheet("fourth sheet"); + try (Workbook wb = new XSSFWorkbook()) { + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); + Sheet sheet3 = wb.createSheet("third sheet"); + Sheet sheet4 = wb.createSheet("fourth sheet"); - // Freeze just one row - sheet1.createFreezePane(0, 1, 0, 1); - // Freeze just one column - sheet2.createFreezePane(1, 0, 1, 0); - // Freeze the columns and rows (forget about scrolling position of the lower right quadrant). - sheet3.createFreezePane(2, 2); - // Create a split with the lower left side being the active quadrant - sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT); + // Freeze just one row + sheet1.createFreezePane(0, 1, 0, 1); + // Freeze just one column + sheet2.createFreezePane(1, 0, 1, 0); + // Freeze the columns and rows (forget about scrolling position of the lower right quadrant). + sheet3.createFreezePane(2, 2); + // Create a split with the lower left side being the active quadrant + sheet4.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT); - FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + try (FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx")) { + wb.write(fileOut); + } + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
