use of org.apache.poi.POIXMLException in project OpenRefine by OpenRefine.
the class ExcelImporter method createParserUIInitializationData.
@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
JSONArray sheetRecords = new JSONArray();
JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
try {
if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
InputStream is = new FileInputStream(file);
if (!is.markSupported()) {
is = new PushbackInputStream(is, 8);
}
try {
Workbook wb = POIXMLDocument.hasOOXMLHeader(is) ? new XSSFWorkbook(is) : new HSSFWorkbook(new POIFSFileSystem(is));
int sheetCount = wb.getNumberOfSheets();
boolean hasData = false;
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = wb.getSheetAt(i);
int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
JSONObject sheetRecord = new JSONObject();
JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
JSONUtilities.safePut(sheetRecord, "rows", rows);
if (hasData) {
JSONUtilities.safePut(sheetRecord, "selected", false);
} else if (rows > 1) {
JSONUtilities.safePut(sheetRecord, "selected", true);
hasData = true;
}
JSONUtilities.append(sheetRecords, sheetRecord);
}
} finally {
is.close();
}
}
} catch (IOException e) {
logger.error("Error generating parser UI initialization data for Excel file", e);
} catch (IllegalArgumentException e) {
logger.error("Error generating parser UI initialization data for Excel file (only Excel 97 & later supported)", e);
} catch (POIXMLException e) {
logger.error("Error generating parser UI initialization data for Excel file - invalid XML", e);
}
return options;
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class ObjectFactory method load.
public T load(String name, Object... varargs) {
Constructor<? extends T> constructor = _types.get(name);
if (constructor == null) {
@SuppressWarnings("unchecked") X xmlObject = (X) varargs[0];
String typeName = xmlObject.schemaType().getName().getLocalPart();
throw new POIXMLException("Invalid '" + typeName + "' name '" + name + "'");
}
try {
return constructor.newInstance(varargs);
} catch (InvocationTargetException e) {
throw new POIXMLException(e.getCause());
} catch (Exception e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XSLFSheet method importPart.
/**
* Import a package part into this sheet.
*/
PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
PackagePart destPP = getPackagePart();
PackagePartName srcPPName = srcPafrt.getPartName();
OPCPackage pkg = destPP.getPackage();
if (pkg.containPart(srcPPName)) {
// already exists
return pkg.getPart(srcPPName);
}
destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
try {
OutputStream out = part.getOutputStream();
InputStream is = srcPafrt.getInputStream();
IOUtils.copy(is, out);
is.close();
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
return part;
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XDGFPageContents method onDocumentRead.
@Override
protected void onDocumentRead() {
try {
try {
_pageContents = PageContentsDocument.Factory.parse(getPackagePart().getInputStream()).getPageContents();
} catch (XmlException e) {
throw new POIXMLException(e);
} catch (IOException e) {
throw new POIXMLException(e);
}
for (POIXMLDocumentPart part : getRelations()) {
if (!(part instanceof XDGFMasterContents))
continue;
//throw new POIXMLException("Unexpected page relation: " + part);
XDGFMaster master = ((XDGFMasterContents) part).getMaster();
_masters.put(master.getID(), master);
}
super.onDocumentRead();
for (XDGFShape shape : _shapes.values()) {
if (shape.isTopmost())
shape.setupMaster(this, null);
}
} catch (POIXMLException e) {
throw XDGFException.wrap(this, e);
}
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XDGFPages method onDocumentRead.
@Override
protected void onDocumentRead() {
try {
try {
_pagesObject = PagesDocument.Factory.parse(getPackagePart().getInputStream()).getPages();
} catch (XmlException e) {
throw new POIXMLException(e);
} catch (IOException e) {
throw new POIXMLException(e);
}
// this iteration is ordered by page number
for (PageType pageSettings : _pagesObject.getPageArray()) {
String relId = pageSettings.getRel().getId();
POIXMLDocumentPart pageContentsPart = getRelationById(relId);
if (pageContentsPart == null)
throw new POIXMLException("PageSettings relationship for " + relId + " not found");
if (!(pageContentsPart instanceof XDGFPageContents))
throw new POIXMLException("Unexpected pages relationship for " + relId + ": " + pageContentsPart);
XDGFPageContents contents = (XDGFPageContents) pageContentsPart;
XDGFPage page = new XDGFPage(pageSettings, contents, _document, this);
contents.onDocumentRead();
_pages.add(page);
}
} catch (POIXMLException e) {
throw XDGFException.wrap(this, e);
}
}
Aggregations