]> source.dussan.org Git - poi.git/commitdiff
SonarQube fixes - close resources
authorAndreas Beeker <kiwiwings@apache.org>
Wed, 31 May 2017 00:31:53 +0000 (00:31 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Wed, 31 May 2017 00:31:53 +0000 (00:31 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1796962 13f79535-47bb-0310-9956-ffa450edef68

21 files changed:
src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java
src/examples/src/org/apache/poi/hslf/examples/CreateHyperlink.java
src/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java
src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java
src/examples/src/org/apache/poi/hslf/examples/TableDemo.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/Borders.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java
src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java
src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java
src/examples/src/org/apache/poi/xslf/usermodel/MergePresentations.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial2.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial4.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial6.java
src/examples/src/org/apache/poi/xslf/usermodel/Tutorial7.java
src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java

index 1997200884599c4243b97ae30459200afb2c1ec3..53378a17ee25264c6827c85f10d28391c8b4af2b 100644 (file)
@@ -34,30 +34,31 @@ public final class BulletsDemo {
     public static void main(String[] args) throws IOException {
 
         HSLFSlideShow ppt = new HSLFSlideShow();
-
-        HSLFSlide slide = ppt.createSlide();
-
-        HSLFTextBox shape = new HSLFTextBox();
-        HSLFTextParagraph rt = shape.getTextParagraphs().get(0);
-        rt.getTextRuns().get(0).setFontSize(42d);
-        rt.setBullet(true);
-        rt.setIndent(0d);  //bullet offset
-        rt.setLeftMargin(50d);   //text offset (should be greater than bullet offset)
-        rt.setBulletChar('\u263A'); //bullet character
-        shape.setText(
-                "January\r" +
-                "February\r" +
-                "March\r" +
-                "April");
-        slide.addShape(shape);
-
-        shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300));  //position of the text box in the slide
-        slide.addShape(shape);
-
-        FileOutputStream out = new FileOutputStream("bullets.ppt");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            HSLFSlide slide = ppt.createSlide();
+    
+            HSLFTextBox shape = new HSLFTextBox();
+            HSLFTextParagraph rt = shape.getTextParagraphs().get(0);
+            rt.getTextRuns().get(0).setFontSize(42d);
+            rt.setBullet(true);
+            rt.setIndent(0d);  //bullet offset
+            rt.setLeftMargin(50d);   //text offset (should be greater than bullet offset)
+            rt.setBulletChar('\u263A'); //bullet character
+            shape.setText(
+                    "January\r" +
+                    "February\r" +
+                    "March\r" +
+                    "April");
+            slide.addShape(shape);
+    
+            shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300));  //position of the text box in the slide
+            slide.addShape(shape);
+    
+            FileOutputStream out = new FileOutputStream("bullets.ppt");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
    }
 }
index fc25afae55997a7ec8a42608c2404fc2cf7a49ec..9ae200f8b27a24772c00e90282acd5afa92cf49d 100644 (file)
@@ -34,31 +34,33 @@ public abstract class CreateHyperlink {
     public static void main(String[] args) throws IOException {
         HSLFSlideShow ppt = new HSLFSlideShow();
 
-        HSLFSlide slideA = ppt.createSlide();
-        ppt.createSlide();
-        HSLFSlide slideC = ppt.createSlide();
-
-        // link to a URL
-        HSLFTextBox textBox1 = slideA.createTextBox();
-        textBox1.setText("Apache POI");
-        textBox1.setAnchor(new Rectangle(100, 100, 200, 50));
-
-        HSLFHyperlink link1 = textBox1.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
-        link1.linkToUrl("http://www.apache.org");
-        link1.setLabel(textBox1.getText());
-
-        // link to another slide
-        HSLFTextBox textBox2 = slideA.createTextBox();
-        textBox2.setText("Go to slide #3");
-        textBox2.setAnchor(new Rectangle(100, 300, 200, 50));
-
-        HSLFHyperlink link2 = textBox2.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
-        link2.linkToSlide(slideC);
-
-        FileOutputStream out = new FileOutputStream("hyperlink.ppt");
-        ppt.write(out);
-        out.close();
-
-        ppt.close();
+        try {
+            HSLFSlide slideA = ppt.createSlide();
+            ppt.createSlide();
+            HSLFSlide slideC = ppt.createSlide();
+    
+            // link to a URL
+            HSLFTextBox textBox1 = slideA.createTextBox();
+            textBox1.setText("Apache POI");
+            textBox1.setAnchor(new Rectangle(100, 100, 200, 50));
+    
+            HSLFHyperlink link1 = textBox1.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
+            link1.linkToUrl("http://www.apache.org");
+            link1.setLabel(textBox1.getText());
+    
+            // link to another slide
+            HSLFTextBox textBox2 = slideA.createTextBox();
+            textBox2.setText("Go to slide #3");
+            textBox2.setAnchor(new Rectangle(100, 300, 200, 50));
+    
+            HSLFHyperlink link2 = textBox2.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
+            link2.linkToSlide(slideC);
+    
+            FileOutputStream out = new FileOutputStream("hyperlink.ppt");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
    }
 }
index 53cd53edc28e67e0b507793bde0e6cf500d83a18..c149a9d47d489e72a9f0bc2a26bc778705abb513 100644 (file)
@@ -41,45 +41,47 @@ public final class Graphics2DDemo {
     public static void main(String[] args) throws Exception {
         HSLFSlideShow ppt = new HSLFSlideShow();
 
-        //bar chart data. The first value is the bar color, the second is the width
-        Object[] def = new Object[]{
-            Color.yellow, 40,
-            Color.green, 60,
-            Color.gray, 30,
-            Color.red, 80,
-        };
-
-        HSLFSlide slide = ppt.createSlide();
-
-        HSLFGroupShape group = new HSLFGroupShape();
-        //define position of the drawing in the slide
-        Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
-        group.setAnchor(bounds);
-        group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
-        slide.addShape(group);
-        Graphics2D graphics = new PPGraphics2D(group);
-
-        //draw a simple bar graph
-        int x = 10, y = 10;
-        graphics.setFont(new Font("Arial", Font.BOLD, 10));
-        for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
+        try {
+            //bar chart data. The first value is the bar color, the second is the width
+            Object[] def = new Object[]{
+                Color.yellow, 40,
+                Color.green, 60,
+                Color.gray, 30,
+                Color.red, 80,
+            };
+    
+            HSLFSlide slide = ppt.createSlide();
+    
+            HSLFGroupShape group = new HSLFGroupShape();
+            //define position of the drawing in the slide
+            Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
+            group.setAnchor(bounds);
+            group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
+            slide.addShape(group);
+            Graphics2D graphics = new PPGraphics2D(group);
+    
+            //draw a simple bar graph
+            int x = 10, y = 10;
+            graphics.setFont(new Font("Arial", Font.BOLD, 10));
+            for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
+                graphics.setColor(Color.black);
+                int width = ((Integer)def[i+1]).intValue();
+                graphics.drawString("Q" + idx, x-5, y+10);
+                graphics.drawString(width + "%", x + width+3, y + 10);
+                graphics.setColor((Color)def[i]);
+                graphics.fill(new Rectangle(x, y, width, 10));
+                y += 15;
+            }
             graphics.setColor(Color.black);
-            int width = ((Integer)def[i+1]).intValue();
-            graphics.drawString("Q" + idx, x-5, y+10);
-            graphics.drawString(width + "%", x + width+3, y + 10);
-            graphics.setColor((Color)def[i]);
-            graphics.fill(new Rectangle(x, y, width, 10));
-            y += 15;
+            graphics.setFont(new Font("Arial", Font.BOLD, 14));
+            graphics.draw(group.getInteriorAnchor());
+            graphics.drawString("Performance", x + 30, y + 10);
+    
+            FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
         }
-        graphics.setColor(Color.black);
-        graphics.setFont(new Font("Arial", Font.BOLD, 14));
-        graphics.draw(group.getInteriorAnchor());
-        graphics.drawString("Performance", x + 30, y + 10);
-
-        FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
-        ppt.write(out);
-        out.close();
-
-        ppt.close();
     }
 }
index 3ef5a46ab3c248cfddadb35cfabfffdfef64db33..cc86b760636102fcbbd06c25591d73bdbe04f041 100644 (file)
@@ -29,22 +29,24 @@ public abstract class HeadersFootersDemo {
     public static void main(String[] args) throws IOException {
         HSLFSlideShow ppt = new HSLFSlideShow();
         
-        HeadersFooters slideHeaders = ppt.getSlideHeadersFooters();
-        slideHeaders.setFootersText("Created by POI-HSLF");
-        slideHeaders.setSlideNumberVisible(true);
-        slideHeaders.setDateTimeText("custom date time");
-
-        HeadersFooters notesHeaders = ppt.getNotesHeadersFooters();
-        notesHeaders.setFootersText("My notes footers");
-        notesHeaders.setHeaderText("My notes header");
-
-        ppt.createSlide();
-
-        FileOutputStream out = new FileOutputStream("headers_footers.ppt");
-        ppt.write(out);
-        out.close();
-
-        ppt.close();
+        try {
+            HeadersFooters slideHeaders = ppt.getSlideHeadersFooters();
+            slideHeaders.setFootersText("Created by POI-HSLF");
+            slideHeaders.setSlideNumberVisible(true);
+            slideHeaders.setDateTimeText("custom date time");
+    
+            HeadersFooters notesHeaders = ppt.getNotesHeadersFooters();
+            notesHeaders.setFootersText("My notes footers");
+            notesHeaders.setHeaderText("My notes header");
+    
+            ppt.createSlide();
+    
+            FileOutputStream out = new FileOutputStream("headers_footers.ppt");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 
 }
index e4ad98dc19e17174f6cc327a6493bb735bfec0da..ab869dea628ac61262f50f4ffa5f110ffc9d03e6 100644 (file)
@@ -31,27 +31,45 @@ import org.apache.poi.sl.usermodel.VerticalAlignment;
 
 /**
  * Demonstrates how to create tables
- *
- * @author Yegor Kozlov
  */
 public final class TableDemo {
 
-    public static void main(String[] args) throws Exception {
+    //test data for the first table
+    static final String[][] txt1 = {
+        {"INPUT FILE", "NUMBER OF RECORDS"},
+        {"Item File", "11,559"},
+        {"Vendor File", "502"},
+        {"Purchase History File - # of PO\u2019s\r(12/01/04 - 05/31/06)", "12,852"},
+        {"Purchase History File - # of PO Lines\r(12/01/04 - 05/31/06)", "53,523" },
+        {"Total PO History Spend", "$10,172,038"}
+    };
+
+    //test data for the second taable
+    static final String[][] txt2 = {
+        {"Data Source"},
+        {"CAS Internal Metrics - Item Master Summary\r" +
+         "CAS Internal Metrics - Vendor Summary\r" +
+         "CAS Internal Metrics - PO History Summary"}
+    };
 
-        //test data for the first taable
-        String[][] txt1 = {
-            {"INPUT FILE", "NUMBER OF RECORDS"},
-            {"Item File", "11,559"},
-            {"Vendor File", "502"},
-            {"Purchase History File - # of PO\u2019s\r(12/01/04 - 05/31/06)", "12,852"},
-            {"Purchase History File - # of PO Lines\r(12/01/04 - 05/31/06)", "53,523" },
-            {"Total PO History Spend", "$10,172,038"}
-        };
 
+    public static void main(String[] args) throws Exception {
         HSLFSlideShow ppt = new HSLFSlideShow();
 
-        HSLFSlide slide = ppt.createSlide();
-
+        try {
+            HSLFSlide slide = ppt.createSlide();
+            create1stTable(slide);
+            create2ndTable(slide);
+    
+            FileOutputStream out = new FileOutputStream("hslf-table.ppt");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
+    }
+    
+    static void create1stTable(HSLFSlide slide) {
         //six rows, two columns
         HSLFTable table1 = slide.createTable(6, 2);
         for (int i = 0; i < txt1.length; i++) {
@@ -77,17 +95,11 @@ public final class TableDemo {
         table1.setColumnWidth(0, 300);
         table1.setColumnWidth(1, 150);
 
-        int pgWidth = ppt.getPageSize().width;
+        int pgWidth = slide.getSlideShow().getPageSize().width;
         table1.moveTo((pgWidth - table1.getAnchor().getWidth())/2., 100.);
+    }
 
-        //test data for the second taable
-        String[][] txt2 = {
-            {"Data Source"},
-            {"CAS Internal Metrics - Item Master Summary\r" +
-             "CAS Internal Metrics - Vendor Summary\r" +
-             "CAS Internal Metrics - PO History Summary"}
-        };
-
+    static void create2ndTable(HSLFSlide slide) {
         //two rows, one column
         HSLFTable table2 = slide.createTable(2, 1);
         for (int i = 0; i < txt2.length; i++) {
@@ -120,11 +132,5 @@ public final class TableDemo {
         dts2.setOutsideBorders(Color.black, 1.0);
 
         table2.moveTo(200, 400);
-
-        FileOutputStream out = new FileOutputStream("hslf-table.ppt");
-        ppt.write(out);
-        out.close();
-
-        ppt.close();
     }
 }
index 9b86cf7793542b4e15a5d16f942da02ddcf1916b..d9896a712c4623b5f306abf47fd39bc03f4fee94 100644 (file)
@@ -731,13 +731,16 @@ public class AddDimensionedImage {
             outputFile = args[1];
 
             HSSFWorkbook workbook = new HSSFWorkbook();
-            sheet = workbook.createSheet("Picture Test");
-            new AddDimensionedImage().addImageToSheet("A1", sheet,
-                    imageFile, 125, 125,
-                    AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
-            fos = new FileOutputStream(outputFile);
-            workbook.write(fos);
-            workbook.close();
+            try  {
+                sheet = workbook.createSheet("Picture Test");
+                new AddDimensionedImage().addImageToSheet("A1", sheet,
+                        imageFile, 125, 125,
+                        AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
+                fos = new FileOutputStream(outputFile);
+                workbook.write(fos);
+            } finally {
+                workbook.close();
+            }
         }
         catch(FileNotFoundException fnfEx) {
             System.out.println("Caught an: " + fnfEx.getClass().getName());
index d108214e60c9c14d845841cc010fcb05380b370d..63b8b57dadeee7f5f19e0625e3ea3cea2c8a695b 100644 (file)
@@ -38,138 +38,142 @@ public class BigExample {
     public static void main(String[] args) throws IOException {
         int rownum;
 
-        // create a new file
-        FileOutputStream out = new FileOutputStream("workbook.xls");
         // create a new workbook
         HSSFWorkbook wb = new HSSFWorkbook();
-        // create a new sheet
-        HSSFSheet s = wb.createSheet();
-        // declare a row object reference
-        HSSFRow r = null;
-        // declare a cell object reference
-        HSSFCell c = null;
-        // create 3 cell styles
-        HSSFCellStyle cs = wb.createCellStyle();
-        HSSFCellStyle cs2 = wb.createCellStyle();
-        HSSFCellStyle cs3 = wb.createCellStyle();
-        // create 2 fonts objects
-        HSSFFont f = wb.createFont();
-        HSSFFont f2 = wb.createFont();
-
-        //set font 1 to 12 point type
-        f.setFontHeightInPoints((short) 12);
-        //make it red
-        f.setColor(HSSFColorPredefined.RED.getIndex());
-        // make it bold
-        //arial is the default font
-        f.setBold(true);
-
-        //set font 2 to 10 point type
-        f2.setFontHeightInPoints((short) 10);
-        //make it the color at palette index 0xf (white)
-        f2.setColor(HSSFColorPredefined.WHITE.getIndex());
-        //make it bold
-        f2.setBold(true);
-
-        //set cell stlye
-        cs.setFont(f);
-        //set the cell format see HSSFDataFromat for a full list
-        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
-
-        //set a thin border
-        cs2.setBorderBottom(BorderStyle.THIN);
-        //fill w fg fill color
-        cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
-        // set foreground fill to red
-        cs2.setFillForegroundColor(HSSFColorPredefined.RED.getIndex());
-
-        // set the font
-        cs2.setFont(f2);
-
-        // set the sheet name to HSSF Test
-        wb.setSheetName(0, "HSSF Test");
-        // create a sheet with 300 rows (0-299)
-        for (rownum = 0; rownum < 300; rownum++)
-        {
-            // create a row
-            r = s.createRow(rownum);
-            // on every other row
-            if ((rownum % 2) == 0)
+        try {
+            // create a new sheet
+            HSSFSheet s = wb.createSheet();
+            // declare a row object reference
+            HSSFRow r = null;
+            // declare a cell object reference
+            HSSFCell c = null;
+            // create 3 cell styles
+            HSSFCellStyle cs = wb.createCellStyle();
+            HSSFCellStyle cs2 = wb.createCellStyle();
+            HSSFCellStyle cs3 = wb.createCellStyle();
+            // create 2 fonts objects
+            HSSFFont f = wb.createFont();
+            HSSFFont f2 = wb.createFont();
+    
+            //set font 1 to 12 point type
+            f.setFontHeightInPoints((short) 12);
+            //make it red
+            f.setColor(HSSFColorPredefined.RED.getIndex());
+            // make it bold
+            //arial is the default font
+            f.setBold(true);
+    
+            //set font 2 to 10 point type
+            f2.setFontHeightInPoints((short) 10);
+            //make it the color at palette index 0xf (white)
+            f2.setColor(HSSFColorPredefined.WHITE.getIndex());
+            //make it bold
+            f2.setBold(true);
+    
+            //set cell stlye
+            cs.setFont(f);
+            //set the cell format see HSSFDataFromat for a full list
+            cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
+    
+            //set a thin border
+            cs2.setBorderBottom(BorderStyle.THIN);
+            //fill w fg fill color
+            cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            // set foreground fill to red
+            cs2.setFillForegroundColor(HSSFColorPredefined.RED.getIndex());
+    
+            // set the font
+            cs2.setFont(f2);
+    
+            // set the sheet name to HSSF Test
+            wb.setSheetName(0, "HSSF Test");
+            // create a sheet with 300 rows (0-299)
+            for (rownum = 0; rownum < 300; rownum++)
             {
-                // make the row height bigger  (in twips - 1/20 of a point)
-                r.setHeight((short) 0x249);
-            }
-
-            //r.setRowNum(( short ) rownum);
-            // create 50 cells (0-49) (the += 2 becomes apparent later
-            for (int cellnum = 0; cellnum < 50; cellnum += 2)
-            {
-                // create a numeric cell
-                c = r.createCell(cellnum);
-                // do some goofy math to demonstrate decimals
-                c.setCellValue(rownum * 10000 + cellnum
-                        + (((double) rownum / 1000)
-                        + ((double) cellnum / 10000)));
-
+                // create a row
+                r = s.createRow(rownum);
                 // on every other row
                 if ((rownum % 2) == 0)
                 {
-                    // set this cell to the first cell style we defined
-                    c.setCellStyle(cs);
+                    // make the row height bigger  (in twips - 1/20 of a point)
+                    r.setHeight((short) 0x249);
                 }
-
-                // create a string cell (see why += 2 in the
-                c = r.createCell(cellnum + 1);
-
-                // set the cell's string value to "TEST"
-                c.setCellValue("TEST");
-                // make this column a bit wider
-                s.setColumnWidth(cellnum + 1, (int)((50 * 8) / ((double) 1 / 20)));
-
-                // on every other row
-                if ((rownum % 2) == 0)
+    
+                //r.setRowNum(( short ) rownum);
+                // create 50 cells (0-49) (the += 2 becomes apparent later
+                for (int cellnum = 0; cellnum < 50; cellnum += 2)
                 {
-                    // set this to the white on red cell style
-                    // we defined above
-                    c.setCellStyle(cs2);
+                    // create a numeric cell
+                    c = r.createCell(cellnum);
+                    // do some goofy math to demonstrate decimals
+                    c.setCellValue(rownum * 10000 + cellnum
+                            + (((double) rownum / 1000)
+                            + ((double) cellnum / 10000)));
+    
+                    // on every other row
+                    if ((rownum % 2) == 0)
+                    {
+                        // set this cell to the first cell style we defined
+                        c.setCellStyle(cs);
+                    }
+    
+                    // create a string cell (see why += 2 in the
+                    c = r.createCell(cellnum + 1);
+    
+                    // set the cell's string value to "TEST"
+                    c.setCellValue("TEST");
+                    // make this column a bit wider
+                    s.setColumnWidth(cellnum + 1, (int)((50 * 8) / ((double) 1 / 20)));
+    
+                    // on every other row
+                    if ((rownum % 2) == 0)
+                    {
+                        // set this to the white on red cell style
+                        // we defined above
+                        c.setCellStyle(cs2);
+                    }
+    
                 }
-
             }
+    
+            //draw a thick black border on the row at the bottom using BLANKS
+            // advance 2 rows
+            rownum++;
+            rownum++;
+    
+            r = s.createRow(rownum);
+    
+            // define the third style to be the default
+            // except with a thick black border at the bottom
+            cs3.setBorderBottom(BorderStyle.THICK);
+    
+            //create 50 cells
+            for (int cellnum =0; cellnum < 50; cellnum++) {
+                //create a blank type cell (no value)
+                c = r.createCell(cellnum);
+                // set it to the thick black border style
+                c.setCellStyle(cs3);
+            }
+    
+            //end draw thick black border
+    
+    
+            // demonstrate adding/naming and deleting a sheet
+            // create a sheet, set its title then delete it
+            wb.createSheet();
+            wb.setSheetName(1, "DeletedSheet");
+            wb.removeSheetAt(1);
+            //end deleted sheet
+    
+            // create a new file
+            FileOutputStream out = new FileOutputStream("workbook.xls");
+    
+            // write the workbook to the output stream
+            // close our file (don't blow out our file handles
+            wb.write(out);
+            out.close();
+        } finally {
+            wb.close();
         }
-
-        //draw a thick black border on the row at the bottom using BLANKS
-        // advance 2 rows
-        rownum++;
-        rownum++;
-
-        r = s.createRow(rownum);
-
-        // define the third style to be the default
-        // except with a thick black border at the bottom
-        cs3.setBorderBottom(BorderStyle.THICK);
-
-        //create 50 cells
-        for (int cellnum =0; cellnum < 50; cellnum++) {
-            //create a blank type cell (no value)
-            c = r.createCell(cellnum);
-            // set it to the thick black border style
-            c.setCellStyle(cs3);
-        }
-
-        //end draw thick black border
-
-
-        // demonstrate adding/naming and deleting a sheet
-        // create a sheet, set its title then delete it
-        wb.createSheet();
-        wb.setSheetName(1, "DeletedSheet");
-        wb.removeSheetAt(1);
-        //end deleted sheet
-
-        // write the workbook to the output stream
-        // close our file (don't blow out our file handles
-        wb.write(out);
-        out.close();
-        wb.close();
     }
 }
index 8573f111393755e321c09ef67821cc6652f369b7..e332f4f7fbbf5d83770aba4206b304c5ddf3b4fa 100644 (file)
@@ -34,32 +34,34 @@ import org.apache.poi.ss.usermodel.BorderStyle;
 public class Borders {
     public static void main(String[] args) throws IOException {
         HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet("new sheet");
+        try {
+            HSSFSheet sheet = wb.createSheet("new sheet");
 
-        // Create a row and put some cells in it. Rows are 0 based.
-        HSSFRow row = sheet.createRow(1);
+            // Create a row and put some cells in it. Rows are 0 based.
+            HSSFRow row = sheet.createRow(1);
 
-        // Create a cell and put a value in it.
-        HSSFCell cell = row.createCell(1);
-        cell.setCellValue(4);
+            // Create a cell and put a value in it.
+            HSSFCell cell = row.createCell(1);
+            cell.setCellValue(4);
 
-        // Style the cell with borders all around.
-        HSSFCellStyle style = wb.createCellStyle();
-        style.setBorderBottom(BorderStyle.THIN);
-        style.setBottomBorderColor(HSSFColorPredefined.BLACK.getIndex());
-        style.setBorderLeft(BorderStyle.THIN);
-        style.setLeftBorderColor(HSSFColorPredefined.GREEN.getIndex());
-        style.setBorderRight(BorderStyle.THIN);
-        style.setRightBorderColor(HSSFColorPredefined.BLUE.getIndex());
-        style.setBorderTop(BorderStyle.MEDIUM_DASHED);
-        style.setTopBorderColor(HSSFColorPredefined.ORANGE.getIndex());
-        cell.setCellStyle(style);
+            // Style the cell with borders all around.
+            HSSFCellStyle style = wb.createCellStyle();
+            style.setBorderBottom(BorderStyle.THIN);
+            style.setBottomBorderColor(HSSFColorPredefined.BLACK.getIndex());
+            style.setBorderLeft(BorderStyle.THIN);
+            style.setLeftBorderColor(HSSFColorPredefined.GREEN.getIndex());
+            style.setBorderRight(BorderStyle.THIN);
+            style.setRightBorderColor(HSSFColorPredefined.BLUE.getIndex());
+            style.setBorderTop(BorderStyle.MEDIUM_DASHED);
+            style.setTopBorderColor(HSSFColorPredefined.ORANGE.getIndex());
+            cell.setCellStyle(style);
 
-        // Write the output to a file
-        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-        wb.write(fileOut);
-        fileOut.close();
-        
-        wb.close();
+            // Write the output to a file
+            FileOutputStream fileOut = new FileOutputStream("workbook.xls");
+            wb.write(fileOut);
+            fileOut.close();
+        } finally {
+            wb.close();
+        }
     }
 }
index 35df7690ac3b486d291536201a1ad4e03d604cdc..2368591392ba6daf8211be5255395e1f7a424121 100644 (file)
@@ -31,76 +31,76 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
 
 /**
- * Demonstrates how to work with excel cell comments.
+ * Demonstrates how to work with excel cell comments.<p>
  *
- * <p>
  * Excel comment is a kind of a text shape,
  * so inserting a comment is very similar to placing a text box in a worksheet
- * </p>
  */
 public class CellComments {
 
     public static void main(String[] args) throws IOException  {
 
         HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
-
-        // Create the drawing patriarch. This is the top level container for all shapes including cell comments.
-        HSSFPatriarch patr = sheet.createDrawingPatriarch();
-
-        //create a cell in row 3
-        HSSFCell cell1 = sheet.createRow(3).createCell(1);
-        cell1.setCellValue(new HSSFRichTextString("Hello, World"));
-
-        //anchor defines size and position of the comment in worksheet
-        HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
-
-         // set text in the comment
-        comment1.setString(new HSSFRichTextString("We can set comments in POI"));
-
-        //set comment author.
-        //you can see it in the status bar when moving mouse over the commented cell
-        comment1.setAuthor("Apache Software Foundation");
-
-        // The first way to assign comment to a cell is via HSSFCell.setCellComment method
-        cell1.setCellComment(comment1);
-
-        //create another cell in row 6
-        HSSFCell cell2 = sheet.createRow(6).createCell(1);
-        cell2.setCellValue(36.6);
-
-
-        HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 8, (short) 6, 11));
-        //modify background color of the comment
-        comment2.setFillColor(204, 236, 255);
-
-        HSSFRichTextString string = new HSSFRichTextString("Normal body temperature");
-
-        //apply custom font to the text in the comment
-        HSSFFont font = wb.createFont();
-        font.setFontName("Arial");
-        font.setFontHeightInPoints((short)10);
-        font.setBold(true);
-        font.setColor(HSSFColorPredefined.RED.getIndex());
-        string.applyFont(font);
-
-        comment2.setString(string);
-        comment2.setVisible(true); //by default comments are hidden. This one is always visible.
-
-        comment2.setAuthor("Bill Gates");
-
-        /**
-         * The second way to assign comment to a cell is to implicitly specify its row and column.
-         * Note, it is possible to set row and column of a non-existing cell.
-         * It works, the comment is visible.
-         */
-        comment2.setRow(6);
-        comment2.setColumn(1);
-
-        FileOutputStream out = new FileOutputStream("poi_comment.xls");
-        wb.write(out);
-        out.close();
-        
-        wb.close();
+        try {
+            HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
+    
+            // Create the drawing patriarch. This is the top level container for all shapes including cell comments.
+            HSSFPatriarch patr = sheet.createDrawingPatriarch();
+    
+            //create a cell in row 3
+            HSSFCell cell1 = sheet.createRow(3).createCell(1);
+            cell1.setCellValue(new HSSFRichTextString("Hello, World"));
+    
+            //anchor defines size and position of the comment in worksheet
+            HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
+    
+             // set text in the comment
+            comment1.setString(new HSSFRichTextString("We can set comments in POI"));
+    
+            //set comment author.
+            //you can see it in the status bar when moving mouse over the commented cell
+            comment1.setAuthor("Apache Software Foundation");
+    
+            // The first way to assign comment to a cell is via HSSFCell.setCellComment method
+            cell1.setCellComment(comment1);
+    
+            //create another cell in row 6
+            HSSFCell cell2 = sheet.createRow(6).createCell(1);
+            cell2.setCellValue(36.6);
+    
+    
+            HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 8, (short) 6, 11));
+            //modify background color of the comment
+            comment2.setFillColor(204, 236, 255);
+    
+            HSSFRichTextString string = new HSSFRichTextString("Normal body temperature");
+    
+            //apply custom font to the text in the comment
+            HSSFFont font = wb.createFont();
+            font.setFontName("Arial");
+            font.setFontHeightInPoints((short)10);
+            font.setBold(true);
+            font.setColor(HSSFColorPredefined.RED.getIndex());
+            string.applyFont(font);
+    
+            comment2.setString(string);
+            comment2.setVisible(true); //by default comments are hidden. This one is always visible.
+    
+            comment2.setAuthor("Bill Gates");
+    
+            /**
+             * The second way to assign comment to a cell is to implicitly specify its row and column.
+             * Note, it is possible to set row and column of a non-existing cell.
+             * It works, the comment is visible.
+             */
+            comment2.setRow(6);
+            comment2.setColumn(1);
+    
+            FileOutputStream out = new FileOutputStream("poi_comment.xls");
+            wb.write(out);
+            out.close();
+        } finally {
+            wb.close();
+        }
     }
 }
index 80ced95ac0c37aaa1fae8350348890b7856f92b4..fd29e34e0ead58bc414debd06240ee69ceb86300 100644 (file)
@@ -29,19 +29,21 @@ import org.apache.poi.ss.usermodel.CellType;
 public class CellTypes {
     public static void main(String[] args) throws IOException {
         HSSFWorkbook wb = new HSSFWorkbook();
-        HSSFSheet sheet = wb.createSheet("new sheet");
-        HSSFRow row = sheet.createRow(2);
-        row.createCell(0).setCellValue(1.1);
-        row.createCell(1).setCellValue(new Date());
-        row.createCell(2).setCellValue("a string");
-        row.createCell(3).setCellValue(true);
-        row.createCell(4).setCellType(CellType.ERROR);
-
-        // Write the output to a file
-        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-        wb.write(fileOut);
-        fileOut.close();
-        
-        wb.close();
+        try {
+            HSSFSheet sheet = wb.createSheet("new sheet");
+            HSSFRow row = sheet.createRow(2);
+            row.createCell(0).setCellValue(1.1);
+            row.createCell(1).setCellValue(new Date());
+            row.createCell(2).setCellValue("a string");
+            row.createCell(3).setCellValue(true);
+            row.createCell(4).setCellType(CellType.ERROR);
+    
+            // Write the output to a file
+            FileOutputStream fileOut = new FileOutputStream("workbook.xls");
+            wb.write(fileOut);
+            fileOut.close();
+        } finally {
+            wb.close();
+        }
     }
 }
index 394026cdc57c47927d49f5f958bde83967034fa3..cfbc0941c05e3bc70d8d222c3cf37f3e103f657b 100644 (file)
@@ -41,6 +41,7 @@ public class UserDefinedFunctionExample {
     public static void main( String[] args ) throws Exception {
         
         if(  args.length != 2 ) {
+            // e.g. src/examples/src/org/apache/poi/ss/examples/formula/mortgage-calculation.xls Sheet1!B4
             System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
             return;
         }
@@ -50,32 +51,32 @@ public class UserDefinedFunctionExample {
         
         File workbookFile = new File( args[0] ) ;
         
-        FileInputStream fis = new FileInputStream(workbookFile);
-        Workbook workbook = WorkbookFactory.create(fis);
-        fis.close();
+        Workbook workbook = WorkbookFactory.create(workbookFile, null, true);
 
-        String[] functionNames = { "calculatePayment" } ;
-        FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
-        
-        UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
-
-        // register the user-defined function in the workbook
-        workbook.addToolPack(udfToolpack);
-
-        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
-
-        CellReference cr = new CellReference( args[1] ) ;
-        String sheetName = cr.getSheetName() ;
-        Sheet sheet = workbook.getSheet( sheetName ) ;
-        int rowIdx = cr.getRow() ;
-        int colIdx = cr.getCol() ;
-        Row row = sheet.getRow( rowIdx ) ;
-        Cell cell = row.getCell( colIdx ) ;
-        
-        CellValue value = evaluator.evaluate( cell ) ;
-        
-        System.out.println("returns value: " +  value ) ;
-        
-        workbook.close();
+        try {
+            String[] functionNames = { "calculatePayment" } ;
+            FreeRefFunction[] functionImpls = { new CalculateMortgage() } ;
+            
+            UDFFinder udfToolpack = new DefaultUDFFinder( functionNames, functionImpls ) ;
+    
+            // register the user-defined function in the workbook
+            workbook.addToolPack(udfToolpack);
+    
+            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
+    
+            CellReference cr = new CellReference( args[1] ) ;
+            String sheetName = cr.getSheetName() ;
+            Sheet sheet = workbook.getSheet( sheetName ) ;
+            int rowIdx = cr.getRow() ;
+            int colIdx = cr.getCol() ;
+            Row row = sheet.getRow( rowIdx ) ;
+            Cell cell = row.getCell( colIdx ) ;
+            
+            CellValue value = evaluator.evaluate( cell ) ;
+            
+            System.out.println("returns value: " +  value ) ;
+        } finally {
+            workbook.close();
+        }
     }
 }
index 73f195eb7608aeabc9ddd7717a4681e244d82573..de48a7df4cc729212df9a504a01cfa2322775675 100644 (file)
@@ -24,31 +24,30 @@ import java.io.FileOutputStream;
 
 /**
  * Merge multiple pptx presentations together
- *
- * @author Yegor Kozlov
  */
 public final class MergePresentations {
 
     public static void main(String args[]) throws Exception {
         XMLSlideShow ppt = new XMLSlideShow();
 
-        for(String arg : args){
-            FileInputStream is = new FileInputStream(arg);
-            XMLSlideShow src = new XMLSlideShow(is);
-            is.close();
-
-            for(XSLFSlide srcSlide : src.getSlides()){
-                ppt.createSlide().importContent(srcSlide);
+        try {
+            for (String arg : args){
+                FileInputStream is = new FileInputStream(arg);
+                XMLSlideShow src = new XMLSlideShow(is);
+                is.close();
+    
+                for(XSLFSlide srcSlide : src.getSlides()){
+                    ppt.createSlide().importContent(srcSlide);
+                }
+                
+                src.close();
             }
-            
-            src.close();
+    
+            FileOutputStream out = new FileOutputStream("merged.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
         }
-
-        FileOutputStream out = new FileOutputStream("merged.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
     }
-
 }
index 714ebb8121bce8072508c6c676bcf9af4e85d517..ed39f3051947fd01b425ceeb3edd447acb19861e 100644 (file)
@@ -25,50 +25,50 @@ import java.io.IOException;
 /**
  * Demonstrates how to create slides with predefined layout
  * and fill the placeholder shapes
- *
- * @author Yegor Kozlov
  */
 public class Tutorial1 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        // XSLFSlide#createSlide() with no arguments creates a blank slide
-        /*XSLFSlide blankSlide =*/ ppt.createSlide();
-
-        
-        XSLFSlideMaster master = ppt.getSlideMasters().get(0);
-
-        XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
-        XSLFSlide slide1 = ppt.createSlide(layout1) ;
-        XSLFTextShape[] ph1 = slide1.getPlaceholders();
-        XSLFTextShape titlePlaceholder1 = ph1[0];
-        titlePlaceholder1.setText("This is a title");
-        XSLFTextShape subtitlePlaceholder1 = ph1[1];
-        subtitlePlaceholder1.setText("this is a subtitle");
-
-        XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
-        XSLFSlide slide2 = ppt.createSlide(layout2) ;
-        XSLFTextShape[] ph2 = slide2.getPlaceholders();
-        XSLFTextShape titlePlaceholder2 = ph2[0];
-        titlePlaceholder2.setText("This is a title");
-        XSLFTextShape bodyPlaceholder = ph2[1];
-        // we are going to add text by paragraphs. Clear the default placehoder text before that
-        bodyPlaceholder.clearText();
-        XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();
-        p1.setIndentLevel(0);
-        p1.addNewTextRun().setText("Level1 text");
-        XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();
-        p2.setIndentLevel(1);
-        p2.addNewTextRun().setText("Level2 text");
-        XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();
-        p3.setIndentLevel(2);
-        p3.addNewTextRun().setText("Level3 text");
-
-        FileOutputStream out = new FileOutputStream("slides.pptx");
-        ppt.write(out);
-        out.close();
-
-        ppt.close();
+        try {
+            // XSLFSlide#createSlide() with no arguments creates a blank slide
+            /*XSLFSlide blankSlide =*/ ppt.createSlide();
+    
+            
+            XSLFSlideMaster master = ppt.getSlideMasters().get(0);
+    
+            XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
+            XSLFSlide slide1 = ppt.createSlide(layout1) ;
+            XSLFTextShape[] ph1 = slide1.getPlaceholders();
+            XSLFTextShape titlePlaceholder1 = ph1[0];
+            titlePlaceholder1.setText("This is a title");
+            XSLFTextShape subtitlePlaceholder1 = ph1[1];
+            subtitlePlaceholder1.setText("this is a subtitle");
+    
+            XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
+            XSLFSlide slide2 = ppt.createSlide(layout2) ;
+            XSLFTextShape[] ph2 = slide2.getPlaceholders();
+            XSLFTextShape titlePlaceholder2 = ph2[0];
+            titlePlaceholder2.setText("This is a title");
+            XSLFTextShape bodyPlaceholder = ph2[1];
+            // we are going to add text by paragraphs. Clear the default placehoder text before that
+            bodyPlaceholder.clearText();
+            XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();
+            p1.setIndentLevel(0);
+            p1.addNewTextRun().setText("Level1 text");
+            XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();
+            p2.setIndentLevel(1);
+            p2.addNewTextRun().setText("Level2 text");
+            XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();
+            p3.setIndentLevel(2);
+            p3.addNewTextRun().setText("Level3 text");
+    
+            FileOutputStream out = new FileOutputStream("slides.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index 3f1402ae62983582623dc935cc1b1bab65625e3a..86ce72eba66d430ae233384d732084f75e08dde9 100644 (file)
@@ -26,60 +26,60 @@ import java.io.IOException;
 
 /**
  * Basic paragraph and text formatting
- *
- * @author Yegor Kozlov
  */
 public class Tutorial2 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        XSLFSlide slide1 = ppt.createSlide();
-        XSLFTextBox shape1 = slide1.createTextBox();
-        // initial height of the text box is 100 pt but
-        Rectangle anchor = new Rectangle(10, 100, 300, 100);
-        shape1.setAnchor(anchor);
-
-        XSLFTextParagraph p1 = shape1.addNewTextParagraph();
-        XSLFTextRun r1 = p1.addNewTextRun();
-        r1.setText("Paragraph Formatting");
-        r1.setFontSize(24d);
-        r1.setFontColor(new Color(85, 142, 213));
-
-        XSLFTextParagraph p2 = shape1.addNewTextParagraph();
-        // If spaceBefore >= 0, then space is a percentage of normal line height.
-        // If spaceBefore < 0, the absolute value of linespacing is the spacing in points
-        p2.setSpaceBefore(-20d); // 20 pt from the previous paragraph
-        p2.setSpaceAfter(300d); // 3 lines after the paragraph
-        XSLFTextRun r2 = p2.addNewTextRun();
-        r2.setText("Paragraph  properties apply to all text residing within the corresponding paragraph.");
-        r2.setFontSize(16d);
-
-        XSLFTextParagraph p3 = shape1.addNewTextParagraph();
-
-        XSLFTextRun r3 = p3.addNewTextRun();
-        r3.setText("Run Formatting");
-        r3.setFontSize(24d);
-        r3.setFontColor(new Color(85, 142, 213));
-
-        XSLFTextParagraph p4 = shape1.addNewTextParagraph();
-        p4.setSpaceBefore(-20d); // 20 pt from the previous paragraph
-        p4.setSpaceAfter(300d); // 3 lines after the paragraph
-        XSLFTextRun r4 = p4.addNewTextRun();
-        r4.setFontSize(16d);
-        r4.setText(
-                "Run level formatting is the most granular property level and allows " +
-                "for the specifying of all low level text properties. The text run is " +
-                "what all paragraphs are derived from and thus specifying various " +
-                "properties per run will allow for a diversely formatted text paragraph.");
-
-        // resize the shape to fit text
-        shape1.resizeToFitText();
-
-        FileOutputStream out = new FileOutputStream("text.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            XSLFSlide slide1 = ppt.createSlide();
+            XSLFTextBox shape1 = slide1.createTextBox();
+            // initial height of the text box is 100 pt but
+            Rectangle anchor = new Rectangle(10, 100, 300, 100);
+            shape1.setAnchor(anchor);
+    
+            XSLFTextParagraph p1 = shape1.addNewTextParagraph();
+            XSLFTextRun r1 = p1.addNewTextRun();
+            r1.setText("Paragraph Formatting");
+            r1.setFontSize(24d);
+            r1.setFontColor(new Color(85, 142, 213));
+    
+            XSLFTextParagraph p2 = shape1.addNewTextParagraph();
+            // If spaceBefore >= 0, then space is a percentage of normal line height.
+            // If spaceBefore < 0, the absolute value of linespacing is the spacing in points
+            p2.setSpaceBefore(-20d); // 20 pt from the previous paragraph
+            p2.setSpaceAfter(300d); // 3 lines after the paragraph
+            XSLFTextRun r2 = p2.addNewTextRun();
+            r2.setText("Paragraph  properties apply to all text residing within the corresponding paragraph.");
+            r2.setFontSize(16d);
+    
+            XSLFTextParagraph p3 = shape1.addNewTextParagraph();
+    
+            XSLFTextRun r3 = p3.addNewTextRun();
+            r3.setText("Run Formatting");
+            r3.setFontSize(24d);
+            r3.setFontColor(new Color(85, 142, 213));
+    
+            XSLFTextParagraph p4 = shape1.addNewTextParagraph();
+            p4.setSpaceBefore(-20d); // 20 pt from the previous paragraph
+            p4.setSpaceAfter(300d); // 3 lines after the paragraph
+            XSLFTextRun r4 = p4.addNewTextRun();
+            r4.setFontSize(16d);
+            r4.setText(
+                    "Run level formatting is the most granular property level and allows " +
+                    "for the specifying of all low level text properties. The text run is " +
+                    "what all paragraphs are derived from and thus specifying various " +
+                    "properties per run will allow for a diversely formatted text paragraph.");
+    
+            // resize the shape to fit text
+            shape1.resizeToFitText();
+    
+            FileOutputStream out = new FileOutputStream("text.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index 4cab79d886b6cabc5e04c3996b52d144548ca9d0..d561a9f5fdb7f74c7fbf47a07977f473d8f9bf52 100644 (file)
@@ -27,25 +27,25 @@ import org.apache.poi.sl.usermodel.Placeholder;
 
 /**
  * How to set slide title
- *
- * @author Yegor Kozlov
  */
 public class Tutorial3 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        XSLFSlide slide = ppt.createSlide();
-
-        XSLFTextShape titleShape = slide.createTextBox();
-        titleShape.setPlaceholder(Placeholder.TITLE);
-        titleShape.setText("This is a slide title");
-        titleShape.setAnchor(new Rectangle(50, 50, 400, 100));
-
-        FileOutputStream out = new FileOutputStream("title.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            XSLFSlide slide = ppt.createSlide();
+    
+            XSLFTextShape titleShape = slide.createTextBox();
+            titleShape.setPlaceholder(Placeholder.TITLE);
+            titleShape.setText("This is a slide title");
+            titleShape.setAnchor(new Rectangle(50, 50, 400, 100));
+    
+            FileOutputStream out = new FileOutputStream("title.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index 0cf0fd667db59096112b3e53f2ae6165647106aa..3db718d679c5b2b04d208049cb1aa371f5cbbc4a 100644 (file)
@@ -29,66 +29,64 @@ import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
 
 /**
  * PPTX Tables
- *
- * @author Yegor Kozlov
  */
 public class Tutorial4 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        // XSLFSlide#createSlide() with no arguments creates a blank slide
-        XSLFSlide slide = ppt.createSlide();
-
-        XSLFTable tbl = slide.createTable();
-        tbl.setAnchor(new Rectangle(50, 50, 450, 300));
-
-        int numColumns = 3;
-        int numRows = 5;
-        XSLFTableRow headerRow = tbl.addRow();
-        headerRow.setHeight(50);
-        // header
-        for(int i = 0; i < numColumns; i++) {
-            XSLFTableCell th = headerRow.addCell();
-            XSLFTextParagraph p = th.addNewTextParagraph();
-            p.setTextAlign(TextAlign.CENTER);
-            XSLFTextRun r = p.addNewTextRun();
-            r.setText("Header " + (i+1));
-            r.setBold(true);
-            r.setFontColor(Color.white);
-            th.setFillColor(new Color(79, 129, 189));
-            th.setBorderWidth(BorderEdge.bottom, 2.0);
-            th.setBorderColor(BorderEdge.bottom, Color.white);
-
-            tbl.setColumnWidth(i, 150);  // all columns are equally sized
-        }
-        
-        // rows
-        
-        for(int rownum = 0; rownum < numRows; rownum ++){
-            XSLFTableRow tr = tbl.addRow();
-            tr.setHeight(50);
+        try {
+            // XSLFSlide#createSlide() with no arguments creates a blank slide
+            XSLFSlide slide = ppt.createSlide();
+    
+            XSLFTable tbl = slide.createTable();
+            tbl.setAnchor(new Rectangle(50, 50, 450, 300));
+    
+            int numColumns = 3;
+            int numRows = 5;
+            XSLFTableRow headerRow = tbl.addRow();
+            headerRow.setHeight(50);
             // header
             for(int i = 0; i < numColumns; i++) {
-                XSLFTableCell cell = tr.addCell();
-                XSLFTextParagraph p = cell.addNewTextParagraph();
+                XSLFTableCell th = headerRow.addCell();
+                XSLFTextParagraph p = th.addNewTextParagraph();
+                p.setTextAlign(TextAlign.CENTER);
                 XSLFTextRun r = p.addNewTextRun();
-
-                r.setText("Cell " + (i+1));
-                if(rownum % 2 == 0)
-                    cell.setFillColor(new Color(208, 216, 232));
-                else
-                    cell.setFillColor(new Color(233, 247, 244));
-
+                r.setText("Header " + (i+1));
+                r.setBold(true);
+                r.setFontColor(Color.white);
+                th.setFillColor(new Color(79, 129, 189));
+                th.setBorderWidth(BorderEdge.bottom, 2.0);
+                th.setBorderColor(BorderEdge.bottom, Color.white);
+    
+                tbl.setColumnWidth(i, 150);  // all columns are equally sized
             }
             
+            // rows
+            
+            for(int rownum = 0; rownum < numRows; rownum ++){
+                XSLFTableRow tr = tbl.addRow();
+                tr.setHeight(50);
+                // header
+                for(int i = 0; i < numColumns; i++) {
+                    XSLFTableCell cell = tr.addCell();
+                    XSLFTextParagraph p = cell.addNewTextParagraph();
+                    XSLFTextRun r = p.addNewTextRun();
+    
+                    r.setText("Cell " + (i+1));
+                    if(rownum % 2 == 0)
+                        cell.setFillColor(new Color(208, 216, 232));
+                    else
+                        cell.setFillColor(new Color(233, 247, 244));
+    
+                }
+            }
+    
+            FileOutputStream out = new FileOutputStream("table.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
         }
-
-
-        FileOutputStream out = new FileOutputStream("table.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
     }
 }
index 988caee2e54a16a4e396188aec059e558c60c880..d5dbddf7326e816c15f275c4a4f2a41b7a4ae881 100644 (file)
@@ -27,25 +27,25 @@ import org.apache.poi.sl.usermodel.PictureData.PictureType;
 
 /**
  * Images
- *
- * @author Yegor Kozlov
  */
 public class Tutorial5 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        XSLFSlide slide = ppt.createSlide();
-
-        File img = new File(System.getProperty("POI.testdata.path", "test-data"), "slideshow/clock.jpg");
-        XSLFPictureData pictureData = ppt.addPicture(img, PictureType.PNG);
-
-        /*XSLFPictureShape shape =*/ slide.createPicture(pictureData);
-
-        FileOutputStream out = new FileOutputStream("images.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            XSLFSlide slide = ppt.createSlide();
+    
+            File img = new File(System.getProperty("POI.testdata.path", "test-data"), "slideshow/clock.jpg");
+            XSLFPictureData pictureData = ppt.addPicture(img, PictureType.PNG);
+    
+            /*XSLFPictureShape shape =*/ slide.createPicture(pictureData);
+    
+            FileOutputStream out = new FileOutputStream("images.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index 020089555d8799e57e9a3a0b7d83e2ef8d70387f..5eaff4541b9ae52aab57de5465fdf911c65a6596 100644 (file)
@@ -25,37 +25,37 @@ import java.io.IOException;
 
 /**
  * Hyperlinks
- *
- * @author Yegor Kozlov
  */
 public class Tutorial6 {
 
     public static void main(String[] args) throws IOException{
         XMLSlideShow ppt = new XMLSlideShow();
 
-        XSLFSlide slide1 = ppt.createSlide();
-        XSLFSlide slide2 = ppt.createSlide();
-
-        XSLFTextBox shape1 = slide1.createTextBox();
-        shape1.setAnchor(new Rectangle(50, 50, 200, 50));
-        XSLFTextRun r1 = shape1.addNewTextParagraph().addNewTextRun();
-        XSLFHyperlink link1 = r1.createHyperlink();
-        r1.setText("http://poi.apache.org"); // visible text
-        link1.setAddress("http://poi.apache.org");  // link address
-
-        XSLFTextBox shape2 = slide1.createTextBox();
-        shape2.setAnchor(new Rectangle(300, 50, 200, 50));
-        XSLFTextRun r2 = shape2.addNewTextParagraph().addNewTextRun();
-        XSLFHyperlink link2 = r2.createHyperlink();
-        r2.setText("Go to the second slide"); // visible text
-        link2.linkToSlide(slide2);  // link address
-
-
-
-        FileOutputStream out = new FileOutputStream("hyperlinks.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            XSLFSlide slide1 = ppt.createSlide();
+            XSLFSlide slide2 = ppt.createSlide();
+    
+            XSLFTextBox shape1 = slide1.createTextBox();
+            shape1.setAnchor(new Rectangle(50, 50, 200, 50));
+            XSLFTextRun r1 = shape1.addNewTextParagraph().addNewTextRun();
+            XSLFHyperlink link1 = r1.createHyperlink();
+            r1.setText("http://poi.apache.org"); // visible text
+            link1.setAddress("http://poi.apache.org");  // link address
+    
+            XSLFTextBox shape2 = slide1.createTextBox();
+            shape2.setAnchor(new Rectangle(300, 50, 200, 50));
+            XSLFTextRun r2 = shape2.addNewTextParagraph().addNewTextRun();
+            XSLFHyperlink link2 = r2.createHyperlink();
+            r2.setText("Go to the second slide"); // visible text
+            link2.linkToSlide(slide2);  // link address
+    
+    
+    
+            FileOutputStream out = new FileOutputStream("hyperlinks.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index 647fef75838df9c83c883bf533884130867dfcfc..4fee5bafea560b16fcf8ef78f43f6534f67134e7 100644 (file)
@@ -28,63 +28,63 @@ import org.apache.poi.sl.usermodel.AutoNumberingScheme;
 
 /**
  * Bullets and numbering
- *
- * @author Yegor Kozlov
  */
 public class Tutorial7 {
 
-    public static void main(String[] args) throws IOException{
+    public static void main(String[] args) throws IOException {
         XMLSlideShow ppt = new XMLSlideShow();
 
-        XSLFSlide slide = ppt.createSlide();
-        XSLFTextBox shape = slide.createTextBox();
-        shape.setAnchor(new Rectangle(50, 50, 400, 200));
-
-        XSLFTextParagraph p1 = shape.addNewTextParagraph();
-        p1.setIndentLevel(0);
-        p1.setBullet(true);
-        XSLFTextRun r1 = p1.addNewTextRun();
-        r1.setText("Bullet1");
-
-        XSLFTextParagraph p2 = shape.addNewTextParagraph();
-        // indentation before text
-        p2.setLeftMargin(60d);
-        // the bullet is set 40 pt before the text
-        p2.setIndent(-40d);
-        p2.setBullet(true);
-        // customize bullets
-        p2.setBulletFontColor(Color.red);
-        p2.setBulletFont("Wingdings");
-        p2.setBulletCharacter("\u0075");
-        p2.setIndentLevel(1);
-        XSLFTextRun r2 = p2.addNewTextRun();
-        r2.setText("Bullet2");
-
-        // the next three paragraphs form an auto-numbered list
-        XSLFTextParagraph p3 = shape.addNewTextParagraph();
-        p3.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 1);
-        p3.setIndentLevel(2);
-        XSLFTextRun r3 = p3.addNewTextRun();
-        r3.setText("Numbered List Item - 1");
-
-        XSLFTextParagraph p4 = shape.addNewTextParagraph();
-        p4.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 2);
-        p4.setIndentLevel(2);
-        XSLFTextRun r4 = p4.addNewTextRun();
-        r4.setText("Numbered List Item - 2");
-
-        XSLFTextParagraph p5 = shape.addNewTextParagraph();
-        p5.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 3);
-        p5.setIndentLevel(2);
-        XSLFTextRun r5 = p5.addNewTextRun();
-        r5.setText("Numbered List Item - 3");
-
-        shape.resizeToFitText();
-
-        FileOutputStream out = new FileOutputStream("list.pptx");
-        ppt.write(out);
-        out.close();
-        
-        ppt.close();
+        try {
+            XSLFSlide slide = ppt.createSlide();
+            XSLFTextBox shape = slide.createTextBox();
+            shape.setAnchor(new Rectangle(50, 50, 400, 200));
+    
+            XSLFTextParagraph p1 = shape.addNewTextParagraph();
+            p1.setIndentLevel(0);
+            p1.setBullet(true);
+            XSLFTextRun r1 = p1.addNewTextRun();
+            r1.setText("Bullet1");
+    
+            XSLFTextParagraph p2 = shape.addNewTextParagraph();
+            // indentation before text
+            p2.setLeftMargin(60d);
+            // the bullet is set 40 pt before the text
+            p2.setIndent(-40d);
+            p2.setBullet(true);
+            // customize bullets
+            p2.setBulletFontColor(Color.red);
+            p2.setBulletFont("Wingdings");
+            p2.setBulletCharacter("\u0075");
+            p2.setIndentLevel(1);
+            XSLFTextRun r2 = p2.addNewTextRun();
+            r2.setText("Bullet2");
+    
+            // the next three paragraphs form an auto-numbered list
+            XSLFTextParagraph p3 = shape.addNewTextParagraph();
+            p3.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 1);
+            p3.setIndentLevel(2);
+            XSLFTextRun r3 = p3.addNewTextRun();
+            r3.setText("Numbered List Item - 1");
+    
+            XSLFTextParagraph p4 = shape.addNewTextParagraph();
+            p4.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 2);
+            p4.setIndentLevel(2);
+            XSLFTextRun r4 = p4.addNewTextRun();
+            r4.setText("Numbered List Item - 2");
+    
+            XSLFTextParagraph p5 = shape.addNewTextParagraph();
+            p5.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 3);
+            p5.setIndentLevel(2);
+            XSLFTextRun r5 = p5.addNewTextRun();
+            r5.setText("Numbered List Item - 3");
+    
+            shape.resizeToFitText();
+    
+            FileOutputStream out = new FileOutputStream("list.pptx");
+            ppt.write(out);
+            out.close();
+        } finally {
+            ppt.close();
+        }
     }
 }
index f431d81254be00cff491d08d43b9ee497a4edbb9..e3d80cecf855de90ca4eccb47b96791869c865ad 100644 (file)
@@ -164,12 +164,15 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> {
 
     protected EmbeddedData extract(DirectoryNode dn) throws IOException {
         assert(canExtract(dn));
-        POIFSFileSystem dest = new POIFSFileSystem();
-        copyNodes(dn, dest.getRoot());
-        // start with a reasonable big size
         ByteArrayOutputStream bos = new ByteArrayOutputStream(20000);
-        dest.writeFilesystem(bos);
-        dest.close();
+        POIFSFileSystem dest = new POIFSFileSystem();
+        try {
+            copyNodes(dn, dest.getRoot());
+            // start with a reasonable big size
+            dest.writeFilesystem(bos);
+        } finally {
+            dest.close();
+        }
 
         return new EmbeddedData(dn.getName(), bos.toByteArray(), CONTENT_TYPE_BYTES);
     }
index 29d7ffce04b84a4e18d9e023d15b8b958400b64a..f00476cfc0f97fa5ad2d905e038fdb2f3e2d0924 100644 (file)
@@ -110,13 +110,15 @@ public class ExcelToFoConverter extends AbstractExcelConverter
     public static Document process( File xlsFile ) throws Exception
     {
         final HSSFWorkbook workbook = ExcelToFoUtils.loadXls( xlsFile );
-        ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter(
-                XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
-                        .newDocument() );
-        excelToHtmlConverter.processWorkbook( workbook );
-        Document doc = excelToHtmlConverter.getDocument();
-        workbook.close();
-        return doc;
+        try {
+            ExcelToFoConverter excelToHtmlConverter = new ExcelToFoConverter(
+                    XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
+                            .newDocument() );
+            excelToHtmlConverter.processWorkbook( workbook );
+            return excelToHtmlConverter.getDocument();
+        } finally {
+            workbook.close();
+        }
     }
 
     private final FoDocumentFacade foDocumentFacade;