Support Excel files which contain a WORKBOOK entry, rather than the usual Workbook one. Fixes bug 40840
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@480987 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f86c96a2ab
commit
3f3a52e12b
@ -38,6 +38,7 @@ import org.apache.poi.util.POILogFactory;
|
|||||||
import org.apache.poi.util.POILogger;
|
import org.apache.poi.util.POILogger;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -175,8 +176,29 @@ public class HSSFWorkbook
|
|||||||
|
|
||||||
sheets = new ArrayList(INITIAL_CAPACITY);
|
sheets = new ArrayList(INITIAL_CAPACITY);
|
||||||
names = 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();
|
EventRecordFactory factory = new EventRecordFactory();
|
||||||
|
|
||||||
|
BIN
src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls
Normal file
BIN
src/testcases/org/apache/poi/hssf/data/WORKBOOK_in_capitals.xls
Normal file
Binary file not shown.
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user