Search in sources :

Example 16 with POIXMLException

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

the class XMLSlideShow method addPicture.

/**
     * Adds a picture to the workbook.
     *
     * @param pictureData       The bytes of the picture
     * @param format            The format of the picture.
     *
     * @return the picture data
     */
@Override
public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
    XSLFPictureData img = findPictureData(pictureData);
    if (img != null) {
        return img;
    }
    int imageNumber = _pictures.size();
    XSLFRelation relType = XSLFPictureData.getRelationForType(format);
    if (relType == null) {
        throw new IllegalArgumentException("Picture type " + format + " is not supported.");
    }
    img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true).getDocumentPart();
    img.setIndex(imageNumber);
    _pictures.add(img);
    try {
        OutputStream out = img.getPackagePart().getOutputStream();
        out.write(pictureData);
        out.close();
    } catch (IOException e) {
        throw new POIXMLException(e);
    }
    return img;
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException)

Example 17 with POIXMLException

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

the class XSSFWorkbook method addPicture.

/**
     * Adds a picture to the workbook.
     *
     * @param pictureData       The bytes of the picture
     * @param format            The format of the picture.
     *
     * @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
     * @see Workbook#PICTURE_TYPE_EMF
     * @see Workbook#PICTURE_TYPE_WMF
     * @see Workbook#PICTURE_TYPE_PICT
     * @see Workbook#PICTURE_TYPE_JPEG
     * @see Workbook#PICTURE_TYPE_PNG
     * @see Workbook#PICTURE_TYPE_DIB
     * @see #getAllPictures()
     */
@Override
public int addPicture(byte[] pictureData, int format) {
    int imageNumber = getAllPictures().size() + 1;
    XSSFPictureData img = (XSSFPictureData) createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
    try {
        OutputStream out = img.getPackagePart().getOutputStream();
        out.write(pictureData);
        out.close();
    } catch (IOException e) {
        throw new POIXMLException(e);
    }
    pictures.add(img);
    return imageNumber - 1;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException)

Example 18 with POIXMLException

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

the class XSSFWorkbook method setVBAProject.

/**
     * Adds a vbaProject.bin file to the workbook.  This will change the workbook
     * type if necessary.
     *
     * @throws IOException
     */
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
    if (!isMacroEnabled()) {
        setWorkbookType(XSSFWorkbookType.XLSM);
    }
    PackagePartName ppName;
    try {
        ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    OPCPackage opc = getPackage();
    OutputStream outputStream;
    if (!opc.containPart(ppName)) {
        POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
        outputStream = relationship.getPackagePart().getOutputStream();
    } else {
        PackagePart part = opc.getPart(ppName);
        outputStream = part.getOutputStream();
    }
    try {
        IOUtils.copy(vbaProjectStream, outputStream);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 19 with POIXMLException

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

the class XSSFWorkbook method cloneSheet.

/**
     * Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
     *  The cloned sheet is a deep copy of the original but with a new given
     *  name.
     *
     * @param sheetNum The index of the sheet to clone
     * @param newName The name to set for the newly created sheet
     * @return XSSFSheet representing the cloned sheet.
     * @throws IllegalArgumentException if the sheet index or the sheet
     *         name is invalid
     * @throws POIXMLException if there were errors when cloning
     */
public XSSFSheet cloneSheet(int sheetNum, String newName) {
    validateSheetIndex(sheetNum);
    XSSFSheet srcSheet = sheets.get(sheetNum);
    if (newName == null) {
        String srcName = srcSheet.getSheetName();
        newName = getUniqueSheetName(srcName);
    } else {
        validateSheetName(newName);
    }
    XSSFSheet clonedSheet = createSheet(newName);
    // copy sheet's relations
    List<RelationPart> rels = srcSheet.getRelationParts();
    // if the sheet being cloned has a drawing then rememebr it and re-create it too
    XSSFDrawing dg = null;
    for (RelationPart rp : rels) {
        POIXMLDocumentPart r = rp.getDocumentPart();
        // do not copy the drawing relationship, it will be re-created
        if (r instanceof XSSFDrawing) {
            dg = (XSSFDrawing) r;
            continue;
        }
        addRelation(rp, clonedSheet);
    }
    try {
        for (PackageRelationship pr : srcSheet.getPackagePart().getRelationships()) {
            if (pr.getTargetMode() == TargetMode.EXTERNAL) {
                clonedSheet.getPackagePart().addExternalRelationship(pr.getTargetURI().toASCIIString(), pr.getRelationshipType(), pr.getId());
            }
        }
    } catch (InvalidFormatException e) {
        throw new POIXMLException("Failed to clone sheet", e);
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        srcSheet.write(out);
        clonedSheet.read(new ByteArrayInputStream(out.toByteArray()));
    } catch (IOException e) {
        throw new POIXMLException("Failed to clone sheet", e);
    }
    CTWorksheet ct = clonedSheet.getCTWorksheet();
    if (ct.isSetLegacyDrawing()) {
        logger.log(POILogger.WARN, "Cloning sheets with comments is not yet supported.");
        ct.unsetLegacyDrawing();
    }
    if (ct.isSetPageSetup()) {
        logger.log(POILogger.WARN, "Cloning sheets with page setup is not yet supported.");
        ct.unsetPageSetup();
    }
    clonedSheet.setSelected(false);
    // clone the sheet drawing alongs with its relationships
    if (dg != null) {
        if (ct.isSetDrawing()) {
            // unset the existing reference to the drawing,
            // so that subsequent call of clonedSheet.createDrawingPatriarch() will create a new one
            ct.unsetDrawing();
        }
        XSSFDrawing clonedDg = clonedSheet.createDrawingPatriarch();
        // copy drawing contents
        clonedDg.getCTDrawing().set(dg.getCTDrawing());
        clonedDg = clonedSheet.createDrawingPatriarch();
        // Clone drawing relations
        List<RelationPart> srcRels = srcSheet.createDrawingPatriarch().getRelationParts();
        for (RelationPart rp : srcRels) {
            addRelation(rp, clonedDg);
        }
    }
    return clonedSheet;
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) POIXMLException(org.apache.poi.POIXMLException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 20 with POIXMLException

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

the class XSSFSheet method read.

protected void read(InputStream is) throws IOException {
    try {
        worksheet = WorksheetDocument.Factory.parse(is, DEFAULT_XML_OPTIONS).getWorksheet();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
    initRows(worksheet);
    columnHelper = new ColumnHelper(worksheet);
    // Look for bits we're interested in
    for (RelationPart rp : getRelationParts()) {
        POIXMLDocumentPart p = rp.getDocumentPart();
        if (p instanceof CommentsTable) {
            sheetComments = (CommentsTable) p;
        }
        if (p instanceof XSSFTable) {
            tables.put(rp.getRelationship().getId(), (XSSFTable) p);
        }
        if (p instanceof XSSFPivotTable) {
            getWorkbook().getPivotTables().add((XSSFPivotTable) p);
        }
    }
    // Process external hyperlinks for the sheet, if there are any
    initHyperlinks();
}
Also used : ColumnHelper(org.apache.poi.xssf.usermodel.helpers.ColumnHelper) XmlException(org.apache.xmlbeans.XmlException) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) CommentsTable(org.apache.poi.xssf.model.CommentsTable)

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