Search in sources :

Example 1 with OldExcelFormatException

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");
}
Also used : POITextExtractor(org.apache.poi.POITextExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) EventBasedExcelExtractor(org.apache.poi.hssf.extractor.EventBasedExcelExtractor) Method(java.lang.reflect.Method) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) IOException(java.io.IOException) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) EventBasedExcelExtractor(org.apache.poi.hssf.extractor.EventBasedExcelExtractor)

Example 2 with OldExcelFormatException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) URI(java.net.URI) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException)

Example 3 with OldExcelFormatException

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"));
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) OPOIFSFileSystem(org.apache.poi.poifs.filesystem.OPOIFSFileSystem) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) Test(org.junit.Test)

Example 4 with OldExcelFormatException

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) Test(org.junit.Test)

Aggregations

OldExcelFormatException (org.apache.poi.hssf.OldExcelFormatException)4 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 POITextExtractor (org.apache.poi.POITextExtractor)1 EventBasedExcelExtractor (org.apache.poi.hssf.extractor.EventBasedExcelExtractor)1 ExcelExtractor (org.apache.poi.hssf.extractor.ExcelExtractor)1 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)1 OPOIFSFileSystem (org.apache.poi.poifs.filesystem.OPOIFSFileSystem)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1