From 115319165849923da581e4b804406c47db2c1716 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 11 Mar 2008 12:47:16 +0000 Subject: [PATCH] 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 --- build.xml | 4 +- .../content/xdocs/ss/quick-guide.xml | 38 +++++--- .../poi/ss/usermodel/WorkbookFactory.java | 66 +++++++++++++ .../apache/poi/ss/TestWorkbookFactory.java | 93 +++++++++++++++++++ 4 files changed, 184 insertions(+), 17 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java create mode 100644 src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java diff --git a/build.xml b/build.xml index 3c40743da6..98658fbad6 100644 --- a/build.xml +++ b/build.xml @@ -145,8 +145,8 @@ under the License. - - + + diff --git a/src/documentation/content/xdocs/ss/quick-guide.xml b/src/documentation/content/xdocs/ss/quick-guide.xml index f334948779..e2186953bd 100644 --- a/src/documentation/content/xdocs/ss/quick-guide.xml +++ b/src/documentation/content/xdocs/ss/quick-guide.xml @@ -77,18 +77,24 @@
New Workbook - HSSFWorkbook wb = new HSSFWorkbook(); + Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); + fileOut.close(); + + Workbook wb = new XSSFWorkbook(); + FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); + wb.write(fileOut); fileOut.close();
New Sheet - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("new sheet"); - HSSFSheet sheet2 = wb.createSheet("second sheet"); + Workbook wb = new HSSFWorkbook(); + //Workbook wb = new XSSFWorkbook(); + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); @@ -97,13 +103,14 @@
Creating Cells - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("new sheet"); + Workbook wb = new HSSFWorkbook(); + //Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); // 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. - HSSFCell cell = row.createCell((short)0); + Cell cell = row.createCell((short)0); cell.setCellValue(1); // Or do it on one line. @@ -481,15 +488,16 @@ Examples:
Reading and Rewriting Workbooks - POIFSFileSystem fs = - new POIFSFileSystem(new FileInputStream("workbook.xls")); - HSSFWorkbook wb = new HSSFWorkbook(fs); - HSSFSheet sheet = wb.getSheetAt(0); - HSSFRow row = sheet.getRow(2); - HSSFCell cell = row.getCell((short)3); + InputStream inp = new FileInputStream("workbook.xls"); + //InputStream inp = new FileInputStream("workbook.xlsx"); + + Workbook wb = WorkbookFactory.create(inp); + Sheet sheet = wb.getSheetAt(0); + Row row = sheet.getRow(2); + Cell cell = row.getCell((short)3); if (cell == null) cell = row.createCell((short)3); - cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue("a test"); // Write the output to a file diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java new file mode 100644 index 0000000000..d7984d3b19 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -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"); + } +} diff --git a/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java new file mode 100644 index 0000000000..27d0301b64 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/ss/TestWorkbookFactory.java @@ -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 + } + } +} -- 2.39.5