poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1828472 - in /poi/site: publish/spreadsheet/quick-guide.html src/documentation/content/xdocs/spreadsheet/quick-guide.xml
Date Thu, 05 Apr 2018 19:57:25 GMT
Author: centic
Date: Thu Apr  5 19:57:25 2018
New Revision: 1828472

URL: http://svn.apache.org/viewvc?rev=1828472&view=rev
Log:
Bug 61898: Adjust quick-guide some more

Modified:
    poi/site/publish/spreadsheet/quick-guide.html
    poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml

Modified: poi/site/publish/spreadsheet/quick-guide.html
URL: http://svn.apache.org/viewvc/poi/site/publish/spreadsheet/quick-guide.html?rev=1828472&r1=1828471&r2=1828472&view=diff
==============================================================================
--- poi/site/publish/spreadsheet/quick-guide.html (original)
+++ poi/site/publish/spreadsheet/quick-guide.html Thu Apr  5 19:57:25 2018
@@ -406,14 +406,16 @@ if (VERSION > 3) {
                     
 <pre class="code">
     Workbook wb = new HSSFWorkbook();
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    ...
+    try  (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
 
     Workbook wb = new XSSFWorkbook();
-    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    ...
+    try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
                 
@@ -446,9 +448,9 @@ if (VERSION > 3) {
     String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns
" O'Brien's sales   "
     Sheet sheet3 = wb.createSheet(safeName);
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                      </pre>
                 
                 
@@ -478,9 +480,9 @@ if (VERSION > 3) {
     row.createCell(3).setCellValue(true);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
                 
@@ -521,9 +523,9 @@ if (VERSION > 3) {
     cell.setCellStyle(cellStyle);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
                 
@@ -546,9 +548,9 @@ if (VERSION > 3) {
     row.createCell(5).setCellType(CellType.ERROR);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
 
@@ -616,7 +618,7 @@ if (VERSION > 3) {
 </div>
                     
 <pre class="code">
-    public static void main(String[] args)  throws Exception {
+    public static void main(String[] args) throws Exception {
         Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
 
         Sheet sheet = wb.createSheet();
@@ -632,10 +634,11 @@ if (VERSION > 3) {
         createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
 
         // Write the output to a file
-        FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
-        wb.write(fileOut);
-        fileOut.close();
+        try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) {
+            wb.write(fileOut);
+        }
 
+        wb.close();
     }
 
     /**
@@ -689,9 +692,11 @@ if (VERSION > 3) {
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+                        
+    wb.close();
                     </pre>
                 
                 
@@ -844,13 +849,15 @@ if (VERSION > 3) {
 					ExcelExtractor class should provide all you need.</p>
 					
 <pre class="code">
-    InputStream inp = new FileInputStream("workbook.xls");
-    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
-    ExcelExtractor extractor = new ExcelExtractor(wb);
-
-    extractor.setFormulasNotResults(true);
-    extractor.setIncludeSheetNames(false);
-    String text = extractor.getText();
+    try (InputStream inp = new FileInputStream("workbook.xls")) {
+        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
+        ExcelExtractor extractor = new ExcelExtractor(wb);
+    
+        extractor.setFormulasNotResults(true);
+        extractor.setIncludeSheetNames(false);
+        String text = extractor.getText();
+        wb.close();
+    }
 					</pre>
 					
 <p>For very fancy text extraction, XLS to CSV etc, 
@@ -891,9 +898,11 @@ if (VERSION > 3) {
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
                 
@@ -920,9 +929,11 @@ if (VERSION > 3) {
     ));
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
                 
@@ -957,9 +968,11 @@ if (VERSION > 3) {
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
   </pre>
 
 <p>
@@ -990,7 +1003,6 @@ Examples:
 </p>
 
 <pre class="code">
-
     CellStyle style = workbook.createCellStyle();
     Font font = workbook.createFont();
     font.setBoldweight(Font.BOLDWEIGHT_BOLD);
@@ -1037,9 +1049,9 @@ Examples:
     cell.setCellStyle(style);
 
     //save with the default palette
-    FileOutputStream out = new FileOutputStream("default_palette.xls");
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream("default_palette.xls")) {
+        wb.write(out);
+    }
 
     //now, let's replace RED and LIME in the palette
     // with a more attractive combination
@@ -1062,9 +1074,9 @@ Examples:
     //save with the modified palette
     // note that wherever we have previously used RED or LIME, the
     // new colors magically appear
-    out = new FileOutputStream("modified_palette.xls");
-    wb.write(out);
-    out.close();
+    try (out = new FileOutputStream("modified_palette.xls")) {
+        wb.write(out);
+    }
                     </pre>
                     
 <p>
@@ -1092,22 +1104,23 @@ Examples:
 </div>
                     
 <pre class="code">
-    InputStream inp = new FileInputStream("workbook.xls");
+    try (InputStream inp = new FileInputStream("workbook.xls")) {
     //InputStream inp = new FileInputStream("workbook.xlsx");
-
-    Workbook wb = WorkbookFactory.create(inp);
-    Sheet sheet = wb.getSheetAt(0);
-    Row row = sheet.getRow(2);
-    Cell cell = row.getCell(3);
-    if (cell == null)
-        cell = row.createCell(3);
-    cell.setCellType(CellType.STRING);
-    cell.setCellValue("a test");
-
-    // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    
+        Workbook wb = WorkbookFactory.create(inp);
+        Sheet sheet = wb.getSheetAt(0);
+        Row row = sheet.getRow(2);
+        Cell cell = row.getCell(3);
+        if (cell == null)
+            cell = row.createCell(3);
+        cell.setCellType(CellType.STRING);
+        cell.setCellValue("a test");
+    
+        // Write the output to a file
+        try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+            wb.write(fileOut);
+        }
+    }
                     </pre>
                 
                 
@@ -1137,9 +1150,11 @@ Examples:
     //adjust column width to fit the content
     sheet.autoSizeColumn(2);
 
-    FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                   </pre>
                 
                 
@@ -1174,9 +1189,11 @@ Examples:
     style.setDataFormat(format.getFormat("#,##0.0000"));
     cell.setCellStyle(style);
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
                 
@@ -1200,9 +1217,11 @@ Examples:
 
     // Create various cells and rows for spreadsheet.
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
                 
@@ -1228,9 +1247,11 @@ Examples:
             0  //end row
     );
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
 
@@ -1253,9 +1274,11 @@ Examples:
 
     // Create various cells and rows for spreadsheet.
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </pre>
                 
 
@@ -1304,9 +1327,11 @@ Examples:
     CellUtil.setAlignment(cell2, HorizontalAlignment.CENTER);
 
     // Write out the workbook
-    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
-    wb.write( fileOut );
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream( "workbook.xls" )) {
+        wb.write( fileOut );
+    }
+
+    wb.close();
                     </pre>
                 
 
@@ -1441,9 +1466,9 @@ Examples:
     // Create a split with the lower left side being the active quadrant
     sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
 
@@ -1485,9 +1510,9 @@ Examples:
     // Set the columns to repeat from column A to C on the second sheet
     sheet2.setRepeatingColumns(CellRangeAddress.valueOf("A:C"));
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
                 
@@ -1512,9 +1537,9 @@ Examples:
     header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
                     HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font
and size 16");
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
                 
@@ -1572,9 +1597,9 @@ Examples:
     prop.removeDifferentFirstPage(); // This does not remove first page headers or footers
     prop.removeDifferentEvenOdd(); // This does not remove even headers or footers
     
-    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
+        wb.write(fileOut);
+    }
                     </pre>
                 
 
@@ -1881,9 +1906,9 @@ Examples:
     sheet1.groupColumn( 9, 12 );
     sheet1.groupColumn( 10, 11 );
 
-    FileOutputStream fileOut = new FileOutputStream(filename);
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream(filename)) {
+        wb.write(fileOut);
+    }
                     </pre>
                     
 <p>
@@ -1964,9 +1989,9 @@ Examples:
     //save workbook
     String file = "picture.xls";
     if(wb instanceof XSSFWorkbook) file += "x";
-    FileOutputStream fileOut = new FileOutputStream(file);
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream(file)) {
+        wb.write(fileOut);
+    }
         </pre>
         
 <div class="frame warning">
@@ -1986,9 +2011,9 @@ Examples:
         String ext = pict.suggestFileExtension();
         byte[] data = pict.getData();
         if (ext.equals("jpeg")){
-          FileOutputStream out = new FileOutputStream("pict.jpg");
-          out.write(data);
-          out.close();
+          try (OutputStream out = new FileOutputStream("pict.jpg")) {
+            out.write(data);
+          }
         }
     }
       </pre>
@@ -2170,9 +2195,11 @@ Examples:
 
     String fname = "comment-xssf.xls";
     if(wb instanceof XSSFWorkbook) fname += "x";
-    FileOutputStream out = new FileOutputStream(fname);
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream(fname)) {
+        wb.write(out);
+    }
+
+    wb.close();
         </pre>
          
 <p>
@@ -2348,9 +2375,11 @@ Examples:
     cell.setHyperlink(link2);
     cell.setCellStyle(hlink_style);
 
-    FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream("hyperinks.xlsx")) {
+        wb.write(out);
+    }
+
+    wb.close();
         </pre>
      
      

Modified: poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml?rev=1828472&r1=1828471&r2=1828472&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml (original)
+++ poi/site/src/documentation/content/xdocs/spreadsheet/quick-guide.xml Thu Apr  5 19:57:25
2018
@@ -86,14 +86,16 @@
                 <section><title>New Workbook</title>
                     <source>
     Workbook wb = new HSSFWorkbook();
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    ...
+    try  (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
 
     Workbook wb = new XSSFWorkbook();
-    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    ...
+    try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
                 <anchor id="NewSheet"/>
@@ -120,9 +122,9 @@
     String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns
" O'Brien's sales   "
     Sheet sheet3 = wb.createSheet(safeName);
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                      </source>
                 </section>
                 <anchor id="CreateCells"/>
@@ -146,9 +148,9 @@
     row.createCell(3).setCellValue(true);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
                 <anchor id="CreateDateCells"/>
@@ -183,9 +185,9 @@
     cell.setCellStyle(cellStyle);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
                 <anchor id="CellTypes"/>
@@ -202,9 +204,9 @@
     row.createCell(5).setCellType(CellType.ERROR);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
 
@@ -256,7 +258,7 @@
                 <anchor id="Alignment"/>
                 <section><title>Demonstrates various alignment options</title>
                     <source>
-    public static void main(String[] args)  throws Exception {
+    public static void main(String[] args) throws Exception {
         Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
 
         Sheet sheet = wb.createSheet();
@@ -272,10 +274,11 @@
         createCell(wb, row, 6, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);
 
         // Write the output to a file
-        FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
-        wb.write(fileOut);
-        fileOut.close();
+        try (OutputStream fileOut = new FileOutputStream("xssf-align.xlsx")) {
+            wb.write(fileOut);
+        }
 
+        wb.close();
     }
 
     /**
@@ -323,9 +326,11 @@
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+                        
+    wb.close();
                     </source>
                 </section>
                 <anchor id="Iterator"/>
@@ -447,13 +452,15 @@
 					<p>For most text extraction requirements, the standard
 					ExcelExtractor class should provide all you need.</p>
 					<source>
-    InputStream inp = new FileInputStream("workbook.xls");
-    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
-    ExcelExtractor extractor = new ExcelExtractor(wb);
-
-    extractor.setFormulasNotResults(true);
-    extractor.setIncludeSheetNames(false);
-    String text = extractor.getText();
+    try (InputStream inp = new FileInputStream("workbook.xls")) {
+        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
+        ExcelExtractor extractor = new ExcelExtractor(wb);
+    
+        extractor.setFormulasNotResults(true);
+        extractor.setIncludeSheetNames(false);
+        String text = extractor.getText();
+        wb.close();
+    }
 					</source>
 					<p>For very fancy text extraction, XLS to CSV etc, 
 					take a look at
@@ -486,9 +493,11 @@
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
                 <anchor id="MergedCells"/>
@@ -509,9 +518,11 @@
     ));
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
                 <anchor id="WorkingWithFonts"/>
@@ -540,9 +551,11 @@
     cell.setCellStyle(style);
 
     // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
   </source>
 <p>
   Note, the maximum number of unique fonts in a workbook is limited to 32767. You should
re-use fonts in your applications instead of
@@ -564,7 +577,6 @@ Examples:
 </source>
 <p><strong>Correct:</strong></p>
 <source>
-
     CellStyle style = workbook.createCellStyle();
     Font font = workbook.createFont();
     font.setBoldweight(Font.BOLDWEIGHT_BOLD);
@@ -602,9 +614,9 @@ Examples:
     cell.setCellStyle(style);
 
     //save with the default palette
-    FileOutputStream out = new FileOutputStream("default_palette.xls");
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream("default_palette.xls")) {
+        wb.write(out);
+    }
 
     //now, let's replace RED and LIME in the palette
     // with a more attractive combination
@@ -627,9 +639,9 @@ Examples:
     //save with the modified palette
     // note that wherever we have previously used RED or LIME, the
     // new colors magically appear
-    out = new FileOutputStream("modified_palette.xls");
-    wb.write(out);
-    out.close();
+    try (out = new FileOutputStream("modified_palette.xls")) {
+        wb.write(out);
+    }
                     </source>
                     <p><strong>XSSF:</strong></p>
                     <source>
@@ -647,22 +659,23 @@ Examples:
                 <anchor id="ReadWriteWorkbook"/>
                 <section><title>Reading and Rewriting Workbooks</title>
                     <source>
-    InputStream inp = new FileInputStream("workbook.xls");
+    try (InputStream inp = new FileInputStream("workbook.xls")) {
     //InputStream inp = new FileInputStream("workbook.xlsx");
-
-    Workbook wb = WorkbookFactory.create(inp);
-    Sheet sheet = wb.getSheetAt(0);
-    Row row = sheet.getRow(2);
-    Cell cell = row.getCell(3);
-    if (cell == null)
-        cell = row.createCell(3);
-    cell.setCellType(CellType.STRING);
-    cell.setCellValue("a test");
-
-    // Write the output to a file
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    
+        Workbook wb = WorkbookFactory.create(inp);
+        Sheet sheet = wb.getSheetAt(0);
+        Row row = sheet.getRow(2);
+        Cell cell = row.getCell(3);
+        if (cell == null)
+            cell = row.createCell(3);
+        cell.setCellType(CellType.STRING);
+        cell.setCellValue("a test");
+    
+        // Write the output to a file
+        try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+            wb.write(fileOut);
+        }
+    }
                     </source>
                 </section>
                 <anchor id="NewLinesInCells"/>
@@ -686,9 +699,11 @@ Examples:
     //adjust column width to fit the content
     sheet.autoSizeColumn(2);
 
-    FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                   </source>
                 </section>
                 <anchor id="DataFormats"/>
@@ -717,9 +732,11 @@ Examples:
     style.setDataFormat(format.getFormat("#,##0.0000"));
     cell.setCellStyle(style);
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
                 <anchor id="FitTo"/>
@@ -737,9 +754,11 @@ Examples:
 
     // Create various cells and rows for spreadsheet.
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
                 <anchor id="PrintArea2"/>
@@ -759,9 +778,11 @@ Examples:
             0  //end row
     );
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
 
@@ -778,9 +799,11 @@ Examples:
 
     // Create various cells and rows for spreadsheet.
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
+
+    wb.close();
                     </source>
                 </section>
 
@@ -822,9 +845,11 @@ Examples:
     CellUtil.setAlignment(cell2, HorizontalAlignment.CENTER);
 
     // Write out the workbook
-    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
-    wb.write( fileOut );
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream( "workbook.xls" )) {
+        wb.write( fileOut );
+    }
+
+    wb.close();
                     </source>
                 </section>
 
@@ -925,9 +950,9 @@ Examples:
     // Create a split with the lower left side being the active quadrant
     sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
 
@@ -961,9 +986,9 @@ Examples:
     // Set the columns to repeat from column A to C on the second sheet
     sheet2.setRepeatingColumns(CellRangeAddress.valueOf("A:C"));
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
                 <anchor id="HeaderFooter"/>
@@ -981,9 +1006,9 @@ Examples:
     header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
                     HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font
and size 16");
 
-    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
                 <anchor id="XSSFHeaderFooter"/>
@@ -1033,9 +1058,9 @@ Examples:
     prop.removeDifferentFirstPage(); // This does not remove first page headers or footers
     prop.removeDifferentEvenOdd(); // This does not remove even headers or footers
     
-    FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
+        wb.write(fileOut);
+    }
                     </source>
                 </section>
 
@@ -1263,9 +1288,9 @@ Examples:
     sheet1.groupColumn( 9, 12 );
     sheet1.groupColumn( 10, 11 );
 
-    FileOutputStream fileOut = new FileOutputStream(filename);
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream(filename)) {
+        wb.write(fileOut);
+    }
                     </source>
                     <p>
                         To collapse (or expand) an outline use the following calls:
@@ -1330,9 +1355,9 @@ Examples:
     //save workbook
     String file = "picture.xls";
     if(wb instanceof XSSFWorkbook) file += "x";
-    FileOutputStream fileOut = new FileOutputStream(file);
-    wb.write(fileOut);
-    fileOut.close();
+    try (OutputStream fileOut = new FileOutputStream(file)) {
+        wb.write(fileOut);
+    }
         </source>
         <warning>
           Picture.resize() works only for JPEG and PNG. Other formats are not yet supported.
@@ -1346,9 +1371,9 @@ Examples:
         String ext = pict.suggestFileExtension();
         byte[] data = pict.getData();
         if (ext.equals("jpeg")){
-          FileOutputStream out = new FileOutputStream("pict.jpg");
-          out.write(data);
-          out.close();
+          try (OutputStream out = new FileOutputStream("pict.jpg")) {
+            out.write(data);
+          }
         }
     }
       </source>
@@ -1508,9 +1533,11 @@ Examples:
 
     String fname = "comment-xssf.xls";
     if(wb instanceof XSSFWorkbook) fname += "x";
-    FileOutputStream out = new FileOutputStream(fname);
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream(fname)) {
+        wb.write(out);
+    }
+
+    wb.close();
         </source>
          <p>
   Reading cell comments
@@ -1657,9 +1684,11 @@ Examples:
     cell.setHyperlink(link2);
     cell.setCellStyle(hlink_style);
 
-    FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
-    wb.write(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream("hyperinks.xlsx")) {
+        wb.write(out);
+    }
+
+    wb.close();
         </source>
      </section>
      <anchor id="Validation"/>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message