Start on a factory for producing the right Workbook

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@635902 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-11 12:47:16 +00:00
parent f3c5b82d9d
commit c8afd832c7
4 changed files with 184 additions and 17 deletions

View File

@ -145,8 +145,8 @@ under the License.
<property name="ooxml.jar4.dir" location="${ooxml.lib}/jsr173_1.0_api.jar"/> <property name="ooxml.jar4.dir" location="${ooxml.lib}/jsr173_1.0_api.jar"/>
<property name="ooxml.jar4.url" value="${repository}/xmlbeans/jars/jsr173_1.0_api.jar"/> <property name="ooxml.jar4.url" value="${repository}/xmlbeans/jars/jsr173_1.0_api.jar"/>
<!-- No official release of openxml4j yet --> <!-- No official release of openxml4j yet -->
<property name="ooxml.jar5.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080125.jar"/> <property name="ooxml.jar5.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080311.jar"/>
<property name="ooxml.jar5.url" value="http://people.apache.org/~ugo/openxml4j-bin-alpha-080125.jar"/> <property name="ooxml.jar5.url" value="http://people.apache.org/~nick/openxml4j-bin-alpha-080311.jar"/>
<!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm --> <!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
<!-- "Copy these file(s), free of charge" --> <!-- "Copy these file(s), free of charge" -->

View File

@ -77,18 +77,24 @@
<anchor id="NewWorkbook"/> <anchor id="NewWorkbook"/>
<section><title>New Workbook</title> <section><title>New Workbook</title>
<source> <source>
HSSFWorkbook wb = new HSSFWorkbook(); Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls"); FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut); wb.write(fileOut);
fileOut.close();
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close(); fileOut.close();
</source> </source>
</section> </section>
<anchor id="NewSheet"/> <anchor id="NewSheet"/>
<section><title>New Sheet</title> <section><title>New Sheet</title>
<source> <source>
HSSFWorkbook wb = new HSSFWorkbook(); Workbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet"); //Workbook wb = new XSSFWorkbook();
HSSFSheet sheet2 = wb.createSheet("second sheet"); Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls"); FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut); wb.write(fileOut);
fileOut.close(); fileOut.close();
@ -97,13 +103,14 @@
<anchor id="CreateCells"/> <anchor id="CreateCells"/>
<section><title>Creating Cells</title> <section><title>Creating Cells</title>
<source> <source>
HSSFWorkbook wb = new HSSFWorkbook(); Workbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet"); //Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based. // Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0); Row row = sheet.createRow((short)0);
// Create a cell and put a value in it. // Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0); Cell cell = row.createCell((short)0);
cell.setCellValue(1); cell.setCellValue(1);
// Or do it on one line. // Or do it on one line.
@ -481,15 +488,16 @@ Examples:
<anchor id="ReadWriteWorkbook"/> <anchor id="ReadWriteWorkbook"/>
<section><title>Reading and Rewriting Workbooks</title> <section><title>Reading and Rewriting Workbooks</title>
<source> <source>
POIFSFileSystem fs = InputStream inp = new FileInputStream("workbook.xls");
new POIFSFileSystem(new FileInputStream("workbook.xls")); //InputStream inp = new FileInputStream("workbook.xlsx");
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0); Workbook wb = WorkbookFactory.create(inp);
HSSFRow row = sheet.getRow(2); Sheet sheet = wb.getSheetAt(0);
HSSFCell cell = row.getCell((short)3); Row row = sheet.getRow(2);
Cell cell = row.getCell((short)3);
if (cell == null) if (cell == null)
cell = row.createCell((short)3); cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test"); cell.setCellValue("a test");
// Write the output to a file // Write the output to a file

View File

@ -0,0 +1,66 @@
/* ====================================================================
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.ss.usermodel;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxml4j.opc.Package;
/**
* Factory for creating the appropriate kind of Workbook
* (be it HSSFWorkbook or XSSFWorkbook), from the given input
*/
public class WorkbookFactory {
/**
* Creates an HSSFWorkbook from the given POIFSFileSystem
*/
public static Workbook create(POIFSFileSystem fs) throws IOException {
return new HSSFWorkbook(fs);
}
/**
* Creates an XSSFWorkbook from the given OOXML Package
*/
public static Workbook create(Package pkg) throws IOException {
return new XSSFWorkbook(pkg);
}
/**
* Creates the appropriate HSSFWorkbook / XSSFWorkbook from
* the given InputStream.
* Your input stream MUST either support mark/reset, or
* be wrapped as a {@link PushbackInputStream}!
*/
public static Workbook create(InputStream inp) throws Exception {
// If clearly doesn't do mark/reset, wrap up
if(! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
if(POIFSFileSystem.hasPOIFSHeader(inp)) {
return new HSSFWorkbook(inp);
}
if(POIXMLDocument.hasOOXMLHeader(inp)) {
return new XSSFWorkbook( Package.open(inp) );
}
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
}

View File

@ -0,0 +1,93 @@
/* ====================================================================
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.ss;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxml4j.opc.Package;
import junit.framework.TestCase;
public class TestWorkbookFactory extends TestCase {
private File xls;
private File xlsx;
private File txt;
protected void setUp() throws Exception {
xls = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "SampleSS.xls"
);
xlsx = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "SampleSS.xlsx"
);
txt = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "SampleSS.txt"
);
assertTrue(xls.exists());
assertTrue(xlsx.exists());
assertTrue(txt.exists());
}
public void testCreate() throws Exception {
Workbook wb;
// POIFS -> hssf
wb = WorkbookFactory.create(
new POIFSFileSystem(new FileInputStream(xls))
);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
// Package -> xssf
wb = WorkbookFactory.create(
Package.open(xlsx.toString())
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
// InputStream -> either
wb = WorkbookFactory.create(
new FileInputStream(xls)
);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
wb = WorkbookFactory.create(
new FileInputStream(xlsx)
);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
try {
wb = WorkbookFactory.create(
new FileInputStream(txt)
);
fail();
} catch(IllegalArgumentException e) {
// Good
}
}
}