mirror of https://github.com/apache/poi.git
Fleshing out reading XSSF.
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@619419 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cf6b3bd464
commit
75b74411b2
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Row>();
|
||||
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<Row>();
|
||||
for (CTRow row : worksheet.getSheetData().getRowArray()) {
|
||||
this.rows.add(new XSSFRow(row));
|
||||
}
|
||||
}
|
||||
|
||||
private void initColumns(CTWorksheet worksheet) {
|
||||
columnHelper = new ColumnHelper(worksheet);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||
|
@ -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"));
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue