diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2016-12-18 22:03:31 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2016-12-18 22:03:31 +0000 |
commit | b8bfe77718d26887ddccc7be4f5332aafad9e287 (patch) | |
tree | df9350982cab011e51bf01db95def4b7a2e0fb61 /src/examples | |
parent | 89699818195ddadb13f5e00961fd87c5d0046cb5 (diff) | |
download | poi-b8bfe77718d26887ddccc7be4f5332aafad9e287.tar.gz poi-b8bfe77718d26887ddccc7be4f5332aafad9e287.zip |
SonarQube fixes - close resources
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1774969 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
20 files changed, 289 insertions, 266 deletions
diff --git a/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java b/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java index 3c706e3e73..1997200884 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java @@ -18,6 +18,7 @@ package org.apache.poi.hslf.examples; import java.io.FileOutputStream; +import java.io.IOException; import org.apache.poi.hslf.usermodel.HSLFSlide; import org.apache.poi.hslf.usermodel.HSLFSlideShow; @@ -27,12 +28,10 @@ import org.apache.poi.hslf.usermodel.HSLFTextParagraph; /** * How to create a single-level bulleted list * and change some of the bullet attributes - * - * @author Yegor Kozlov */ public final class BulletsDemo { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { HSLFSlideShow ppt = new HSLFSlideShow(); @@ -58,5 +57,7 @@ public final class BulletsDemo { FileOutputStream out = new FileOutputStream("bullets.ppt"); ppt.write(out); out.close(); + + ppt.close(); } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java b/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java index b37aed2df6..3ef5a46ab3 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java @@ -44,6 +44,7 @@ public abstract class HeadersFootersDemo { ppt.write(out); out.close(); + ppt.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Borders.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Borders.java index d29635004a..9158b0ccff 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Borders.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Borders.java @@ -17,16 +17,19 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.hssf.util.HSSFColor; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.BorderStyle; + /** * Demonstrates how to create borders around cells. - * - * @author Glen Stampoultzis (glens at apache.org) */ public class Borders { public static void main(String[] args) throws IOException { @@ -42,13 +45,13 @@ public class Borders { // Style the cell with borders all around. HSSFCellStyle style = wb.createCellStyle(); - style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); - style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(HSSFColor.GREEN.index); - style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(HSSFColor.BLUE.index); - style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED); + style.setBorderTop(BorderStyle.MEDIUM_DASHED); style.setTopBorderColor(HSSFColor.ORANGE.index); cell.setCellStyle(style); @@ -56,5 +59,7 @@ public class Borders { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java index c3a213d582..4109a87f48 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellComments.java @@ -17,11 +17,19 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; +import org.apache.poi.hssf.usermodel.HSSFComment; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFPatriarch; +import org.apache.poi.hssf.usermodel.HSSFRichTextString; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; -import java.io.*; - /** * Demonstrates how to work with excel cell comments. * @@ -29,8 +37,6 @@ import java.io.*; * 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> - * - * @author Yegor Kozlov */ public class CellComments { @@ -74,7 +80,7 @@ public class CellComments { HSSFFont font = wb.createFont(); font.setFontName("Arial"); font.setFontHeightInPoints((short)10); - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + font.setBold(true); font.setColor(HSSFColor.RED.index); string.applyFont(font); @@ -94,5 +100,7 @@ public class CellComments { FileOutputStream out = new FileOutputStream("poi_comment.xls"); wb.write(out); out.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java index 3e31d11f26..80ced95ac0 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CellTypes.java @@ -41,5 +41,7 @@ public class CellTypes { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateDateCells.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateDateCells.java index 746fd536b8..8afe085f1e 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateDateCells.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/CreateDateCells.java @@ -17,18 +17,21 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; - import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFDataFormat; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + /** * An example on how to cells with dates. The important thing to note * about dates is that they are really normal numeric cells that are * formatted specially. - * - * @author Glen Stampoultzis (glens at apache.org) */ public class CreateDateCells { public static void main(String[] args) throws IOException { @@ -54,5 +57,7 @@ public class CreateDateCells { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/FrillsAndFills.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/FrillsAndFills.java index 02b7cb3e39..40be15e014 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/FrillsAndFills.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/FrillsAndFills.java @@ -17,16 +17,19 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.hssf.util.HSSFColor; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.FillPatternType; + /** * Shows how to use various fills. - * - * @author Glen Stampoultzis (glens at apache.org) */ public class FrillsAndFills { public static void main(String[] args) throws IOException { @@ -39,7 +42,7 @@ public class FrillsAndFills { // Aqua background HSSFCellStyle style = wb.createCellStyle(); style.setFillBackgroundColor(HSSFColor.AQUA.index); - style.setFillPattern(HSSFCellStyle.BIG_SPOTS); + style.setFillPattern(FillPatternType.BIG_SPOTS); HSSFCell cell = row.createCell(1); cell.setCellValue("X"); cell.setCellStyle(style); @@ -47,7 +50,7 @@ public class FrillsAndFills { // Orange "foreground", foreground being the fill foreground not the font color. style = wb.createCellStyle(); style.setFillForegroundColor(HSSFColor.ORANGE.index); - style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell = row.createCell(2); cell.setCellValue("X"); cell.setCellStyle(style); @@ -56,5 +59,7 @@ public class FrillsAndFills { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java index 2f67f7f0e5..632cd5585f 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java @@ -31,6 +31,7 @@ import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.util.CellRangeAddress; /** @@ -77,7 +78,7 @@ public final class HSSFReadWrite { cs.setFont(f); cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cs2.setBorderBottom(BorderStyle.THIN); - cs2.setFillPattern((short) 1); // fill w fg + cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND); cs2.setFillForegroundColor((short) 0xA); cs2.setFont(f2); wb.setSheetName(0, "HSSF Test"); @@ -128,9 +129,8 @@ public final class HSSFReadWrite { wb.write(out); } finally { out.close(); + wb.close(); } - - wb.close(); } /** diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HyperlinkFormula.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HyperlinkFormula.java index 148b2a58b0..003c09f688 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HyperlinkFormula.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HyperlinkFormula.java @@ -28,8 +28,6 @@ import org.apache.poi.ss.usermodel.CellType; /** * Test if hyperlink formula, with url that got more than 127 characters, works - * - * @author Bernard Chesnoy */ public class HyperlinkFormula { public static void main(String[] args) throws IOException { @@ -44,5 +42,6 @@ public class HyperlinkFormula { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java index c1ccad9100..adad7da363 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java @@ -29,6 +29,7 @@ import org.apache.poi.hssf.usermodel.HSSFHyperlink; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.Font; /** * Demonstrates how to create hyperlinks. @@ -45,7 +46,7 @@ public class Hyperlinks { //by default hyperlinks are blue and underlined HSSFCellStyle hlink_style = wb.createCellStyle(); HSSFFont hlink_font = wb.createFont(); - hlink_font.setUnderline(HSSFFont.U_SINGLE); + hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(HSSFColor.BLUE.index); hlink_style.setFont(hlink_font); @@ -93,5 +94,6 @@ public class Hyperlinks { FileOutputStream out = new FileOutputStream("hssf-links.xls"); wb.write(out); out.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java index 31e5215b8e..09d08dd257 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/MergedCells.java @@ -17,16 +17,17 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddress; - -import java.io.IOException; import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.util.CellRangeAddress; /** * An example of how to merge regions of cells. - * - * @author Glen Stampoultzis (glens at apache.org) */ public class MergedCells { public static void main(String[] args) throws IOException { @@ -43,5 +44,6 @@ public class MergedCells { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewLinesInCells.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewLinesInCells.java index 018556f991..00f60c4daf 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewLinesInCells.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewLinesInCells.java @@ -17,17 +17,19 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.ss.usermodel.CellType; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.CellType; + /** * Demonstrates how to use newlines in cells. - * - * @author Glen Stampoultzis (glens at apache.org) - * @author Fauzia Lala <fauzia.lala at wcom.com> */ public class NewLinesInCells { public static void main( String[] args ) throws IOException { @@ -56,5 +58,6 @@ public class NewLinesInCells { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewWorkbook.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewWorkbook.java index c1614e6c1a..e6e0e4137e 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewWorkbook.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewWorkbook.java @@ -19,24 +19,20 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + /** * This example creates a new blank workbook. This workbook will contain a single blank sheet. - * - * @author Glen Stampoultzis (glens at apache.org) */ -public class NewWorkbook -{ - public static void main(String[] args) - throws IOException - { +public class NewWorkbook { + public static void main(String[] args) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/SplitAndFreezePanes.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/SplitAndFreezePanes.java index 6e93a317f0..85cba2eb98 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/SplitAndFreezePanes.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/SplitAndFreezePanes.java @@ -19,20 +19,15 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.usermodel.HSSFSheet; - -import java.io.IOException; import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Sheet; -/** - * @author Glen Stampoultzis (glens at apache.org) - */ -public class SplitAndFreezePanes -{ - public static void main(String[] args) - throws IOException - { +public class SplitAndFreezePanes { + public static void main(String[] args) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("new sheet"); HSSFSheet sheet2 = wb.createSheet("second sheet"); @@ -46,10 +41,11 @@ public class SplitAndFreezePanes // Freeze the columns and rows (forget about scrolling position of the lower right quadrant). sheet3.createFreezePane( 2, 2 ); // Create a split with the lower left side being the active quadrant - sheet4.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT ); + sheet4.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT ); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/WorkingWithFonts.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/WorkingWithFonts.java index 6cb15941f2..d3c674a756 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/WorkingWithFonts.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/WorkingWithFonts.java @@ -17,15 +17,18 @@ package org.apache.poi.hssf.usermodel.examples; -import org.apache.poi.hssf.usermodel.*; - import java.io.FileOutputStream; import java.io.IOException; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + /** * Demonstrates how to create and use fonts. - * - * @author Glen Stampoultzis (glens at apache.org) */ public class WorkingWithFonts { public static void main(String[] args) throws IOException { @@ -55,5 +58,6 @@ public class WorkingWithFonts { FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + wb.close(); } } diff --git a/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java b/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java index 5eb10c2429..a7ef6d2ba2 100644 --- a/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java +++ b/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java @@ -19,14 +19,13 @@ package org.apache.poi.ss.examples;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.ss.usermodel.Drawing;
@@ -813,50 +812,28 @@ public class AddDimensionedImage { *
* @param args the command line arguments
*/
- public static void main(String[] args) {
+ public static void main(String[] args) throws IOException {
String imageFile = null;
String outputFile = null;
FileOutputStream fos = null;
Workbook workbook = null;
Sheet sheet = null;
- try {
- if(args.length < 2){
- System.err.println("Usage: AddDimensionedImage imageFile outputFile");
- return;
- }
- workbook = new HSSFWorkbook(); // OR XSSFWorkbook
- sheet = workbook.createSheet("Picture Test");
- imageFile = args[0];
- outputFile = args[1];
- new AddDimensionedImage().addImageToSheet("B5", sheet, sheet.createDrawingPatriarch(),
- new File(imageFile).toURI().toURL(), 100, 40,
- AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
- fos = new FileOutputStream(outputFile);
- workbook.write(fos);
- }
- catch(FileNotFoundException fnfEx) {
- System.out.println("Caught an: " + fnfEx.getClass().getName());
- System.out.println("Message: " + fnfEx.getMessage());
- System.out.println("Stacktrace follows...........");
- fnfEx.printStackTrace(System.out);
- }
- catch(IOException ioEx) {
- System.out.println("Caught an: " + ioEx.getClass().getName());
- System.out.println("Message: " + ioEx.getMessage());
- System.out.println("Stacktrace follows...........");
- ioEx.printStackTrace(System.out);
- }
- finally {
- if(fos != null) {
- try {
- fos.close();
- fos = null;
- }
- catch(IOException ioEx) {
- // I G N O R E
- }
- }
- }
+
+ if(args.length < 2){
+ System.err.println("Usage: AddDimensionedImage imageFile outputFile");
+ return;
+ }
+ workbook = new HSSFWorkbook(); // OR XSSFWorkbook
+ sheet = workbook.createSheet("Picture Test");
+ imageFile = args[0];
+ outputFile = args[1];
+ new AddDimensionedImage().addImageToSheet("B5", sheet, sheet.createDrawingPatriarch(),
+ new File(imageFile).toURI().toURL(), 100, 40,
+ AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
+ fos = new FileOutputStream(outputFile);
+ workbook.write(fos);
+ fos.close();
+ workbook.close();
}
/**
diff --git a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java index 7358edf682..eba1e219f2 100644 --- a/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java +++ b/src/examples/src/org/apache/poi/ss/examples/ConditionalFormats.java @@ -19,16 +19,30 @@ package org.apache.poi.ss.examples;
-import org.apache.poi.hssf.usermodel.*;
-import org.apache.poi.ss.usermodel.*;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BuiltinFormats;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.ColorScaleFormatting;
+import org.apache.poi.ss.usermodel.ComparisonOperator;
+import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
+import org.apache.poi.ss.usermodel.DataBarFormatting;
+import org.apache.poi.ss.usermodel.ExtendedColor;
+import org.apache.poi.ss.usermodel.FontFormatting;
+import org.apache.poi.ss.usermodel.IconMultiStateFormatting;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.PatternFormatting;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
/**
* Excel Conditional Formatting -- Examples
*
@@ -42,8 +56,11 @@ public class ConditionalFormats { public static void main(String[] args) throws IOException {
Workbook wb;
- if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
- else wb = new XSSFWorkbook();
+ if(args.length > 0 && args[0].equals("-xls")) {
+ wb = new HSSFWorkbook();
+ } else {
+ wb = new XSSFWorkbook();
+ }
sameCell(wb.createSheet("Same Cell"));
multiCell(wb.createSheet("MultiCell"));
@@ -61,11 +78,14 @@ public class ConditionalFormats { // Write the output to a file
String file = "cf-poi.xls";
- if(wb instanceof XSSFWorkbook) file += "x";
+ if(wb instanceof XSSFWorkbook) {
+ file += "x";
+ }
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
System.out.println("Generated: " + file);
+ wb.close();
}
/**
@@ -157,11 +177,21 @@ public class ConditionalFormats { Row r = sheet.createRow(i);
r.createCell(0).setCellValue("This is row " + rn + " (" + i + ")");
String str = "";
- if (rn%2 == 0) str = str + "even ";
- if (rn%3 == 0) str = str + "x3 ";
- if (rn%5 == 0) str = str + "x5 ";
- if (rn%10 == 0) str = str + "x10 ";
- if (str.length() == 0) str = "nothing special...";
+ if (rn%2 == 0) {
+ str = str + "even ";
+ }
+ if (rn%3 == 0) {
+ str = str + "x3 ";
+ }
+ if (rn%5 == 0) {
+ str = str + "x5 ";
+ }
+ if (rn%10 == 0) {
+ str = str + "x10 ";
+ }
+ if (str.length() == 0) {
+ str = "nothing special...";
+ }
r.createCell(1).setCellValue("It is " + str);
}
sheet.autoSizeColumn(0);
@@ -353,7 +383,9 @@ public class ConditionalFormats { sheet.createRow(2).createCell(0).setCellFormula("A2+1");
sheet.createRow(3).createCell(0).setCellFormula("A3+1");
- for(int rownum = 1; rownum <= 3; rownum++) sheet.getRow(rownum).getCell(0).setCellStyle(style);
+ for(int rownum = 1; rownum <= 3; rownum++) {
+ sheet.getRow(rownum).getCell(0).setCellStyle(style);
+ }
SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
diff --git a/src/examples/src/org/apache/poi/ss/examples/LinkedDropDownLists.java b/src/examples/src/org/apache/poi/ss/examples/LinkedDropDownLists.java index b0efc4f0ce..7b672b792c 100644 --- a/src/examples/src/org/apache/poi/ss/examples/LinkedDropDownLists.java +++ b/src/examples/src/org/apache/poi/ss/examples/LinkedDropDownLists.java @@ -16,11 +16,20 @@ ==================================================================== */
package org.apache.poi.ss.examples;
-import java.io.*;
-import org.apache.poi.xssf.usermodel.*;
-import org.apache.poi.hssf.usermodel.*;
-import org.apache.poi.ss.usermodel.*;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.DataValidation;
+import org.apache.poi.ss.usermodel.DataValidationConstraint;
+import org.apache.poi.ss.usermodel.DataValidationHelper;
+import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Demonstrates one technique that may be used to create linked or dependent
@@ -56,78 +65,52 @@ import org.apache.poi.ss.util.CellRangeAddressList; */
public class LinkedDropDownLists {
- LinkedDropDownLists(String workbookName) {
- File file = null;
- FileOutputStream fos = null;
- Workbook workbook = null;
- Sheet sheet = null;
- DataValidationHelper dvHelper = null;
- DataValidationConstraint dvConstraint = null;
- DataValidation validation = null;
- CellRangeAddressList addressList = null;
- try {
-
- // Using the ss.usermodel allows this class to support both binary
- // and xml based workbooks. The choice of which one to create is
- // made by checking the file extension.
- if (workbookName.endsWith(".xlsx")) {
- workbook = new XSSFWorkbook();
- } else {
- workbook = new HSSFWorkbook();
- }
-
- // Build the sheet that will hold the data for the validations. This
- // must be done first as it will create names that are referenced
- // later.
- sheet = workbook.createSheet("Linked Validations");
- LinkedDropDownLists.buildDataSheet(sheet);
-
- // Build the first data validation to occupy cell A1. Note
- // that it retrieves it's data from the named area or region called
- // CHOICES. Further information about this can be found in the
- // static buildDataSheet() method below.
- addressList = new CellRangeAddressList(0, 0, 0, 0);
- dvHelper = sheet.getDataValidationHelper();
- dvConstraint = dvHelper.createFormulaListConstraint("CHOICES");
- validation = dvHelper.createValidation(dvConstraint, addressList);
- sheet.addValidationData(validation);
-
- // Now, build the linked or dependent drop down list that will
- // occupy cell B1. The key to the whole process is the use of the
- // INDIRECT() function. In the buildDataSheet(0 method, a series of
- // named regions are created and the names of three of them mirror
- // the options available to the user in the first drop down list
- // (in cell A1). Using the INDIRECT() function makes it possible
- // to convert the selection the user makes in that first drop down
- // into the addresses of a named region of cells and then to use
- // those cells to populate the second drop down list.
- addressList = new CellRangeAddressList(0, 0, 1, 1);
- dvConstraint = dvHelper.createFormulaListConstraint(
- "INDIRECT(UPPER($A$1))");
- validation = dvHelper.createValidation(dvConstraint, addressList);
- sheet.addValidationData(validation);
-
- file = new File(workbookName);
- fos = new FileOutputStream(file);
- workbook.write(fos);
- } catch (IOException ioEx) {
- System.out.println("Caught a: " + ioEx.getClass().getName());
- System.out.println("Message: " + ioEx.getMessage());
- System.out.println("Stacktrace follws:.....");
- ioEx.printStackTrace(System.out);
- } finally {
- try {
- if (fos != null) {
- fos.close();
- fos = null;
- }
- } catch (IOException ioEx) {
- System.out.println("Caught a: " + ioEx.getClass().getName());
- System.out.println("Message: " + ioEx.getMessage());
- System.out.println("Stacktrace follws:.....");
- ioEx.printStackTrace(System.out);
- }
+ LinkedDropDownLists(String workbookName) throws IOException {
+ // Using the ss.usermodel allows this class to support both binary
+ // and xml based workbooks. The choice of which one to create is
+ // made by checking the file extension.
+ Workbook workbook;
+ if (workbookName.endsWith(".xlsx")) {
+ workbook = new XSSFWorkbook();
+ } else {
+ workbook = new HSSFWorkbook();
}
+
+ // Build the sheet that will hold the data for the validations. This
+ // must be done first as it will create names that are referenced
+ // later.
+ Sheet sheet = workbook.createSheet("Linked Validations");
+ LinkedDropDownLists.buildDataSheet(sheet);
+
+ // Build the first data validation to occupy cell A1. Note
+ // that it retrieves it's data from the named area or region called
+ // CHOICES. Further information about this can be found in the
+ // static buildDataSheet() method below.
+ CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
+ DataValidationHelper dvHelper = sheet.getDataValidationHelper();
+ DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint("CHOICES");
+ DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
+ sheet.addValidationData(validation);
+
+ // Now, build the linked or dependent drop down list that will
+ // occupy cell B1. The key to the whole process is the use of the
+ // INDIRECT() function. In the buildDataSheet(0 method, a series of
+ // named regions are created and the names of three of them mirror
+ // the options available to the user in the first drop down list
+ // (in cell A1). Using the INDIRECT() function makes it possible
+ // to convert the selection the user makes in that first drop down
+ // into the addresses of a named region of cells and then to use
+ // those cells to populate the second drop down list.
+ addressList = new CellRangeAddressList(0, 0, 1, 1);
+ dvConstraint = dvHelper.createFormulaListConstraint(
+ "INDIRECT(UPPER($A$1))");
+ validation = dvHelper.createValidation(dvConstraint, addressList);
+ sheet.addValidationData(validation);
+
+ FileOutputStream fos = new FileOutputStream(workbookName);
+ workbook.write(fos);
+ fos.close();
+ workbook.close();
}
/**
diff --git a/src/examples/src/org/apache/poi/ss/examples/LoadEmbedded.java b/src/examples/src/org/apache/poi/ss/examples/LoadEmbedded.java index 66ca8ba7b2..2ad0347f7b 100644 --- a/src/examples/src/org/apache/poi/ss/examples/LoadEmbedded.java +++ b/src/examples/src/org/apache/poi/ss/examples/LoadEmbedded.java @@ -18,34 +18,38 @@ package org.apache.poi.ss.examples; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hslf.usermodel.HSLFSlideShow; import org.apache.poi.hssf.usermodel.HSSFObjectData; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hwpf.HWPFDocument; -import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; -import org.apache.poi.xslf.usermodel.XSLFSlideShow; +import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.xmlbeans.XmlException; /** * Loads embedded resources from Workbooks. Code taken from the website: * https://poi.apache.org/spreadsheet/quick-guide.html#Embedded */ public class LoadEmbedded { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException, EncryptedDocumentException, OpenXML4JException, XmlException { Workbook wb = WorkbookFactory.create(new File(args[0])); loadEmbedded(wb); } - public static void loadEmbedded(Workbook wb) throws Exception { + public static void loadEmbedded(Workbook wb) throws IOException, InvalidFormatException, OpenXML4JException, XmlException { if (wb instanceof HSSFWorkbook) { loadEmbedded((HSSFWorkbook)wb); } @@ -57,22 +61,22 @@ public class LoadEmbedded { } } - public static void loadEmbedded(HSSFWorkbook workbook) throws Exception { + public static void loadEmbedded(HSSFWorkbook workbook) throws IOException { for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) { //the OLE2 Class Name of the object String oleName = obj.getOLE2ClassName(); if (oleName.equals("Worksheet")) { DirectoryNode dn = (DirectoryNode) obj.getDirectory(); HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, false); - //System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets()); + embeddedWorkbook.close(); } else if (oleName.equals("Document")) { DirectoryNode dn = (DirectoryNode) obj.getDirectory(); HWPFDocument embeddedWordDocument = new HWPFDocument(dn); - //System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text()); + embeddedWordDocument.close(); } else if (oleName.equals("Presentation")) { DirectoryNode dn = (DirectoryNode) obj.getDirectory(); - SlideShow<?,?> embeddedPowerPointDocument = new HSLFSlideShow(dn); - //System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length); + SlideShow<?,?> embeddedSlieShow = new HSLFSlideShow(dn); + embeddedSlieShow.close(); } else { if(obj.hasDirectoryEntry()){ // The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is @@ -89,40 +93,38 @@ public class LoadEmbedded { } } - public static void loadEmbedded(XSSFWorkbook workbook) throws Exception { + public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException { for (PackagePart pPart : workbook.getAllEmbedds()) { String contentType = pPart.getContentType(); - // Excel Workbook - either binary or OpenXML if (contentType.equals("application/vnd.ms-excel")) { + // Excel Workbook - either binary or OpenXML HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream()); - } - // Excel Workbook - OpenXML file format - else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { - OPCPackage docPackage = OPCPackage.open(pPart.getInputStream()); - XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(docPackage); - } - // Word Document - binary (OLE2CDF) file format - else if (contentType.equals("application/msword")) { + embeddedWorkbook.close(); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { + // Excel Workbook - OpenXML file format + XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream()); + embeddedWorkbook.close(); + } else if (contentType.equals("application/msword")) { + // Word Document - binary (OLE2CDF) file format HWPFDocument document = new HWPFDocument(pPart.getInputStream()); - } - // Word Document - OpenXML file format - else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { - OPCPackage docPackage = OPCPackage.open(pPart.getInputStream()); - XWPFDocument document = new XWPFDocument(docPackage); - } - // PowerPoint Document - binary file format - else if (contentType.equals("application/vnd.ms-powerpoint")) { + document.close(); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) { + // Word Document - OpenXML file format + XWPFDocument document = new XWPFDocument(pPart.getInputStream()); + document.close(); + } else if (contentType.equals("application/vnd.ms-powerpoint")) { + // PowerPoint Document - binary file format HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream()); - } - // PowerPoint Document - OpenXML file format - else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { - OPCPackage docPackage = OPCPackage.open(pPart.getInputStream()); - XSLFSlideShow slideShow = new XSLFSlideShow(docPackage); - } - // Any other type of embedded object. - else { + slideShow.close(); + } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) { + // PowerPoint Document - OpenXML file format + XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream()); + slideShow.close(); + } else { + // Any other type of embedded object. System.out.println("Unknown Embedded Document: " + contentType); InputStream inputStream = pPart.getInputStream(); + inputStream.close(); } } } diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java index 3f044c839e..5d367316b3 100644 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/EmbeddedObjects.java @@ -16,57 +16,57 @@ ==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import java.io.InputStream;
+
+import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xslf.usermodel.XSLFSlideShow;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
-import org.apache.poi.hwpf.HWPFDocument;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
-import java.io.InputStream;
/**
* Demonstrates how you can extract embedded data from a .xlsx file
*/
public class EmbeddedObjects {
public static void main(String[] args) throws Exception {
- OPCPackage pkg = OPCPackage.open(args[0]);
- XSSFWorkbook workbook = new XSSFWorkbook(pkg);
+ XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
- // Excel Workbook - either binary or OpenXML
if (contentType.equals("application/vnd.ms-excel")) {
+ // Excel Workbook - either binary or OpenXML
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
- }
- // Excel Workbook - OpenXML file format
- else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
+ embeddedWorkbook.close();
+ } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
+ // Excel Workbook - OpenXML file format
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
- }
- // Word Document - binary (OLE2CDF) file format
- else if (contentType.equals("application/msword")) {
+ embeddedWorkbook.close();
+ } else if (contentType.equals("application/msword")) {
+ // Word Document - binary (OLE2CDF) file format
HWPFDocument document = new HWPFDocument(pPart.getInputStream());
- }
- // Word Document - OpenXML file format
- else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
+ document.close();
+ } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
+ // Word Document - OpenXML file format
XWPFDocument document = new XWPFDocument(pPart.getInputStream());
- }
- // PowerPoint Document - binary file format
- else if (contentType.equals("application/vnd.ms-powerpoint")) {
+ document.close();
+ } else if (contentType.equals("application/vnd.ms-powerpoint")) {
+ // PowerPoint Document - binary file format
HSLFSlideShowImpl slideShow = new HSLFSlideShowImpl(pPart.getInputStream());
- }
- // PowerPoint Document - OpenXML file format
- else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
+ slideShow.close();
+ } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
+ // PowerPoint Document - OpenXML file format
OPCPackage docPackage = OPCPackage.open(pPart.getInputStream());
XSLFSlideShow slideShow = new XSLFSlideShow(docPackage);
- }
- // Any other type of embedded object.
- else {
+ slideShow.close();
+ } else {
+ // Any other type of embedded object.
System.out.println("Unknown Embedded Document: " + contentType);
InputStream inputStream = pPart.getInputStream();
+ inputStream.close();
}
}
- pkg.close();
+ workbook.close();
}
}
\ No newline at end of file |