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 199720088..53378a17e 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/BulletsDemo.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/CreateHyperlink.java b/src/examples/src/org/apache/poi/hslf/examples/CreateHyperlink.java index fc25afae5..9ae200f8b 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/CreateHyperlink.java +++ b/src/examples/src/org/apache/poi/hslf/examples/CreateHyperlink.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java b/src/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java index 53cd53edc..c149a9d47 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java @@ -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(); } } 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 3ef5a46ab..cc86b7606 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/HeadersFootersDemo.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java b/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java index e4ad98dc1..ab869dea6 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java @@ -31,27 +31,45 @@ import org.apache.poi.sl.usermodel.VerticalAlignment; /** * Demonstrates how to create tables - * - * @author Yegor Kozlov */ public final class TableDemo { + //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"} + }; + + public static void main(String[] args) throws Exception { - - //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"} - }; - 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(); } } diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java index 9b86cf779..d9896a712 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java @@ -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()); diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java index d108214e6..63b8b57da 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java @@ -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 + 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++) + { + // create a row + r = s.createRow(rownum); + // on every other row + if ((rownum % 2) == 0) + { + // 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))); + + // 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); - // on every other row - if ((rownum % 2) == 0) - { - // 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 + + // 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); - // 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); - } - + // 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(); } } 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 8573f1113..e332f4f7f 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 @@ -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(); + } } } 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 35df7690a..236859139 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 @@ -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.

* - *

* Excel comment is a kind of a text shape, * so inserting a comment is very similar to placing a text box in a worksheet - *

*/ 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(); + } } } 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 80ced95ac..fd29e34e0 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 @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java b/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java index 394026cdc..cfbc0941c 100644 --- a/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java +++ b/src/examples/src/org/apache/poi/ss/examples/formula/UserDefinedFunctionExample.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/MergePresentations.java b/src/examples/src/org/apache/poi/xslf/usermodel/MergePresentations.java index 73f195eb7..de48a7df4 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/MergePresentations.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/MergePresentations.java @@ -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(); } - } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java index 714ebb812..ed39f3051 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial2.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial2.java index 3f1402ae6..86ce72eba 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial2.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial2.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java index 4cab79d88..d561a9f5f 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial3.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial4.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial4.java index 0cf0fd667..3db718d67 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial4.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial4.java @@ -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(); } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java index 988caee2e..d5dbddf73 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial5.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial6.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial6.java index 020089555..5eaff4541 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial6.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial6.java @@ -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(); + } } } diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial7.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial7.java index 647fef758..4fee5bafe 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial7.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial7.java @@ -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(); + } } } diff --git a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java index f431d8125..e3d80cecf 100644 --- a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java @@ -164,12 +164,15 @@ public class EmbeddedExtractor implements Iterable { 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); } diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java index 29d7ffce0..f00476cfc 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java @@ -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;