use of org.apache.poi.hssf.OldExcelFormatException in project poi by apache.
the class OLE2ExtractorFactory method createExtractor.
/**
* Create the Extractor, if possible. Generally needs the Scratchpad jar.
* Note that this won't check for embedded OOXML resources either, use
* {@link org.apache.poi.extractor.ExtractorFactory} for that.
*/
public static POITextExtractor createExtractor(DirectoryNode poifsDir) throws IOException {
// out from
for (String workbookName : WORKBOOK_DIR_ENTRY_NAMES) {
if (poifsDir.hasEntry(workbookName)) {
if (getPreferEventExtractor()) {
return new EventBasedExcelExtractor(poifsDir);
}
return new ExcelExtractor(poifsDir);
}
}
if (poifsDir.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) {
throw new OldExcelFormatException("Old Excel Spreadsheet format (1-95) " + "found. Please call OldExcelExtractor directly for basic text extraction");
}
// Ask Scratchpad, or fail trying
Class<?> cls = getScratchpadClass();
try {
Method m = cls.getDeclaredMethod("createExtractor", DirectoryNode.class);
POITextExtractor ext = (POITextExtractor) m.invoke(null, poifsDir);
if (ext != null)
return ext;
} catch (IllegalArgumentException iae) {
throw iae;
} catch (Exception e) {
throw new IllegalArgumentException("Error creating Scratchpad Extractor", e);
}
throw new IllegalArgumentException("No supported documents found in the OLE2 stream");
}
use of org.apache.poi.hssf.OldExcelFormatException in project hale by halestudio.
the class XLSSchemaTypePage method onShowPage.
@Override
protected void onShowPage(boolean firstShow) {
URI newLocation = getWizard().getProvider().getSource().getLocation();
if (!firstShow && newLocation != null && !newLocation.equals(oldLocation)) {
sheetNum = 0;
}
try {
Workbook wb = WorkbookFactory.create(getWizard().getProvider().getSource().getInput());
int numberOfSheets = wb.getNumberOfSheets();
if (sheetNum >= numberOfSheets) {
sheetNum = 0;
}
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < numberOfSheets; i++) {
items.add(wb.getSheetAt(i).getSheetName());
// only add items if there is a header (no empty sheet)
Row row = wb.getSheetAt(i).getRow(0);
if (row == null && newLocation != null && !newLocation.equals(oldLocation)) {
sheetNum++;
}
}
sheet.setItems(items.toArray(new String[items.size()]));
sheet.select(sheetNum);
// try to update
update(sheetNum);
super.onShowPage(firstShow);
// Overwrite super string field editor value
Sheet sheet = wb.getSheetAt(sheetNum);
setStringFieldEditorValue(sheet.getSheetName());
oldLocation = newLocation;
} catch (OldExcelFormatException e) {
// the setup is not in a valid state
clearPage();
clearSuperPage();
setErrorMessage("Old excel format detected (format 5.0/7.0 (BIFF5)). Please convert the excel file to BIFF8 from Excel versions 97/2000/XP/2003.");
setPageComplete(false);
} catch (Exception e) {
clearPage();
clearSuperPage();
setErrorMessage("Excel file cannot be loaded!");
setPageComplete(false);
}
}
use of org.apache.poi.hssf.OldExcelFormatException in project poi by apache.
the class TestBugs method bug46904.
/**
* java.io.IOException: block[ 0 ] already removed
* (is an excel 95 file though)
*/
@Test
public void bug46904() throws Exception {
try {
OPOIFSFileSystem fs = new OPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
} catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
}
try {
NPOIFSFileSystem fs = new NPOIFSFileSystem(HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
try {
new HSSFWorkbook(fs.getRoot(), false).close();
fail("Should catch exception here");
} finally {
fs.close();
}
} catch (OldExcelFormatException e) {
assertTrue(e.getMessage().startsWith("The supplied spreadsheet seems to be Excel"));
}
}
use of org.apache.poi.hssf.OldExcelFormatException in project poi by apache.
the class TestHSSFWorkbook method helpfulExceptionOnOldFiles.
/**
* If we try to open an old (pre-97) workbook, we get a helpful
* Exception give to explain what we've done wrong
*/
@Test
public void helpfulExceptionOnOldFiles() throws Exception {
InputStream excel4 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_4.xls");
try {
new HSSFWorkbook(excel4).close();
fail("Shouldn't be able to load an Excel 4 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF4");
}
excel4.close();
InputStream excel5 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_5.xls");
try {
new HSSFWorkbook(excel5).close();
fail("Shouldn't be able to load an Excel 5 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF5");
}
excel5.close();
InputStream excel95 = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("testEXCEL_95.xls");
try {
new HSSFWorkbook(excel95).close();
fail("Shouldn't be able to load an Excel 95 file");
} catch (OldExcelFormatException e) {
assertContains(e.getMessage(), "BIFF5");
}
excel95.close();
}
Aggregations