]> source.dussan.org Git - poi.git/commitdiff
Support Excel files which contain a WORKBOOK entry, rather than the usual Workbook...
authorNick Burch <nick@apache.org>
Thu, 30 Nov 2006 16:15:55 +0000 (16:15 +0000)
committerNick Burch <nick@apache.org>
Thu, 30 Nov 2006 16:15:55 +0000 (16:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@480987 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java [new file with mode: 0644]

index 13c4669e2257227937872f5a54ad939579cbd7fb..14d0821dd85a52f39934a02cb0a5e6f22a65a99c 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 
 import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -175,8 +176,29 @@ public class HSSFWorkbook
 
         sheets = new ArrayList(INITIAL_CAPACITY);
         names  = new ArrayList(INITIAL_CAPACITY);
+        
+        // Normally, the Workbook will be in a POIFS Stream
+        //  called "Workbook". However, some wierd XLS generators
+        //  put theirs in one called "WORKBOOK"
+        String workbookName = "Workbook";
+        try {
+               fs.getRoot().getEntry(workbookName);
+               // Is the default name
+        } catch(FileNotFoundException fe) {
+               // Try the upper case form
+               try {
+                       workbookName = "WORKBOOK";
+                       fs.getRoot().getEntry(workbookName);
+               } catch(FileNotFoundException wfe) {
+                       // Doesn't contain it in either form
+                       throw new IllegalArgumentException("The supplied POIFSFileSystem contained neither a 'Workbook' entry, nor a 'WORKBOOK' entry. Is it really an excel file?");
+               }
+        }
 
-        InputStream stream = fs.createDocumentInputStream("Workbook");
+        
+        // Grab the data from the workbook stream, however
+        //  it happens to be spelt.
+        InputStream stream = fs.createDocumentInputStream(workbookName);
 
         EventRecordFactory factory = new EventRecordFactory();
 
diff --git a/src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls b/src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls
new file mode 100644 (file)
index 0000000..1c3ef61
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls differ
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java
new file mode 100644 (file)
index 0000000..2239a9f
--- /dev/null
@@ -0,0 +1,39 @@
+package org.apache.poi.hssf.usermodel;
+
+import java.io.FileInputStream;
+
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for how HSSFWorkbook behaves with XLS files
+ *  with a WORKBOOK directory entry (instead of the more
+ *  usual, Workbook)
+ */
+public class TestUppercaseWorkbook extends TestCase {
+       private String dirPath;
+       private String xlsA = "WORKBOOK_in_capitals.xls";
+
+       protected void setUp() throws Exception {
+               super.setUp();
+               
+        dirPath = System.getProperty("HSSF.testdata.path");
+       }
+
+       /**
+        * Test that we can open a file with WORKBOOK
+        */
+       public void testOpen() throws Exception {
+               FileInputStream is = new FileInputStream(dirPath + "/" + xlsA);
+               
+               POIFSFileSystem fs = new POIFSFileSystem(is);
+
+               // Ensure that we have a WORKBOOK entry
+               fs.getRoot().getEntry("WORKBOOK");
+               assertTrue(true);
+               
+               // Try to open the workbook
+               HSSFWorkbook wb = new HSSFWorkbook(fs);
+       }
+}