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);
}
}
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);
}
}
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);
}
}
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");
}
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);
}
}
Aggregations