Search in sources :

Example 21 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class XSSFWorkbook method newPackage.

/**
     * Create a new SpreadsheetML package and setup the default minimal content
     */
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
    try {
        OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
        // Main part
        PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
        // Create main part relationship
        pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
        // Create main document part
        pkg.createPart(corePartName, workbookType.getContentType());
        pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
        return pkg;
    } catch (Exception e) {
        throw new POIXMLException(e);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) NoSuchElementException(java.util.NoSuchElementException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 22 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestZipPackage method testZipEntityExpansionTerminates.

@Test
public void testZipEntityExpansionTerminates() throws IOException {
    try {
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("poc-xmlbomb.xlsx");
        wb.close();
        fail("Should catch exception due to entity expansion limitations");
    } catch (POIXMLException e) {
        assertEntityLimitReached(e);
    }
}
Also used : POIXMLException(org.apache.poi.POIXMLException) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 23 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestZipPackage method testZipEntityExpansionExceedsMemory.

@Test
public void testZipEntityExpansionExceedsMemory() throws Exception {
    try {
        Workbook wb = WorkbookFactory.create(XSSFTestDataSamples.openSamplePackage("poc-xmlbomb.xlsx"));
        wb.close();
        fail("Should catch exception due to entity expansion limitations");
    } catch (POIXMLException e) {
        assertEntityLimitReached(e);
    }
    try {
        POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-xmlbomb.xlsx"));
        try {
            assertNotNull(extractor);
            try {
                extractor.getText();
            } catch (IllegalStateException e) {
            // expected due to shared strings expansion
            }
        } finally {
            extractor.close();
        }
    } catch (POIXMLException e) {
        assertEntityLimitReached(e);
    }
}
Also used : POITextExtractor(org.apache.poi.POITextExtractor) POIXMLException(org.apache.poi.POIXMLException) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 24 with POIXMLException

use of org.apache.poi.POIXMLException in project poi by apache.

the class TestXSSFReader method testBug57914.

public void testBug57914() throws Exception {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("57914.xlsx");
    final XSSFReader r;
    // this test as well
    try {
        r = new XSSFReader(pkg);
        fail("This will fail until bug 57699 is fixed");
    } catch (POIXMLException e) {
        assertContains(e.getMessage(), "57699");
        return;
    }
    XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
    String text = getShapesString(it);
    assertContains(text, "Line 1");
    assertContains(text, "Line 2");
    assertContains(text, "Line 3");
}
Also used : POIXMLException(org.apache.poi.POIXMLException) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 25 with POIXMLException

use of org.apache.poi.POIXMLException in project OpenRefine by OpenRefine.

the class ExcelImporter method parseOneFile.

@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, JSONObject options, List<Exception> exceptions) {
    Workbook wb = null;
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }
    try {
        wb = POIXMLDocument.hasOOXMLHeader(inputStream) ? new XSSFWorkbook(inputStream) : new HSSFWorkbook(new POIFSFileSystem(inputStream));
    } catch (IOException e) {
        exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Try to use Excel to re-save the file as a different Excel version or as TSV and upload again.", e));
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        exceptions.add(new ImportException("Attempted to parse file as an Excel file but failed. " + "This is probably caused by a corrupt excel file, or due to the file having previously been created or saved by a non-Microsoft application. " + "Please try opening the file in Microsoft Excel and resaving it, then try re-uploading the file. " + "See https://issues.apache.org/bugzilla/show_bug.cgi?id=48261 for further details", e));
        return;
    } catch (IllegalArgumentException e) {
        exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Only Excel 97 and later formats are supported.", e));
        return;
    } catch (POIXMLException e) {
        exceptions.add(new ImportException("Attempted to parse as an Excel file but failed. " + "Invalid XML.", e));
        return;
    }
    int[] sheets = JSONUtilities.getIntArray(options, "sheets");
    for (int sheetIndex : sheets) {
        final Sheet sheet = wb.getSheetAt(sheetIndex);
        final int lastRow = sheet.getLastRowNum();
        TableDataReader dataReader = new TableDataReader() {

            int nextRow = 0;

            Map<String, Recon> reconMap = new HashMap<String, Recon>();

            @Override
            public List<Object> getNextRowOfCells() throws IOException {
                if (nextRow > lastRow) {
                    return null;
                }
                List<Object> cells = new ArrayList<Object>();
                org.apache.poi.ss.usermodel.Row row = sheet.getRow(nextRow++);
                if (row != null) {
                    short lastCell = row.getLastCellNum();
                    for (short cellIndex = 0; cellIndex < lastCell; cellIndex++) {
                        Cell cell = null;
                        org.apache.poi.ss.usermodel.Cell sourceCell = row.getCell(cellIndex);
                        if (sourceCell != null) {
                            cell = extractCell(sourceCell, reconMap);
                        }
                        cells.add(cell);
                    }
                }
                return cells;
            }
        };
        TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + sheet.getSheetName(), limit, options, exceptions);
    }
}
Also used : ArrayList(java.util.ArrayList) POIXMLException(org.apache.poi.POIXMLException) PushbackInputStream(java.io.PushbackInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Cell(com.google.refine.model.Cell) IOException(java.io.IOException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) JSONObject(org.json.JSONObject) Sheet(org.apache.poi.ss.usermodel.Sheet) Recon(com.google.refine.model.Recon) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

POIXMLException (org.apache.poi.POIXMLException)50 XmlException (org.apache.xmlbeans.XmlException)19 IOException (java.io.IOException)18 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)11 InputStream (java.io.InputStream)9 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)9 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)8 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStream (java.io.OutputStream)6 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)6 XmlObject (org.apache.xmlbeans.XmlObject)6 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)5 XmlCursor (org.apache.xmlbeans.XmlCursor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 Workbook (org.apache.poi.ss.usermodel.Workbook)3