From 0c210e6842f52efda889b2b4732da76f3e959bc2 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 31 Mar 2008 21:06:55 +0000 Subject: [PATCH] Add examples and documentation for xssf eventusermodel code git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@643149 13f79535-47bb-0310-9956-ffa450edef68 --- .../content/xdocs/spreadsheet/how-to.xml | 223 ++++++++++++++---- .../eventusermodel/examples/FromHowTo.java | 124 ++++++++++ 2 files changed, 305 insertions(+), 42 deletions(-) create mode 100644 src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java diff --git a/src/documentation/content/xdocs/spreadsheet/how-to.xml b/src/documentation/content/xdocs/spreadsheet/how-to.xml index 053333e6e..4d13aeee5 100644 --- a/src/documentation/content/xdocs/spreadsheet/how-to.xml +++ b/src/documentation/content/xdocs/spreadsheet/how-to.xml @@ -33,7 +33,8 @@
How to use the HSSF API
Capabilities -

This release of the how-to outlines functionality for the CVS HEAD. +

This release of the how-to outlines functionality for the + current svn trunk. Those looking for information on previous releases should look in the documentation distributed with that release.

@@ -53,55 +54,64 @@ have different characteristics, so you should read up on all to select the best for you.

    -
  • User API
  • -
  • Event API
  • -
  • Event API with extensions to be Record Aware
  • +
  • User API (HSSF and XSSF)
  • +
  • Event API (HSSF Only)
  • +
  • Event API with extensions to be Record Aware (HSSF Only)
  • +
  • XSSF and SAX (Event API)
  • Low Level API
General Use -
User API +
User API (HSSF and XSSF)
Writing a new file -

The high level API (package: org.apache.poi.hssf.usermodel) +

The high level API (package: org.apache.poi.ss.usermodel) is what most people should use. Usage is very simple.

Workbooks are created by creating an instance of - org.apache.poi.hssf.usermodel.HSSFWorkbook. + org.apache.poi.ss.usermodel.Workbook. Either create + a concrete class directly + (org.apache.poi.hssf.usermodel.HSSFWorkbook or + org.apache.poi.xssf.usermodel.XSSFWorkbook), or use + the handy factory class + org.apache.poi.ss.usermodel.WorkbookFactory.

Sheets are created by calling createSheet() from an existing - instance of HSSFWorkbook, the created sheet is automatically added in + instance of Workbook, the created sheet is automatically added in sequence to the workbook. Sheets do not in themselves have a sheet name (the tab at the bottom); you set the name associated with a sheet by calling - HSSFWorkbook.setSheetName(sheetindex,"SheetName",encoding). - The name may be in 8bit format (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) - or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default encoding is 8bit per char. + Workbook.setSheetName(sheetindex,"SheetName",encoding). + For HSSF, the name may be in 8bit format + (HSSFWorkbook.ENCODING_COMPRESSED_UNICODE) + or Unicode (HSSFWorkbook.ENCODING_UTF_16). Default + encoding for HSSF is 8bit per char. For XSSF, the name + is automatically handled as unicode.

Rows are created by calling createRow(rowNumber) from an existing - instance of HSSFSheet. Only rows that have cell values should be + instance of Sheet. Only rows that have cell values should be added to the sheet. To set the row's height, you just call setRowHeight(height) on the row object. The height must be given in twips, or 1/20th of a point. If you prefer, there is also a setRowHeightInPoints method.

Cells are created by calling createCell(column, type) from an - existing HSSFRow. Only cells that have values should be added to the + existing Row. Only cells that have values should be added to the row. Cells should have their cell type set to either - HSSFCell.CELL_TYPE_NUMERIC or HSSFCell.CELL_TYPE_STRING depending on + Cell.CELL_TYPE_NUMERIC or Cell.CELL_TYPE_STRING depending on whether they contain a numeric or textual value. Cells must also have a value set. Set the value by calling setCellValue with either a String or double as a parameter. Individual cells do not have a width; you must call setColumnWidth(colindex, width) (use units of - 1/256th of a character) on the HSSFSheet object. (You can't do it on + 1/256th of a character) on the Sheet object. (You can't do it on an individual basis in the GUI either).

-

Cells are styled with HSSFCellStyle objects which in turn contain - a reference to an HSSFFont object. These are created via the - HSSFWorkbook object by calling createCellStyle() and createFont(). +

Cells are styled with CellStyle objects which in turn contain + a reference to an Font object. These are created via the + Workbook object by calling createCellStyle() and createFont(). Once you create the object you must set its parameters (colors, - borders, etc). To set a font for an HSSFCellStyle call + borders, etc). To set a font for an CellStyle call setFont(fontobj).

Once you have generated your workbook, you can write it out by @@ -118,21 +128,21 @@ short rownum; // create a new file FileOutputStream out = new FileOutputStream("workbook.xls"); // create a new workbook -HSSFWorkbook wb = new HSSFWorkbook(); +Workbook wb = new HSSFWorkbook(); // create a new sheet -HSSFSheet s = wb.createSheet(); +Sheet s = wb.createSheet(); // declare a row object reference -HSSFRow r = null; +Row r = null; // declare a cell object reference -HSSFCell c = null; +Cell c = null; // create 3 cell styles -HSSFCellStyle cs = wb.createCellStyle(); -HSSFCellStyle cs2 = wb.createCellStyle(); -HSSFCellStyle cs3 = wb.createCellStyle(); -HSSFDataFormat df = wb.createDataFormat(); +CellStyle cs = wb.createCellStyle(); +CellStyle cs2 = wb.createCellStyle(); +CellStyle cs3 = wb.createCellStyle(); +DataFormat df = wb.createDataFormat(); // create 2 fonts objects -HSSFFont f = wb.createFont(); -HSSFFont f2 = wb.createFont(); +Font f = wb.createFont(); +Font f2 = wb.createFont(); //set font 1 to 12 point type f.setFontHeightInPoints((short) 12); @@ -140,14 +150,14 @@ f.setFontHeightInPoints((short) 12); f.setColor( (short)0xc ); // make it bold //arial is the default font -f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); +f.setBoldweight(Font.BOLDWEIGHT_BOLD); //set font 2 to 10 point type f2.setFontHeightInPoints((short) 10); //make it red -f2.setColor( (short)HSSFFont.COLOR_RED ); +f2.setColor( (short)Font.COLOR_RED ); //make it bold -f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); +f2.setBoldweight(Font.BOLDWEIGHT_BOLD); f2.setStrikeout( true ); @@ -159,8 +169,8 @@ cs.setDataFormat(df.getFormat("#,##0.0")); //set a thin border cs2.setBorderBottom(cs2.BORDER_THIN); //fill w fg fill color -cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND); -//set the cell format to text see HSSFDataFormat for a full list +cs2.setFillPattern((short) CellStyle.SOLID_FOREGROUND); +//set the cell format to text see DataFormat for a full list cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); // set the font @@ -168,10 +178,9 @@ cs2.setFont(f2); // set the sheet name in Unicode wb.setSheetName(0, "\u0422\u0435\u0441\u0442\u043E\u0432\u0430\u044F " + - "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430", - HSSFWorkbook.ENCODING_UTF_16 ); -// in case of compressed Unicode -// wb.setSheetName(0, "HSSF Test", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE ); + "\u0421\u0442\u0440\u0430\u043D\u0438\u0447\u043A\u0430" ); +// in case of plain ascii +// wb.setSheetName(0, "HSSF Test"); // create a sheet with 30 rows (0-29) for (rownum = (short) 0; rownum < 30; rownum++) { @@ -206,14 +215,12 @@ for (rownum = (short) 0; rownum < 30; rownum++) // set this cell to the first cell style we defined c.setCellStyle(cs); // set the cell's string value to "Test" - c.setEncoding( HSSFCell.ENCODING_COMPRESSED_UNICODE ); c.setCellValue( "Test" ); } else { c.setCellStyle(cs2); // set the cell's string value to "\u0422\u0435\u0441\u0442" - c.setEncoding( HSSFCell.ENCODING_UTF_16 ); c.setCellValue( "\u0422\u0435\u0441\u0442" ); } @@ -280,7 +287,7 @@ call workbook.write(outputstream) just as you did above.

-
Event API +
Event API (HSSF Only)

The event API is newer than the User API. It is intended for intermediate developers who are willing to learn a little bit of the low level API @@ -419,7 +426,7 @@ public class EventExample

-
Record Aware Event API +
Record Aware Event API (HSSF Only)

This is an extension to the normal Event API. With this, your listener @@ -470,6 +477,138 @@ The latest version is always available from

+ +
XSSF and SAX (Event API) + +

If memory footprint is an issue, then for XSSF, you can get at + the underlying XML data, and process it yourself. This is intended + for intermediate developers who are willing to learn a little bit of + low level structure of .xlsx files, and who are happy processing + XML in java. Its relatively simple to use, but requires a basic + understanding of the file structure. The advantage provided is that + you can read a XLSX file with a relatively small memory footprint. +

+

One important thing to note with the basic Event API is that it + triggers events only for things actually stored within the file. + With the XLSX file format, it is quite common for things that + have yet to be edited to simply not exist in the file. This means + there may well be apparent "gaps" in the record stream, which + you need to work around.

+

To use this API you construct an instance of + org.apache.poi.xssf.eventmodel.XSSFReader. This will optionally + provide a nice interace on the shared strings table, and the styles. + It provides methods to get the raw xml data from the rest of the + file, which you will then pass to SAX.

+

This example shows how to get at a single known sheet, or at + all sheets in the file. It is based on the example in svn + src/examples/src/org/apache/poi/xssf/eventusermodel/exmaples/FromHowTo.java

+ sheets = r.getSheetsData(); + while(sheets.hasNext()) { + InputStream sheet = sheets.next(); + InputSource sheetSource = new InputSource(sheet); + parser.parse(sheetSource); + sheet.close(); + } + } + + public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException { + XMLReader parser = + XMLReaderFactory.createXMLReader( + "org.apache.xerces.parsers.SAXParser" + ); + ContentHandler handler = new SheetHandler(sst); + parser.setContentHandler(handler); + return parser; + } + + /** + * See org.xml.sax.helpers.DefaultHandler javadocs + */ + private static class SheetHandler extends DefaultHandler { + private SharedStringsTable sst; + private String lastContents; + private boolean nextIsString; + + private SheetHandler(SharedStringsTable sst) { + this.sst = sst; + } + + public void startElement(String uri, String localName, String name, + Attributes attributes) throws SAXException { + // c => cell + if(name.equals("c")) { + // Print the cell reference + System.out.print(attributes.getValue("r") + " - "); + // Figure out if the value is an index in the SST + if(attributes.getValue("t").equals("s")) { + nextIsString = true; + } else { + nextIsString = false; + } + } + // v => contents of a cell + if(name.equals("v")) { + System.out.println(lastContents); + } + } + + public void characters(char[] ch, int start, int length) + throws SAXException { + lastContents = new String(ch, start, length); + if(nextIsString) { + int idx = Integer.parseInt(lastContents); + lastContents = sst.getSharedStringAt(idx); + } + } + } + + public static void main(String[] args) throws Exception { + FromHowTo howto = new FromHowTo(); + howto.processOneSheet(args[0]); + howto.processAllSheets(args[0]); + } +} +]]> +
+
Low Level APIs 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 new file mode 100644 index 000000000..794fdee0e --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/examples/FromHowTo.java @@ -0,0 +1,124 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xssf.eventusermodel.examples; + +import java.io.InputStream; +import java.util.Iterator; + +import org.apache.poi.xssf.eventusermodel.XSSFReader; +import org.apache.poi.xssf.model.SharedStringsTable; +import org.openxml4j.opc.Package; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.helpers.XMLReaderFactory; + +/** + * Various things from the how-to documentation + */ +public class FromHowTo { + public void processOneSheet(String filename) throws Exception { + Package pkg = Package.open(filename); + XSSFReader r = new XSSFReader( pkg ); + SharedStringsTable sst = r.getSharedStringsTable(); + + XMLReader parser = fetchSheetParser(sst); + + // rId2 found by processing the Workbook + // Seems to either be rId# or rSheet# + InputStream sheet2 = r.getSheet("rId2"); + InputSource sheetSource = new InputSource(sheet2); + parser.parse(sheetSource); + sheet2.close(); + } + + public void processAllSheets(String filename) throws Exception { + Package pkg = Package.open(filename); + XSSFReader r = new XSSFReader( pkg ); + SharedStringsTable sst = r.getSharedStringsTable(); + + XMLReader parser = fetchSheetParser(sst); + + Iterator sheets = r.getSheetsData(); + while(sheets.hasNext()) { + InputStream sheet = sheets.next(); + InputSource sheetSource = new InputSource(sheet); + parser.parse(sheetSource); + sheet.close(); + } + } + + public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException { + XMLReader parser = + XMLReaderFactory.createXMLReader( + "org.apache.xerces.parsers.SAXParser" + ); + ContentHandler handler = new SheetHandler(sst); + parser.setContentHandler(handler); + return parser; + } + + /** + * See org.xml.sax.helpers.DefaultHandler javadocs + */ + private static class SheetHandler extends DefaultHandler { + private SharedStringsTable sst; + private String lastContents; + private boolean nextIsString; + + private SheetHandler(SharedStringsTable sst) { + this.sst = sst; + } + + public void startElement(String uri, String localName, String name, + Attributes attributes) throws SAXException { + // c => cell + if(name.equals("c")) { + // Print the cell reference + System.out.print(attributes.getValue("r") + " - "); + // Figure out if the value is an index in the SST + if(attributes.getValue("t").equals("s")) { + nextIsString = true; + } else { + nextIsString = false; + } + } + // v => contents of a cell + if(name.equals("v")) { + System.out.println(lastContents); + } + } + + public void characters(char[] ch, int start, int length) + throws SAXException { + lastContents = new String(ch, start, length); + if(nextIsString) { + int idx = Integer.parseInt(lastContents); + lastContents = sst.getSharedStringAt(idx); + } + } + } + + public static void main(String[] args) throws Exception { + FromHowTo howto = new FromHowTo(); + howto.processOneSheet(args[0]); + howto.processAllSheets(args[0]); + } +}