diff --git a/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java b/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java index 9aaa8bf27..79b3293a6 100644 --- a/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java +++ b/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java @@ -84,24 +84,22 @@ public class SavePasswordProtectedXlsx { public static void save(final InputStream inputStream, final String filename, final String pwd) throws InvalidFormatException, IOException, GeneralSecurityException { + POIFSFileSystem fs = null; + FileOutputStream fos = null; + OPCPackage opc = null; try { - POIFSFileSystem fs = new POIFSFileSystem(); + fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile); Encryptor enc = Encryptor.getInstance(info); enc.confirmPassword(pwd); - OPCPackage opc = OPCPackage.open(inputStream); - try { - FileOutputStream fos = new FileOutputStream(filename); - try { - opc.save(enc.getDataStream(fs)); - fs.writeFilesystem(fos); - } finally { - IOUtils.closeQuietly(fos); - } - } finally { - IOUtils.closeQuietly(opc); - } + opc = OPCPackage.open(inputStream); + fos = new FileOutputStream(filename); + opc.save(enc.getDataStream(fs)); + fs.writeFilesystem(fos); } finally { + IOUtils.closeQuietly(fos); + IOUtils.closeQuietly(opc); + IOUtils.closeQuietly(fs); IOUtils.closeQuietly(inputStream); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java index 3e6736014..b84814f1b 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java @@ -18,9 +18,17 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import java.util.Date; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** @@ -29,7 +37,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateCell { - public static void main(String[]args) throws Exception { + 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"); @@ -76,5 +84,6 @@ public class CreateCell { FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java index c26ff067c..056c67fc3 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java @@ -16,9 +16,9 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel.examples; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; + import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; @@ -37,8 +37,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo; */ public class CreateTable { - public static void main(String[] args) throws FileNotFoundException, - IOException { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); @@ -88,5 +87,6 @@ public class CreateTable { FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java index f5edd4a8e..e93d997ed 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/FillsAndColors.java @@ -17,9 +17,15 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -27,7 +33,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * Fills and Colors */ public class FillsAndColors { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); @@ -37,7 +43,7 @@ public class FillsAndColors { // Aqua background CellStyle style = wb.createCellStyle(); style.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); - style.setFillPattern(CellStyle.BIG_SPOTS); + style.setFillPattern(FillPatternType.BIG_SPOTS); Cell cell = row.createCell(1); cell.setCellValue(new XSSFRichTextString("X")); cell.setCellStyle(style); @@ -45,7 +51,7 @@ public class FillsAndColors { // Orange "foreground", foreground being the fill foreground not the font color. style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); - style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell = row.createCell(2); cell.setCellValue(new XSSFRichTextString("X")); cell.setCellStyle(style); @@ -54,6 +60,6 @@ public class FillsAndColors { FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx"); wb.write(fileOut); fileOut.close(); - + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java index a781688c3..a27ebb863 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/FitSheetToOnePage.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Sheet; @@ -25,8 +26,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class FitSheetToOnePage { - - public static void main(String[]args) throws Exception { + 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(); @@ -41,6 +41,6 @@ public class FitSheetToOnePage { FileOutputStream fileOut = new FileOutputStream("fitSheetToOnePage.xlsx"); wb.write(fileOut); fileOut.close(); - + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java index 8b95fe63c..057a87a77 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/HeadersAndFooters.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.ss.usermodel.Footer; import org.apache.poi.ss.usermodel.Header; @@ -28,7 +29,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class HeadersAndFooters { - public static void main(String[]args) throws Exception { + 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); @@ -79,6 +80,6 @@ public class HeadersAndFooters { FileOutputStream fileOut = new FileOutputStream("headerFooter.xlsx"); wb.write(fileOut); fileOut.close(); - + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java index 99d1cacf6..2ac609a8a 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java @@ -17,20 +17,21 @@ package org.apache.poi.xssf.usermodel.examples; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.Sheet; +import java.io.FileInputStream; +import java.io.IOException; + import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; - -import java.io.FileInputStream; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Iterate over rows and cells */ public class IterateCells { - public static void main(String[] args) throws Exception { + 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); @@ -42,5 +43,6 @@ public class IterateCells { } } } + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java index d8564c523..d6d5e8ed0 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Chart; @@ -42,7 +43,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; */ public class LineChart { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("linechart"); final int NUM_OF_ROWS = 3; @@ -59,7 +60,7 @@ public class LineChart { } } - Drawing drawing = sheet.createDrawingPatriarch(); + Drawing> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); @@ -87,5 +88,6 @@ public class LineChart { FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java index 9d63268fc..b6bca2e8d 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/MergingCells.java @@ -17,21 +17,22 @@ package org.apache.poi.xssf.usermodel.examples; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.util.CellRangeAddress; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFRichTextString; - import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * An example of how to merge regions of cells. */ public class MergingCells { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); @@ -45,5 +46,6 @@ public class MergingCells { FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java index eb179357d..35d26918c 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/NewLinesInCells.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -30,7 +31,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; */ public class NewLinesInCells { - public static void main(String[]args) throws Exception { + public static void main(String[]args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet(); @@ -52,6 +53,7 @@ public class NewLinesInCells { FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java index 869316f08..f859c9eb5 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/Outlining.java @@ -18,6 +18,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStream; import org.apache.poi.ss.usermodel.Sheet; @@ -26,14 +27,14 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class Outlining { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Outlining o=new Outlining(); o.groupRowColumn(); o.collapseExpandRowColumn(); } - private void groupRowColumn() throws Exception { + private void groupRowColumn() throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet1 = wb.createSheet("new sheet"); @@ -50,10 +51,11 @@ public class Outlining { wb.write(fileOut); } finally { fileOut.close(); + wb.close(); } } - private void collapseExpandRowColumn() throws Exception { + private void collapseExpandRowColumn() throws IOException { Workbook wb2 = new XSSFWorkbook(); Sheet sheet2 = wb2.createSheet("new sheet"); sheet2.groupRow( 5, 14 ); @@ -76,6 +78,7 @@ public class Outlining { wb2.write(fileOut); } finally { fileOut.close(); + wb2.close(); } } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java index f0a94777f..8b02eba30 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ScatterChart.java @@ -20,19 +20,32 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.*; -import org.apache.poi.ss.usermodel.charts.*; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Chart; +import org.apache.poi.ss.usermodel.ClientAnchor; +import org.apache.poi.ss.usermodel.Drawing; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.charts.AxisCrosses; +import org.apache.poi.ss.usermodel.charts.AxisPosition; +import org.apache.poi.ss.usermodel.charts.ChartDataSource; +import org.apache.poi.ss.usermodel.charts.ChartLegend; +import org.apache.poi.ss.usermodel.charts.DataSources; +import org.apache.poi.ss.usermodel.charts.LegendPosition; +import org.apache.poi.ss.usermodel.charts.ScatterChartData; +import org.apache.poi.ss.usermodel.charts.ValueAxis; +import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Illustrates how to create a simple scatter chart. - * - * @author Roman Kashitsyn */ public class ScatterChart { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet 1"); final int NUM_OF_ROWS = 3; @@ -49,7 +62,7 @@ public class ScatterChart { } } - Drawing drawing = sheet.createDrawingPatriarch(); + Drawing> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); @@ -76,5 +89,6 @@ public class ScatterChart { FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java index ec4bb2139..5f70b6ab9 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/ShiftRows.java @@ -18,6 +18,7 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -29,7 +30,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; */ public class ShiftRows { - public static void main(String[]args) throws Exception { + public static void main(String[]args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); @@ -54,7 +55,7 @@ public class ShiftRows { FileOutputStream fileOut = new FileOutputStream("shiftRows.xlsx"); wb.write(fileOut); fileOut.close(); - + wb.close(); } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java index 937086b85..4ea6ea370 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/SplitAndFreezePanes.java @@ -17,17 +17,18 @@ package org.apache.poi.xssf.usermodel.examples; -import org.apache.poi.ss.usermodel.Workbook; +import java.io.FileOutputStream; +import java.io.IOException; + import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import java.io.FileOutputStream; - /** - * How to set spklit and freeze panes + * How to set split and freeze panes */ public class SplitAndFreezePanes { - public static void main(String[]args) throws Exception { + public static void main(String[]args) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet1 = wb.createSheet("new sheet"); Sheet sheet2 = wb.createSheet("second sheet"); @@ -46,5 +47,6 @@ public class SplitAndFreezePanes { FileOutputStream fileOut = new FileOutputStream("splitFreezePane.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java index 3a8fd56d0..5911caa6c 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java @@ -18,18 +18,17 @@ package org.apache.poi.xssf.usermodel.examples; import java.io.FileOutputStream; +import java.io.IOException; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.POIXMLProperties; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** - * How to set extended and custom properties - * - * @author Yegor Kozlov + * How to set extended and custom properties */ public class WorkbookProperties { - public static void main(String[]args) throws Exception { + public static void main(String[]args) throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(); workbook.createSheet("Workbook Properties"); @@ -59,7 +58,7 @@ public class WorkbookProperties { FileOutputStream out = new FileOutputStream("workbook.xlsx"); workbook.write(out); out.close(); - + workbook.close(); } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java index 6e12fffe7..32b233ce7 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java @@ -17,17 +17,23 @@ package org.apache.poi.xssf.usermodel.examples; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.*; - import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Working with borders */ public class WorkingWithBorders { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet("borders"); @@ -40,13 +46,13 @@ public class WorkingWithBorders { // Style the cell with borders all around. CellStyle style = wb.createCellStyle(); - style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderLeft(CellStyle.BORDER_THIN); + style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); - style.setBorderRight(CellStyle.BORDER_THIN); + style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(IndexedColors.BLUE.getIndex()); - style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); + style.setBorderTop(BorderStyle.MEDIUM_DASHED); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); cell.setCellStyle(style); @@ -54,5 +60,6 @@ public class WorkingWithBorders { FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java index 3d4393ba1..993067484 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java @@ -17,17 +17,22 @@ package org.apache.poi.xssf.usermodel.examples; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.ss.usermodel.IndexedColors; - import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * Working with Fonts */ public class WorkingWithFonts { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); Sheet sheet = wb.createSheet("Fonts"); @@ -97,5 +102,6 @@ public class WorkingWithFonts { FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java index fe5f5ad44..d40c4ed0c 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java @@ -30,7 +30,7 @@ import org.apache.poi.xwpf.usermodel.XWPFRun; public class BetterHeaderFooterExample { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.createParagraph(); @@ -48,13 +48,9 @@ public class BetterHeaderFooterExample { XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT); foot.createParagraph().createRun().setText("footer"); - try { - OutputStream os = new FileOutputStream(new File("header2.docx")); - doc.write(os); - doc.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + OutputStream os = new FileOutputStream(new File("header2.docx")); + doc.write(os); + os.close(); + doc.close(); } } diff --git a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/UpdateEmbeddedDoc.java b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/UpdateEmbeddedDoc.java index 410f3c62f..166f39cda 100644 --- a/src/examples/src/org/apache/poi/xwpf/usermodel/examples/UpdateEmbeddedDoc.java +++ b/src/examples/src/org/apache/poi/xwpf/usermodel/examples/UpdateEmbeddedDoc.java @@ -23,33 +23,31 @@ import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.List; -import java.util.Iterator; -import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; -import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.apache.poi.xwpf.usermodel.XWPFDocument; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xwpf.usermodel.XWPFDocument; /** * Tests whether it is possible to successfully update an Excel workbook that is * embedded into a WordprocessingML document. Note that the test has currently * only been conducted with a binary Excel workbook and NOT yet with a - * SpreadsheetML workbook embedded into the document. - * - *
- * This code was successfully tested with the following file from the POI test collection: - * http://svn.apache.org/repos/asf/poi/trunk/test-data/document/EmbeddedDocument.docx - *
- * - * @author Mark B + * SpreadsheetML workbook embedded into the document.
+ *
+ * This code was successfully tested with the following file from the POI test collection:
+ * http://svn.apache.org/repos/asf/poi/trunk/test-data/document/EmbeddedDocument.docx
*/
public class UpdateEmbeddedDoc {
@@ -79,27 +77,15 @@ public class UpdateEmbeddedDoc {
this.docFile = new File(filename);
FileInputStream fis = null;
if (!this.docFile.exists()) {
- throw new FileNotFoundException("The Word dcoument " +
- filename +
- " does not exist.");
+ throw new FileNotFoundException("The Word dcoument " + filename + " does not exist.");
}
try {
-
// Open the Word document file and instantiate the XWPFDocument
// class.
fis = new FileInputStream(this.docFile);
this.doc = new XWPFDocument(fis);
} finally {
- if (fis != null) {
- try {
- fis.close();
- fis = null;
- } catch (IOException ioEx) {
- System.out.println("IOException caught trying to close " +
- "FileInputStream in the constructor of " +
- "UpdateEmbeddedDoc.");
- }
- }
+ IOUtils.closeQuietly(fis);
}
}
@@ -121,35 +107,38 @@ public class UpdateEmbeddedDoc {
* file system.
*/
public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
- Workbook workbook = null;
- Sheet sheet = null;
- Row row = null;
- Cell cell = null;
- PackagePart pPart = null;
- Iterator A Paragraph within a Document, Table, Header etc.null
* @deprecated POI 3.15 beta 2. Use {@link #findFont(boolean, short, short, String, boolean, boolean, short, byte)} instead.
*/
+ @Deprecated
@Override
+ @Removal(version="3.17")
public Font findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline)
{
return _wb.findFont(boldWeight, color, fontHeight, name, italic, strikeout, typeOffset, underline);
@@ -934,7 +917,7 @@ public class SXSSFWorkbook implements Workbook {
}
/**
- * Write out this workbook to an Outputstream.
+ * Write out this workbook to an OutputStream.
*
* @param stream - the java OutputStream you wish to write to
* @exception IOException if anything can't be written.
@@ -946,27 +929,23 @@ public class SXSSFWorkbook implements Workbook {
//Save the template
File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx");
- try
- {
+ boolean deleted;
+ try {
FileOutputStream os = new FileOutputStream(tmplFile);
- try
- {
+ try {
_wb.write(os);
- }
- finally
- {
+ } finally {
os.close();
}
//Substitute the template entries with the generated sheet data files
final ZipEntrySource source = new ZipFileZipEntrySource(new ZipFile(tmplFile));
injectData(source, stream);
+ } finally {
+ deleted = tmplFile.delete();
}
- finally
- {
- if(!tmplFile.delete()) {
- throw new IOException("Could not delete temporary file after processing: " + tmplFile);
- }
+ if(!deleted) {
+ throw new IOException("Could not delete temporary file after processing: " + tmplFile);
}
}
@@ -1046,6 +1025,7 @@ public class SXSSFWorkbook implements Workbook {
*/
@Override
@Deprecated
+ @Removal(version="3.18")
public Name getNameAt(int nameIndex)
{
return _wb.getNameAt(nameIndex);
@@ -1076,6 +1056,7 @@ public class SXSSFWorkbook implements Workbook {
*/
@Override
@Deprecated
+ @Removal(version="3.18")
public int getNameIndex(String name)
{
return _wb.getNameIndex(name);
@@ -1090,6 +1071,7 @@ public class SXSSFWorkbook implements Workbook {
*/
@Override
@Deprecated
+ @Removal(version="3.18")
public void removeName(int index)
{
_wb.removeName(index);
@@ -1104,6 +1086,7 @@ public class SXSSFWorkbook implements Workbook {
*/
@Override
@Deprecated
+ @Removal(version="3.18")
public void removeName(String name)
{
_wb.removeName(name);
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
index f1f1ca381..a39fd47bf 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
@@ -46,6 +46,7 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.Removal;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
@@ -180,32 +181,20 @@ public final class XSSFCell implements Cell {
// Copy CellStyle
if (policy.isCopyCellStyle()) {
- if (srcCell != null) {
- setCellStyle(srcCell.getCellStyle());
- }
- else {
- // clear cell style
- setCellStyle(null);
- }
+ setCellStyle(srcCell == null ? null : srcCell.getCellStyle());
}
+ final Hyperlink srcHyperlink = (srcCell == null) ? null : srcCell.getHyperlink();
+
if (policy.isMergeHyperlink()) {
// if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink
- final Hyperlink srcHyperlink = srcCell.getHyperlink();
if (srcHyperlink != null) {
setHyperlink(new XSSFHyperlink(srcHyperlink));
}
- }
- else if (policy.isCopyHyperlink()) {
+ } else if (policy.isCopyHyperlink()) {
// overwrite the hyperlink at dest cell with srcCell's hyperlink
// if srcCell doesn't have a hyperlink, clear the hyperlink (if one exists) at destCell
- final Hyperlink srcHyperlink = srcCell.getHyperlink();
- if (srcHyperlink == null) {
- setHyperlink(null);
- }
- else {
- setHyperlink(new XSSFHyperlink(srcHyperlink));
- }
+ setHyperlink(srcHyperlink == null ? null : new XSSFHyperlink(srcHyperlink));
}
}
@@ -303,7 +292,9 @@ public final class XSSFCell implements Cell {
case NUMERIC:
if(_cell.isSetV()) {
String v = _cell.getV();
- if (v.isEmpty()) return 0.0;
+ if (v.isEmpty()) {
+ return 0.0;
+ }
try {
return Double.parseDouble(v);
} catch(NumberFormatException e) {
@@ -487,7 +478,9 @@ public final class XSSFCell implements Cell {
*/
protected String getCellFormula(XSSFEvaluationWorkbook fpb) {
CellType cellType = getCellTypeEnum();
- if(cellType != CellType.FORMULA) throw typeMismatch(CellType.FORMULA, cellType, false);
+ if(cellType != CellType.FORMULA) {
+ throw typeMismatch(CellType.FORMULA, cellType, false);
+ }
CTCellFormula f = _cell.getF();
if (isPartOfArrayFormulaGroup() && f == null) {
@@ -510,8 +503,10 @@ public final class XSSFCell implements Cell {
XSSFSheet sheet = getSheet();
CTCellFormula f = sheet.getSharedFormula(si);
- if(f == null) throw new IllegalStateException(
- "Master cell of a shared formula with sid="+si+" was not found");
+ if(f == null) {
+ throw new IllegalStateException(
+ "Master cell of a shared formula with sid="+si+" was not found");
+ }
String sharedFormula = f.getStringValue();
//Range of cells which the shared formula applies to
@@ -560,7 +555,9 @@ public final class XSSFCell implements Cell {
XSSFWorkbook wb = _row.getSheet().getWorkbook();
if (formula == null) {
wb.onDeleteFormula(this);
- if(_cell.isSetF()) _cell.unsetF();
+ if(_cell.isSetF()) {
+ _cell.unsetF();
+ }
return;
}
@@ -571,7 +568,9 @@ public final class XSSFCell implements Cell {
CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue(formula);
_cell.setF(f);
- if(_cell.isSetV()) _cell.unsetV();
+ if(_cell.isSetV()) {
+ _cell.unsetV();
+ }
}
/**
@@ -644,7 +643,9 @@ public final class XSSFCell implements Cell {
@Override
public void setCellStyle(CellStyle style) {
if(style == null) {
- if(_cell.isSetS()) _cell.unsetS();
+ if(_cell.isSetS()) {
+ _cell.unsetS();
+ }
} else {
XSSFCellStyle xStyle = (XSSFCellStyle)style;
xStyle.verifyBelongsToStylesSource(_stylesSource);
@@ -670,7 +671,9 @@ public final class XSSFCell implements Cell {
* @return the cell type
* @deprecated 3.15. Will return a {@link CellType} enum in the future.
*/
+ @Deprecated
@Override
+ @Removal(version="3.17")
public int getCellType() {
return getCellTypeEnum().getCode();
}
@@ -684,7 +687,9 @@ public final class XSSFCell implements Cell {
*/
@Override
public CellType getCellTypeEnum() {
- if (isFormulaCell()) return CellType.FORMULA;
+ if (isFormulaCell()) {
+ return CellType.FORMULA;
+ }
return getBaseCellType(true);
}
@@ -700,7 +705,9 @@ public final class XSSFCell implements Cell {
* on the cached value of the formula
* @deprecated 3.15. Will return a {@link CellType} enum in the future.
*/
+ @Deprecated
@Override
+ @Removal(version="3.17")
public int getCachedFormulaResultType() {
return getCachedFormulaResultTypeEnum().getCode();
}
@@ -826,7 +833,9 @@ public final class XSSFCell implements Cell {
*/
public String getErrorCellString() throws IllegalStateException {
CellType cellType = getBaseCellType(true);
- if(cellType != CellType.ERROR) throw typeMismatch(CellType.ERROR, cellType, false);
+ if(cellType != CellType.ERROR) {
+ throw typeMismatch(CellType.ERROR, cellType, false);
+ }
return _cell.getV();
}
@@ -897,7 +906,9 @@ public final class XSSFCell implements Cell {
private void setBlank(){
CTCell blank = CTCell.Factory.newInstance();
blank.setR(_cell.getR());
- if(_cell.isSetS()) blank.setS(_cell.getS());
+ if(_cell.isSetS()) {
+ blank.setS(_cell.getS());
+ }
_cell.set(blank);
}
@@ -925,7 +936,9 @@ public final class XSSFCell implements Cell {
* @see CellType#ERROR
* @deprecated POI 3.15 beta 3. Use {@link #setCellType(CellType)} instead.
*/
+ @Deprecated
@Override
+ @Removal(version="3.17")
public void setCellType(int cellType) {
setCellType(CellType.forInt(cellType));
}
@@ -964,7 +977,9 @@ public final class XSSFCell implements Cell {
CTCellFormula f = CTCellFormula.Factory.newInstance();
f.setStringValue("0");
_cell.setF(f);
- if(_cell.isSetT()) _cell.unsetT();
+ if(_cell.isSetT()) {
+ _cell.unsetT();
+ }
}
break;
case BLANK:
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
index d93b8ed5b..5066ff7d0 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
@@ -78,7 +78,7 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
options.setLoadReplaceDocumentElement(null);
ctPivotCacheDefinition = CTPivotCacheDefinition.Factory.parse(is, options);
} catch (XmlException e) {
- throw new IOException(e.getLocalizedMessage());
+ throw new IOException(e.getLocalizedMessage(), e);
}
}
@@ -123,22 +123,30 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
final String ref = wsSource.getRef();
final String name = wsSource.getName();
- if (ref == null && name == null) throw new IllegalArgumentException("Pivot cache must reference an area, named range, or table.");
+ if (ref == null && name == null) {
+ throw new IllegalArgumentException("Pivot cache must reference an area, named range, or table.");
+ }
// this is the XML format, so tell the reference that.
- if (ref != null) return new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
+ if (ref != null) {
+ return new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
+ }
- if (name != null) {
- // named range or table?
- final Name range = wb.getName(name);
- if (range != null) return new AreaReference(range.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
- // not a named range, check for a table.
- // do this second, as tables are sheet-specific, but named ranges are not, and may not have a sheet name given.
- final XSSFSheet sheet = (XSSFSheet) wb.getSheet(wsSource.getSheet());
- for (XSSFTable table : sheet.getTables()) {
- if (table.getName().equals(name)) { //case-sensitive?
- return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
- }
+ assert (name != null);
+
+ // named range or table?
+ final Name range = wb.getName(name);
+ if (range != null) {
+ return new AreaReference(range.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
+ }
+
+ // not a named range, check for a table.
+ // do this second, as tables are sheet-specific, but named ranges are not, and may not have a sheet name given.
+ final XSSFSheet sheet = (XSSFSheet) wb.getSheet(wsSource.getSheet());
+ for (XSSFTable table : sheet.getTables()) {
+ // TODO: case-sensitive?
+ if (name.equals(table.getName())) {
+ return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
}
}
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 204321049..bde93d663 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
@@ -236,7 +236,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
private void initHyperlinks() {
hyperlinks = new ArrayListA1
..
* @deprecated 3.14beta2 (circa 2015-12-05). Use {@link #setActiveCell(CellAddress)} instead.
*/
+ @Deprecated
+ @Removal(version="3.16")
public void setActiveCell(String cellRef) {
CTSelection ctsel = getSheetTypeSelection();
ctsel.setActiveCell(cellRef);
@@ -3313,11 +3351,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*/
private CTSheetView getDefaultSheetView() {
CTSheetViews views = getSheetTypeSheetViews();
- int sz = views == null ? 0 : views.sizeOfSheetViewArray();
- if (sz == 0) {
+ if (views == null) {
return null;
}
- return views.getSheetViewArray(sz - 1);
+ int sz = views.sizeOfSheetViewArray();
+ return (sz == 0) ? null : views.getSheetViewArray(sz - 1);
}
/**
@@ -3835,6 +3873,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return dataValidationHelper;
}
+ @Override
public List