diff --git a/src/documentation/content/xdocs/ss/quick-guide.xml b/src/documentation/content/xdocs/ss/quick-guide.xml index 66da60489..f33494877 100644 --- a/src/documentation/content/xdocs/ss/quick-guide.xml +++ b/src/documentation/content/xdocs/ss/quick-guide.xml @@ -21,7 +21,7 @@
- Busy Developers' Guide to HSSF Features + Busy Developers' Guide to HSSF and XSSF Features @@ -30,8 +30,9 @@
Busy Developers' Guide to Features

- Want to use HSSF read and write spreadsheets in a hurry? This guide is for you. If you're after - more in-depth coverage of the HSSF user-API please consult the HOWTO + Want to use HSSF and XSSF read and write spreadsheets in a hurry? This + guide is for you. If you're after more in-depth coverage of the HSSF and + XSSF user-APIs, please consult the HOWTO guide as it contains actual descriptions of how to use this stuff.

Index of Features diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index 54b92e32d..36f195eeb 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -46,18 +46,33 @@ public abstract class POIXMLDocument { protected POIXMLDocument() {} - protected POIXMLDocument(String path) throws IOException { - try { - this.pkg = Package.open(path); - PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType( - PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); - - // Get core part - this.corePart = this.pkg.getPart(coreDocRelationship); - } catch (InvalidFormatException e) { - throw new IOException(e.toString()); + protected POIXMLDocument(Package pkg) throws IOException { + try { + this.pkg = pkg; + + PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType( + PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); + + // Get core part + this.corePart = this.pkg.getPart(coreDocRelationship); } catch (OpenXML4JException e) { throw new IOException(e.toString()); + } + } + protected POIXMLDocument(String path) throws IOException { + this(openPackage(path)); + } + + /** + * Wrapper to open a package, returning an IOException + * in the event of a problem. + * Works around shortcomings in java's this() constructor calls + */ + protected static Package openPackage(String path) throws IOException { + try { + return Package.open(path); + } catch (InvalidFormatException e) { + throw new IOException(e.toString()); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 69361e7b4..ba3bd1095 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -20,6 +20,11 @@ import java.io.File; import java.io.IOException; import org.apache.poi.POIXMLTextExtractor; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlException; import org.openxml4j.exceptions.OpenXML4JException; @@ -33,10 +38,13 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; * Helper class to extract text from an OOXML Excel file */ public class XSSFExcelExtractor extends POIXMLTextExtractor { - private XSSFWorkbook workbook; + private Workbook workbook; private boolean includeSheetNames = true; private boolean formulasNotResults = false; + public XSSFExcelExtractor(String path) throws XmlException, OpenXML4JException, IOException { + this(new XSSFWorkbook(path)); + } public XSSFExcelExtractor(Package container) throws XmlException, OpenXML4JException, IOException { this(new XSSFWorkbook(container)); } @@ -52,9 +60,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor { System.exit(1); } POIXMLTextExtractor extractor = - new HXFExcelExtractor(HXFDocument.openPackage( - new File(args[0]) - )); + new XSSFExcelExtractor(args[0]); System.out.println(extractor.getText()); } @@ -78,48 +84,27 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor { public String getText() { StringBuffer text = new StringBuffer(); - CTSheet[] sheetRefs = - workbook._getHSSFXML().getSheetReferences().getSheetArray(); - for(int i=0; i 0) { - text.append("\n"); - } - if(includeSheetNames) { - text.append(sheetRefs[i].getName() + "\n"); - } - - for(int j=0; j 0) { - text.append("\t"); - } - - boolean done = false; - - // Is it a formula one? - if(cell.getF() != null) { - if(formulasNotResults) { - text.append(cell.getF().getStringValue()); - done = true; - } - } - if(!done) { - HSSFXMLCell uCell = new HSSFXMLCell(cell, workbook); - text.append(uCell.getStringValue()); - } + for(int i=0; i