Initial support for loading workbooks off files.
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@615275 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c2d4b8b736
commit
2d33f4190a
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -31,12 +32,15 @@ import org.apache.poi.ss.usermodel.Name;
|
|||||||
import org.apache.poi.ss.usermodel.Palette;
|
import org.apache.poi.ss.usermodel.Palette;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.xmlbeans.XmlException;
|
||||||
import org.apache.xmlbeans.XmlObject;
|
import org.apache.xmlbeans.XmlObject;
|
||||||
import org.apache.xmlbeans.XmlOptions;
|
import org.apache.xmlbeans.XmlOptions;
|
||||||
import org.openxml4j.exceptions.InvalidFormatException;
|
import org.openxml4j.exceptions.InvalidFormatException;
|
||||||
|
import org.openxml4j.exceptions.OpenXML4JException;
|
||||||
import org.openxml4j.opc.Package;
|
import org.openxml4j.opc.Package;
|
||||||
import org.openxml4j.opc.PackagePart;
|
import org.openxml4j.opc.PackagePart;
|
||||||
import org.openxml4j.opc.PackagePartName;
|
import org.openxml4j.opc.PackagePartName;
|
||||||
|
import org.openxml4j.opc.PackageRelationship;
|
||||||
import org.openxml4j.opc.PackageRelationshipTypes;
|
import org.openxml4j.opc.PackageRelationshipTypes;
|
||||||
import org.openxml4j.opc.PackagingURIHelper;
|
import org.openxml4j.opc.PackagingURIHelper;
|
||||||
import org.openxml4j.opc.TargetMode;
|
import org.openxml4j.opc.TargetMode;
|
||||||
@ -45,6 +49,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
|
|||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
|
||||||
|
|
||||||
|
|
||||||
public class XSSFWorkbook implements Workbook {
|
public class XSSFWorkbook implements Workbook {
|
||||||
@ -53,6 +58,9 @@ public class XSSFWorkbook implements Workbook {
|
|||||||
|
|
||||||
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||||
|
|
||||||
|
/** The OPC Package */
|
||||||
|
private Package pkg;
|
||||||
|
|
||||||
public XSSFWorkbook() {
|
public XSSFWorkbook() {
|
||||||
this.workbook = CTWorkbook.Factory.newInstance();
|
this.workbook = CTWorkbook.Factory.newInstance();
|
||||||
CTBookViews bvs = this.workbook.addNewBookViews();
|
CTBookViews bvs = this.workbook.addNewBookViews();
|
||||||
@ -61,6 +69,26 @@ public class XSSFWorkbook implements Workbook {
|
|||||||
this.workbook.addNewSheets();
|
this.workbook.addNewSheets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public XSSFWorkbook(String path) throws IOException {
|
||||||
|
try {
|
||||||
|
this.pkg = Package.open(path);
|
||||||
|
PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
|
||||||
|
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
|
||||||
|
|
||||||
|
// Get core part
|
||||||
|
PackagePart corePart = this.pkg.getPart(coreDocRelationship);
|
||||||
|
WorkbookDocument doc = WorkbookDocument.Factory.parse(corePart.getInputStream());
|
||||||
|
this.workbook = doc.getWorkbook();
|
||||||
|
|
||||||
|
} catch (InvalidFormatException e) {
|
||||||
|
throw new IOException(e.toString());
|
||||||
|
} catch (OpenXML4JException e) {
|
||||||
|
throw new IOException(e.toString());
|
||||||
|
} catch (XmlException e) {
|
||||||
|
throw new IOException(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected CTWorkbook getWorkbook() {
|
protected CTWorkbook getWorkbook() {
|
||||||
return this.workbook;
|
return this.workbook;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
/* ====================================================================
|
||||||
|
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.io;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
|
||||||
|
|
||||||
|
public class TestLoadSaveXSSF extends TestCase {
|
||||||
|
|
||||||
|
public void testLoadSample() throws Exception {
|
||||||
|
URL url = this.getClass().getResource("sample.xlsx");
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook(url.getFile());
|
||||||
|
assertEquals(3, workbook.getNumberOfSheets());
|
||||||
|
assertEquals("Sheet1", workbook.getSheetName(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
src/ooxml/testcases/org/apache/poi/xssf/io/sample.xlsx
Normal file
BIN
src/ooxml/testcases/org/apache/poi/xssf/io/sample.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user