Search in sources :

Example 1 with POIXMLException

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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FileInputStream(java.io.FileInputStream) 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) JSONObject(org.json.JSONObject) PushbackInputStream(java.io.PushbackInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 2 with POIXMLException

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);
    }
}
Also used : POIXMLException(org.apache.poi.POIXMLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) POIXMLException(org.apache.poi.POIXMLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with POIXMLException

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;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 4 with POIXMLException

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);
    }
}
Also used : XmlException(org.apache.xmlbeans.XmlException) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) IOException(java.io.IOException)

Example 5 with POIXMLException

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);
    }
}
Also used : XmlException(org.apache.xmlbeans.XmlException) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) IOException(java.io.IOException) PageType(com.microsoft.schemas.office.visio.x2012.main.PageType)

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