From 8987da6f67c7f0480dd7f090ad9eb0830ee7e130 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Tue, 11 Nov 2008 18:57:50 +0000 Subject: [PATCH] docs cleanup and improvements git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713108 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/index.xml | 4 +- .../content/xdocs/spreadsheet/converting.xml | 8 +- .../content/xdocs/spreadsheet/how-to.xml | 2 +- .../content/xdocs/spreadsheet/quick-guide.xml | 457 ++++++++++-------- .../eventusermodel/examples/FromHowTo.java | 2 +- 5 files changed, 257 insertions(+), 216 deletions(-) diff --git a/src/documentation/content/xdocs/index.xml b/src/documentation/content/xdocs/index.xml index 6deed531a2..cbf9d3b3ae 100644 --- a/src/documentation/content/xdocs/index.xml +++ b/src/documentation/content/xdocs/index.xml @@ -31,14 +31,14 @@ -
POI 3.5.1 beta 1, and Office Open XML Support (2008-07-18) +
POI 3.5 beta 3, and Office Open XML Support (2008-07-18)

We are currently working to support the new Office Open XML file formats, such as XLSX and PPTX, which were introduced in Office 2007.

Development for this is in a svn branch, but we are please to announce our first preview release containing this support. Users interested in the OOXML support should download the - POI 3.5.1 beta 1 + POI 3.5 beta 3 the source and binaries from their local mirror. People interested should also follow the diff --git a/src/documentation/content/xdocs/spreadsheet/converting.xml b/src/documentation/content/xdocs/spreadsheet/converting.xml index 8d0e966e35..c8c891bd94 100644 --- a/src/documentation/content/xdocs/spreadsheet/converting.xml +++ b/src/documentation/content/xdocs/spreadsheet/converting.xml @@ -121,12 +121,12 @@ HSSFFont f2 = wb.createFont(); // Set font 1 to 12 point type, blue and bold f.setFontHeightInPoints((short) 12); -f.setColor( (short)0xc ); +f.setColor( HSSFColor.RED.index ); f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Set font 2 to 10 point type, red and bold f2.setFontHeightInPoints((short) 10); -f2.setColor( (short)HSSFFont.COLOR_RED ); +f2.setColor( HSSFFont.RED.index ); f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // Set cell style and formatting @@ -183,12 +183,12 @@ for(int i=0; i contents of a cell diff --git a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml index 7523f500d8..461a5f207f 100644 --- a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml +++ b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml @@ -112,14 +112,14 @@ // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow((short)0); // Create a cell and put a value in it. - Cell cell = row.createCell((short)0); + Cell cell = row.createCell(0); cell.setCellValue(1); // Or do it on one line. - row.createCell((short)1).setCellValue(1.2); - row.createCell((short)2).setCellValue( + row.createCell(1).setCellValue(1.2); + row.createCell(2).setCellValue( createHelper.createRichTextString("This is a string")); - row.createCell((short)3).setCellValue(true); + row.createCell(3).setCellValue(true); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); @@ -136,11 +136,11 @@ Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. - Row row = sheet.createRow((short)0); + Row row = sheet.createRow(0); // Create a cell and put a date value in it. The first cell is not styled // as a date. - Cell cell = row.createCell((short)0); + Cell cell = row.createCell(0); cell.setCellValue(new Date()); // we style the second cell as a date (and time). It is important to @@ -149,10 +149,15 @@ CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( createHelper.createDataFormat().getFormat("m/d/yy h:mm")); - cell = row.createCell((short)1); + cell = row.createCell(1); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); + //you can also set date as java.util.Calendar + cell = row.createCell(2); + cell.setCellValue(Calendar.getInstance()); + cell.setCellStyle(cellStyle); + // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); @@ -162,14 +167,15 @@

Working with different types of cells - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); - HSSFRow row = sheet.createRow((short)2); - row.createCell((short) 0).setCellValue(1.1); - row.createCell((short) 1).setCellValue(new Date()); - row.createCell((short) 2).setCellValue("a string"); - row.createCell((short) 3).setCellValue(true); - row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); + Row row = sheet.createRow((short)2); + row.createCell(0).setCellValue(1.1); + row.createCell(1).setCellValue(new Date()); + row.createCell(2).setCellValue(Calendar.getInstance()); + row.createCell(3).setCellValue("a string"); + row.createCell(4).setCellValue(true); + row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); @@ -180,22 +186,23 @@
Demonstrates various alignment options - public static void main(String[] args) - throws IOException - { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); - HSSFRow row = sheet.createRow((short) 2); - createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER); - createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION); - createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL); - createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL); - createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY); - createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT); - createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT); + public static void main(String[] args) throws Exception { + Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); + + Sheet sheet = wb.createSheet(); + Row row = sheet.createRow((short) 2); + row.setHeightInPoints(30); + + createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM); + createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM); + createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER); + createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER); + createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY); + createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP); + createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP); // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("workbook.xls"); + FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx"); wb.write(fileOut); fileOut.close(); @@ -204,17 +211,17 @@ /** * Creates a cell and aligns it a certain way. * - * @param wb the workbook - * @param row the row to create the cell in - * @param column the column number to create the cell in - * @param align the alignment for the cell. + * @param wb the workbook + * @param row the row to create the cell in + * @param column the column number to create the cell in + * @param halign the horizontal alignment for the cell. */ - private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align) - { - HSSFCell cell = row.createCell(column); - cell.setCellValue("Align It"); - HSSFCellStyle cellStyle = wb.createCellStyle(); - cellStyle.setAlignment(align); + private static void createCell(Workbook wb, Row row, short column, short halign, short valign) { + Cell cell = row.createCell(column); + cell.setCellValue(new XSSFRichTextString("Align It")); + CellStyle cellStyle = wb.createCellStyle(); + cellStyle.setAlignment(halign); + cellStyle.setVerticalAlignment(valign); cell.setCellStyle(cellStyle); } @@ -222,26 +229,26 @@
Working with borders - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. - HSSFRow row = sheet.createRow((short) 1); + Row row = sheet.createRow(1); // Create a cell and put a value in it. - HSSFCell cell = row.createCell((short) 1); + Cell cell = row.createCell(1); cell.setCellValue(4); // Style the cell with borders all around. - HSSFCellStyle style = wb.createCellStyle(); - style.setBorderBottom(HSSFCellStyle.BORDER_THIN); - style.setBottomBorderColor(HSSFColor.BLACK.index); - style.setBorderLeft(HSSFCellStyle.BORDER_THIN); - style.setLeftBorderColor(HSSFColor.GREEN.index); - style.setBorderRight(HSSFCellStyle.BORDER_THIN); - style.setRightBorderColor(HSSFColor.BLUE.index); - style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED); - style.setTopBorderColor(HSSFColor.BLACK.index); + CellStyle style = wb.createCellStyle(); + style.setBorderBottom(CellStyle.BORDER_THIN); + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderLeft(CellStyle.BORDER_THIN); + style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); + style.setBorderRight(CellStyle.BORDER_THIN); + style.setRightBorderColor(IndexedColors.BLUE.getIndex()); + style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); cell.setCellStyle(style); // Write the output to a file @@ -327,24 +334,24 @@ for (Row row : sheet1) { System.out.print(" - "); switch(cell.getCellType()) { - case Cell.CELL_TYPE_STRING: - System.out.println(cell.getRichStringCellValue().getString()); - break; - case Cell.CELL_TYPE_NUMERIC: - if(DateUtil.isCellDateFormatted(cell)) { - System.out.println(cell.getDateCellValue()); - } else { - System.out.println(cell.getNumericCellValue()); - } - break; - case Cell.CELL_TYPE_BOOLEAN: - System.out.println(cell.getBooleanCellValue()); - break; - case Cell.CELL_TYPE_FORMULA: - System.out.println(cell.getCellFormula()); - break; - default: - System.out.println(); + case Cell.CELL_TYPE_STRING: + System.out.println(cell.getRichStringCellValue().getString()); + break; + case Cell.CELL_TYPE_NUMERIC: + if(DateUtil.isCellDateFormatted(cell)) { + System.out.println(cell.getDateCellValue()); + } else { + System.out.println(cell.getNumericCellValue()); + } + break; + case Cell.CELL_TYPE_BOOLEAN: + System.out.println(cell.getBooleanCellValue()); + break; + case Cell.CELL_TYPE_FORMULA: + System.out.println(cell.getCellFormula()); + break; + default: + System.out.println(); } } } @@ -372,24 +379,24 @@ for (Row row : sheet1) {
Fills and colors - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. - HSSFRow row = sheet.createRow((short) 1); + Row row = sheet.createRow((short) 1); // Aqua background - HSSFCellStyle style = wb.createCellStyle(); - style.setFillBackgroundColor(HSSFColor.AQUA.index); - style.setFillPattern(HSSFCellStyle.BIG_SPOTS); - HSSFCell cell = row.createCell((short) 1); + CellStyle style = wb.createCellStyle(); + style.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); + style.setFillPattern(CellStyle.BIG_SPOTS); + Cell cell = row.createCell((short) 1); cell.setCellValue("X"); cell.setCellStyle(style); // 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.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); + style.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row.createCell((short) 2); cell.setCellValue("X"); cell.setCellStyle(style); @@ -403,14 +410,19 @@ for (Row row : sheet1) {
Merging cells - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); - HSSFRow row = sheet.createRow((short) 1); - HSSFCell cell = row.createCell((short) 1); + Row row = sheet.createRow((short) 1); + Cell cell = row.createCell((short) 1); cell.setCellValue("This is a test of merging"); - sheet.addMergedRegion(new Region(1,(short)1,1,(short)2)); + sheet.addMergedRegion(new CellRangeAddress( + 1, //first row (0-based) + 1, //last row (0-based) + 1, //first column (0-based) + 2 //last column (0-based) + )); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); @@ -421,25 +433,25 @@ for (Row row : sheet1) {
Working with fonts - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. - HSSFRow row = sheet.createRow((short) 1); + Row row = sheet.createRow(1); // Create a new font and alter it. - HSSFFont font = wb.createFont(); + Font font = wb.createFont(); font.setFontHeightInPoints((short)24); font.setFontName("Courier New"); font.setItalic(true); font.setStrikeout(true); // Fonts are set into a style so create a new one to use. - HSSFCellStyle style = wb.createCellStyle(); + CellStyle style = wb.createCellStyle(); style.setFont(font); // Create a cell and put a value in it. - HSSFCell cell = row.createCell((short) 1); + Cell cell = row.createCell(1); cell.setCellValue("This is a test of fonts"); cell.setCellStyle(style); @@ -457,12 +469,12 @@ Examples:

Wrong:

for (int i = 0; i < 10000; i++) { - HSSFRow row = sheet.createRow(i); - HSSFCell cell = row.createCell((short) 0); + Row row = sheet.createRow(i); + Cell cell = row.createCell((short) 0); - HSSFCellStyle style = workbook.createCellStyle(); - HSSFFont font = workbook.createFont(); - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + CellStyle style = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(font); cell.setCellStyle(style); } @@ -470,13 +482,13 @@ Examples:

Correct:

- HSSFCellStyle style = workbook.createCellStyle(); - HSSFFont font = workbook.createFont(); - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + CellStyle style = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(font); for (int i = 0; i < 10000; i++) { - HSSFRow row = sheet.createRow(i); - HSSFCell cell = row.createCell((short) 0); + Row row = sheet.createRow(i); + Cell cell = row.createCell((short) 0); cell.setCellStyle(style); } @@ -484,6 +496,7 @@ Examples:
Custom colors +

HSSF:

HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); @@ -535,6 +548,18 @@ Examples: wb.write(out); out.close(); +

XSSF:

+ + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); + XSSFRow row = sheet.createRow(0); + XSSFCell cell = row.createCell( 0); + cell.setCellValue("custom XSSF colors"); + + XSSFCellStyle style1 = wb.createCellStyle(); + style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); + style1.setFillPattern(CellStyle.SOLID_FOREGROUND); +
Reading and Rewriting Workbooks @@ -545,9 +570,9 @@ Examples: Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(2); - Cell cell = row.getCell((short)3); + Cell cell = row.getCell(3); if (cell == null) - cell = row.createCell((short)3); + cell = row.createCell(3); cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("a test"); @@ -560,41 +585,38 @@ Examples:
Using newlines in cells - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = null; - HSSFCell c = null; - HSSFCellStyle cs = wb.createCellStyle(); - HSSFFont f = wb.createFont(); - HSSFFont f2 = wb.createFont(); - - cs = wb.createCellStyle(); - - cs.setFont( f2 ); - //Word Wrap MUST be turned on - cs.setWrapText( true ); - - r = s.createRow( (short) 2 ); - r.setHeight( (short) 0x349 ); - c = r.createCell( (short) 2 ); - c.setCellType( HSSFCell.CELL_TYPE_STRING ); - c.setCellValue( "Use \n with word wrap on to create a new line" ); - c.setCellStyle( cs ); - s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) ); + Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet(); - FileOutputStream fileOut = new FileOutputStream( "workbook.xls" ); - wb.write( fileOut ); - fileOut.close(); + Row row = sheet.createRow(2); + Cell cell = row.createCell(2); + cell.setCellValue("Use \n with word wrap on to create a new line"); + + //to enable newlines you need set a cell styles with wrap=true + CellStyle cs = wb.createCellStyle(); + cs.setWrapText(true); + cell.setCellStyle(cs); + + //increase row height to accomodate two lines of text + row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); + + //adjust column width to fit the content + sheet.autoSizeColumn((short)2); + + FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx"); + wb.write(fileOut); + fileOut.close(); +
Data Formats - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("format sheet"); - HSSFCellStyle style; - HSSFDataFormat format = wb.createDataFormat(); - HSSFRow row; - HSSFCell cell; + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("format sheet"); + CellStyle style; + DataFormat format = wb.createDataFormat(); + Row row; + Cell cell; short rowNum = 0; short colNum = 0; @@ -620,9 +642,9 @@ Examples:
Fit Sheet to One Page - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("format sheet"); - HSSFPrintSetup ps = sheet.getPrintSetup(); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("format sheet"); + PrintSetup ps = sheet.getPrintSetup(); sheet.setAutobreaks(true); @@ -640,20 +662,23 @@ Examples:
Set Print Area - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("Sheet1"); - wb.setPrintArea(0, "$A$1:$C$2"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("Sheet1"); //sets the print area for the first sheet + wb.setPrintArea(0, "$A$1:$C$2"); + //Alternatively: - //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details) - - // Create various cells and rows for spreadsheet. + wb.setPrintArea( + 0, //sheet index + 0, //start column + 1, //end column + 0, //start row + 0 //end row + ); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); - -
@@ -728,32 +753,24 @@ Examples:
Shift rows up or down on a sheet - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("row sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("row sheet"); - // Create various cells and rows for spreadsheet. + // Create various cells and rows for spreadsheet. - // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) - sheet.shiftRows(5, 10, -5); + // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5) + sheet.shiftRows(5, 10, -5); - FileOutputStream fileOut = new FileOutputStream("workbook.xls"); - wb.write(fileOut); - fileOut.close();
Set a sheet as selected - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("row sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("row sheet"); sheet.setSelected(true); - // Create various cells and rows for spreadsheet. - - FileOutputStream fileOut = new FileOutputStream("workbook.xls"); - wb.write(fileOut); - fileOut.close();
@@ -765,12 +782,9 @@ Examples: 4 for the denominator.

- HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); sheet1.setZoom(3,4); // 75 percent magnification - FileOutputStream fileOut = new FileOutputStream("workbook.xls"); - wb.write(fileOut); - fileOut.close();
@@ -803,7 +817,7 @@ Examples: Split panes are created with the following call:

- sheet2.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT ); + sheet2.createSplitPane( 2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT );

@@ -814,15 +828,15 @@ Examples:

The last parameter indicates which pane currently has - the focus. This will be one of HSSFSheet.PANE_LOWER_LEFT, + the focus. This will be one of Sheet.PANE_LOWER_LEFT, PANE_LOWER_RIGHT, PANE_UPPER_RIGHT or PANE_UPPER_LEFT.

- HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("new sheet"); - HSSFSheet sheet2 = wb.createSheet("second sheet"); - HSSFSheet sheet3 = wb.createSheet("third sheet"); - HSSFSheet sheet4 = wb.createSheet("fourth sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); + Sheet sheet3 = wb.createSheet("third sheet"); + Sheet sheet4 = wb.createSheet("fourth sheet"); // Freeze just one row sheet1.createFreezePane( 0, 1, 0, 1 ); @@ -831,7 +845,7 @@ Examples: // 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); @@ -855,9 +869,9 @@ Examples: To stop the columns from repeating pass in -1 as the start and end rows.

- HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("new sheet"); - HSSFSheet sheet2 = wb.createSheet("second sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); // Set the columns to repeat from column 0 to 2 on the first sheet wb.setRepeatingRowsAndColumns(0,0,2,-1,-1); @@ -875,10 +889,10 @@ Examples: Example is for headers but applies directly to footers.

- HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); - HSSFHeader header = sheet.getHeader(); + Header header = sheet.getHeader(); header.setCenter("Center Header"); header.setLeft("Left Header"); header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + @@ -1103,8 +1117,8 @@ Examples: using the POI API. Here's how:

- HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); sheet1.groupRow( 5, 14 ); sheet1.groupRow( 7, 14 ); @@ -1148,7 +1162,8 @@ Examples:

It should be noted that any existing drawings may be erased once you add a image to a sheet. -

+

+

HSSF:

// Create the drawing patriarch. This is the top level container for // all shapes. This will clear out any existing shapes for that sheet. @@ -1173,18 +1188,44 @@ Examples: HSSFPicture picture = patriarch.createPicture(new HSSFClientAnchor(), loadPicture( "src/resources/logos/logoKarmokar4.png", wb )); HSSFClientAnchor preferredSize = picture.getPreferredSize(); picture.setAnchor(preferredSize); + +

XSSF:

+ + //create a new workbook + XSSFWorkbook wb = new XSSFWorkbook(); + + //add a picture in this workbook. + InputStream is = new FileInputStream("lilies.jpg"); + int pictureIdx = wb.addPicture(is, XSSFWorkbook.PICTURE_TYPE_JPEG); + is.close(); + + //create sheet + XSSFSheet sheet = wb.createSheet(); + + //create drawing + XSSFDrawing drawing = sheet.createDrawingPatriarch(); + + //add a picture shape + XSSFPicture pict = drawing.createPicture(new XSSFClientAnchor(), pictureIdx); + + //auto-size picture + pict.resize(); + + //save workbook + FileOutputStream fileOut = new FileOutputStream("xssf-picture.xlsx"); + wb.write(fileOut); + fileOut.close(); - HSSFPicture.resize() works only for JPEG and PNG. Other formats are not yet supported. + HSSFPicture.resize() and XSSFPicture.resize() work only for JPEG and PNG. Other formats are not yet supported.

Reading images from a workbook:

- HSSFWorkbook wb; - - List lst = wb.getAllPictures(); + + List lst = workbook.getAllPictures(); for (Iterator it = lst.iterator(); it.hasNext(); ) { - HSSFPictureData pict = (HSSFPictureData)it.next(); + PictureData pict = (PictureData)it.next(); String ext = pict.suggestFileExtension(); byte[] data = pict.getData(); if (ext.equals("jpeg")){ @@ -1440,7 +1481,7 @@ Examples:
Adjust column width to fit the contents - HSSFSheet sheet = workbook.getSheetAt(0); + Sheet sheet = workbook.getSheetAt(0); sheet.autoSizeColumn((short)0); //adjust width of the first column sheet.autoSizeColumn((short)1); //adjust width of the second column @@ -1448,17 +1489,16 @@ Examples: To calculate column width HSSFSheet.autoSizeColumn uses Java2D classes that throw exception if graphical environment is not available. In case if graphical environment is not available, you must tell Java that you are running in headless mode and - set the following system property: java.awt.headless=true - (either via -Djava.awt.headless=true startup parameter or via System.setProperty("java.awt.headless", "true")). + set the following system property: java.awt.headless=true .
How to read hyperlinks - HSSFSheet sheet = workbook.getSheetAt(0); + Sheet sheet = workbook.getSheetAt(0); - HSSFCell cell = sheet.getRow(0).getCell((short)0); - HSSFHyperlink link = cell.getHyperlink(); + Cell cell = sheet.getRow(0).getCell((short)0); + Hyperlink link = cell.getHyperlink(); if(link != null){ System.out.println(link.getAddress()); } @@ -1466,23 +1506,24 @@ Examples:
How to create hyperlinks - HSSFWorkbook wb = new HSSFWorkbook(); + Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); + CreationHelper createHelper = wb.getCreationHelper(); //cell style for hyperlinks //by default hypelrinks are blue and underlined - HSSFCellStyle hlink_style = wb.createCellStyle(); - HSSFFont hlink_font = wb.createFont(); - hlink_font.setUnderline(HSSFFont.U_SINGLE); - hlink_font.setColor(HSSFColor.BLUE.index); + CellStyle hlink_style = wb.createCellStyle(); + Font hlink_font = wb.createFont(); + hlink_font.setUnderline(Font.U_SINGLE); + hlink_font.setColor(IndexedColors.BLUE.getIndex()); hlink_style.setFont(hlink_font); - HSSFCell cell; - HSSFSheet sheet = wb.createSheet("Hyperlinks"); - + Cell cell; + Sheet sheet = wb.createSheet("Hyperlinks"); //URL cell = sheet.createRow(0).createCell((short)0); cell.setCellValue("URL Link"); - HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL); + + Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); link.setAddress("http://poi.apache.org/"); cell.setHyperlink(link); cell.setCellStyle(hlink_style); @@ -1490,7 +1531,7 @@ Examples: //link to a file in the current directory cell = sheet.createRow(1).createCell((short)0); cell.setCellValue("File Link"); - link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE); + link = createHelper.createHyperlink(Hyperlink.LINK_FILE); link.setAddress("link1.xls"); cell.setHyperlink(link); cell.setCellStyle(hlink_style); @@ -1498,7 +1539,7 @@ Examples: //e-mail link cell = sheet.createRow(2).createCell((short)0); cell.setCellValue("Email Link"); - link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL); + link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL); //note, if subject contains white spaces, make sure they are url-encoded link.setAddress("mailto:poi@apache.org?subject=Hyperlinks"); cell.setHyperlink(link); @@ -1507,20 +1548,20 @@ Examples: //link to a place in this workbook //create a target sheet and cell - HSSFSheet sheet2 = wb.createSheet("Target Sheet"); + Sheet sheet2 = wb.createSheet("Target Sheet"); sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell"); cell = sheet.createRow(3).createCell((short)0); cell.setCellValue("Worksheet Link"); - link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT); - link.setAddress("'Target Sheet'!A1"); - cell.setHyperlink(link); + Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT); + link2.setAddress("'Target Sheet'!A1"); + cell.setHyperlink(link2); cell.setCellStyle(hlink_style); - FileOutputStream out = new FileOutputStream("hssf-links.xls"); + FileOutputStream out = new FileOutputStream("hyperinks.xlsx"); wb.write(out); out.close(); - +
diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java index 2c145bfe11..e4fe3fe169 100644 --- a/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java @@ -32,7 +32,7 @@ import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; /** - * Various things from the how-to documentation + * XSSF and SAX (Event API) */ public class FromHowTo { public void processOneSheet(String filename) throws Exception { -- 2.39.5