From 75b74411b2fe5a195920a4dbe4f0ea5b44966d77 Mon Sep 17 00:00:00 2001 From: Ugo Cei Date: Thu, 7 Feb 2008 14:08:32 +0000 Subject: [PATCH] Fleshing out reading XSSF. git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@619419 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFCell.java | 1 + .../apache/poi/xssf/usermodel/XSSFSheet.java | 42 +++++++++++++------ .../poi/xssf/usermodel/XSSFWorkbook.java | 28 +++++++++++-- .../apache/poi/xssf/io/TestLoadSaveXSSF.java | 11 +++++ 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 95e1971748..7656936d94 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -50,6 +50,7 @@ public class XSSFCell implements Cell { public XSSFCell(XSSFRow row, CTCell cell) { this.cell = cell; + // TODO: parse cell.getR() to obtain cellnum this.row = row; } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 2c63a9afc9..5884f7439a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -35,10 +35,8 @@ import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; @@ -67,11 +65,16 @@ public class XSSFSheet implements Sheet { this.sheet = sheet; this.worksheet = CTWorksheet.Factory.newInstance(); this.worksheet.addNewSheetData(); - this.rows = new LinkedList(); - for (CTRow row : worksheet.getSheetData().getRowArray()) { - this.rows.add(new XSSFRow(row)); - } - // XXX ??? + initRows(worksheet); + + this.worksheet.addNewHeaderFooter(); + worksheet.addNewRowBreaks(); + worksheet.addNewColBreaks(); + CTSheetPr sheetPr = worksheet.addNewSheetPr(); + sheetPr.addNewPageSetUpPr(); + worksheet.addNewPageMargins(); + + // XXX Initial default data, probably useful only for testing. Review and eliminate if necessary. CTSheetViews views = this.worksheet.addNewSheetViews(); CTSheetView view = views.addNewSheetView(); view.setWorkbookViewId(0); @@ -95,12 +98,25 @@ public class XSSFSheet implements Sheet { col.setWidth(13); col.setCustomWidth(true); } - CTHeaderFooter hf = this.worksheet.addNewHeaderFooter(); - CTPageBreak rowBreaks = worksheet.addNewRowBreaks(); - CTPageBreak columnBreak = worksheet.addNewColBreaks(); - CTSheetPr sheetPr = worksheet.addNewSheetPr(); - CTPageSetUpPr sheetPageSetUpPr = sheetPr.addNewPageSetUpPr(); - CTPageMargins pageMargins = worksheet.addNewPageMargins(); + + initColumns(worksheet); + } + + public XSSFSheet(CTSheet sheet, CTWorksheet worksheet) { + this.sheet = sheet; + this.worksheet = worksheet; + initRows(worksheet); + initColumns(worksheet); + } + + private void initRows(CTWorksheet worksheet) { + this.rows = new LinkedList(); + for (CTRow row : worksheet.getSheetData().getRowArray()) { + this.rows.add(new XSSFRow(row)); + } + } + + private void initColumns(CTWorksheet worksheet) { columnHelper = new ColumnHelper(worksheet); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 5d827eca4c..44ad5aed24 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -39,6 +39,7 @@ import org.openxml4j.exceptions.InvalidFormatException; import org.openxml4j.opc.Package; import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePartName; +import org.openxml4j.opc.PackageRelationship; import org.openxml4j.opc.PackageRelationshipTypes; import org.openxml4j.opc.PackagingURIHelper; import org.openxml4j.opc.TargetMode; @@ -48,10 +49,15 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument; public class XSSFWorkbook extends POIXMLDocument implements Workbook { + private static final String WORKSHEET_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"; + + private static final String WORKSHEET_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"; + private CTWorkbook workbook; private List sheets = new LinkedList(); @@ -69,8 +75,25 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { try { WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream()); this.workbook = doc.getWorkbook(); + for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) { + PackageRelationship rel = this.getCorePart().getRelationship(ctSheet.getId()); + PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); + PackagePart part = getPackage().getPart(relName); + if (part == null) { + throw new IllegalArgumentException("No part found for relationship " + rel); + } + WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream()); + XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet()); + this.sheets.add(sheet); + } } catch (XmlException e) { throw new IOException(e.toString()); + } catch (InvalidFormatException e) { + throw new IOException(e.toString()); +/* + } catch (OpenXML4JException e) { + throw new IOException(e.toString()); +*/ } } @@ -401,9 +424,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { for (int i = 0 ; i < this.getNumberOfSheets() ; ++i) { XSSFSheet sheet = (XSSFSheet) this.getSheetAt(i); PackagePartName partName = PackagingURIHelper.createPartName("/xl/worksheets/sheet" + i + ".xml"); - corePart.addRelationship(partName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet" + 1); - PackagePart part = pkg.createPart(partName, - "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"); + corePart.addRelationship(partName, TargetMode.INTERNAL, WORKSHEET_RELATIONSHIP, "rSheet" + 1); + PackagePart part = pkg.createPart(partName, WORKSHEET_TYPE); // XXX This should not be needed, but apparently the setSaveOuter call above does not work in XMLBeans 2.2 xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet")); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java index 6127a63567..7313958745 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/io/TestLoadSaveXSSF.java @@ -21,6 +21,9 @@ import java.io.File; import junit.framework.TestCase; +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.xssf.usermodel.XSSFWorkbook; @@ -31,12 +34,20 @@ public class TestLoadSaveXSSF extends TestCase { protected void setUp() throws Exception { super.setUp(); filename = System.getProperty("HSSF.testdata.path"); + if (filename == null) { + filename = "src/testcases/org/apache/poi/hssf/data"; + } } public void testLoadSample() throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(new File(filename, "sample.xlsx").getAbsolutePath()); assertEquals(3, workbook.getNumberOfSheets()); assertEquals("Sheet1", workbook.getSheetName(0)); + Sheet sheet = workbook.getSheetAt(0); + Row row = sheet.getRow(0); + Cell cell = row.getCell((short) 1); + // assertNotNull(cell); + // assertEquals(111.0, cell.getNumericCellValue()); } }