Search in sources :

Example 41 with POIXMLException

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

the class XSSFCellStyle method cloneStyleFrom.

/**
     * Clones all the style information from another
     *  XSSFCellStyle, onto this one. This
     *  XSSFCellStyle will then have all the same
     *  properties as the source, but the two may
     *  be edited independently.
     * Any stylings on this XSSFCellStyle will be lost!
     *
     * The source XSSFCellStyle could be from another
     *  XSSFWorkbook if you like. This allows you to
     *  copy styles from one XSSFWorkbook to another.
     */
@Override
public void cloneStyleFrom(CellStyle source) {
    if (source instanceof XSSFCellStyle) {
        XSSFCellStyle src = (XSSFCellStyle) source;
        // Is it on our Workbook?
        if (src._stylesSource == _stylesSource) {
            // Nice and easy
            _cellXf.set(src.getCoreXf());
            _cellStyleXf.set(src.getStyleXf());
        } else {
            // Copy the style
            try {
                //  avoid orphaned nodes
                if (_cellXf.isSetAlignment())
                    _cellXf.unsetAlignment();
                if (_cellXf.isSetExtLst())
                    _cellXf.unsetExtLst();
                // Create a new Xf with the same contents
                _cellXf = CTXf.Factory.parse(src.getCoreXf().toString(), DEFAULT_XML_OPTIONS);
                // bug 56295: ensure that the fills is available and set correctly
                CTFill fill = CTFill.Factory.parse(src.getCTFill().toString(), DEFAULT_XML_OPTIONS);
                addFill(fill);
                // bug 58084: set borders correctly
                CTBorder border = CTBorder.Factory.parse(src.getCTBorder().toString(), DEFAULT_XML_OPTIONS);
                addBorder(border);
                // Swap it over
                _stylesSource.replaceCellXfAt(_cellXfId, _cellXf);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
            // Copy the format
            String fmt = src.getDataFormatString();
            setDataFormat((new XSSFDataFormat(_stylesSource)).getFormat(fmt));
            // Copy the font
            try {
                CTFont ctFont = CTFont.Factory.parse(src.getFont().getCTFont().toString(), DEFAULT_XML_OPTIONS);
                XSSFFont font = new XSSFFont(ctFont);
                font.registerTo(_stylesSource);
                setFont(font);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
        }
        // Clear out cached details
        _font = null;
        _cellAlignment = null;
    } else {
        throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
    }
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) CTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont) CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException)

Example 42 with POIXMLException

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

the class XSSFDrawing method createObjectData.

@Override
public XSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
    XSSFSheet sh = getSheet();
    PackagePart sheetPart = sh.getPackagePart();
    /*
         * The shape id of the ole object seems to be a legacy shape id.
         * 
         * see 5.3.2.1 legacyDrawing (Legacy Drawing Object):
         * Legacy Shape ID that is unique throughout the entire document.
         * Legacy shape IDs should be assigned based on which portion of the document the
         * drawing resides on. The assignment of these ids is broken down into clusters of
         * 1024 values. The first cluster is 1-1024, the second 1025-2048 and so on.
         *
         * Ole shapes seem to start with 1025 on the first sheet ...
         * and not sure, if the ids need to be reindexed when sheets are removed
         * or more than 1024 shapes are on a given sheet (see #51332 for a similar issue)
         */
    XSSFSheet sheet = getSheet();
    XSSFWorkbook wb = sheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    long shapeId = (sheetIndex + 1) * 1024 + newShapeId();
    // add reference to OLE part
    PackagePartName olePN;
    try {
        olePN = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + storageId + ".bin");
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    PackageRelationship olePR = sheetPart.addRelationship(olePN, TargetMode.INTERNAL, POIXMLDocument.OLE_OBJECT_REL_TYPE);
    // add reference to image part
    XSSFPictureData imgPD = sh.getWorkbook().getAllPictures().get(pictureIndex);
    PackagePartName imgPN = imgPD.getPackagePart().getPartName();
    PackageRelationship imgSheetPR = sheetPart.addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    PackageRelationship imgDrawPR = getPackagePart().addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    // add OLE part metadata to sheet
    CTWorksheet cwb = sh.getCTWorksheet();
    CTOleObjects oo = cwb.isSetOleObjects() ? cwb.getOleObjects() : cwb.addNewOleObjects();
    CTOleObject ole1 = oo.addNewOleObject();
    ole1.setProgId("Package");
    ole1.setShapeId(shapeId);
    ole1.setId(olePR.getId());
    XmlCursor cur1 = ole1.newCursor();
    cur1.toEndToken();
    cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
    cur1.insertAttributeWithValue("defaultSize", "0");
    cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("moveWithCells", "1");
    CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
    XmlCursor cur2 = ctAnchor.newCursor();
    cur2.copyXmlContents(cur1);
    cur2.dispose();
    cur1.toParent();
    cur1.toFirstChild();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
    cur1.toNextSibling();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
    cur1.dispose();
    // add a new shape and link OLE & image part
    CTShape ctShape = ctAnchor.addNewSp();
    ctShape.set(XSSFObjectData.prototype());
    ctShape.getSpPr().setXfrm(createXfrm((XSSFClientAnchor) anchor));
    // workaround for not having the vmlDrawing filled
    CTBlipFillProperties blipFill = ctShape.getSpPr().addNewBlipFill();
    blipFill.addNewBlip().setEmbed(imgDrawPR.getId());
    blipFill.addNewStretch().addNewFillRect();
    CTNonVisualDrawingProps cNvPr = ctShape.getNvSpPr().getCNvPr();
    cNvPr.setId(shapeId);
    cNvPr.setName("Object " + shapeId);
    XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
    extCur.toFirstChild();
    extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
    extCur.dispose();
    XSSFObjectData shape = new XSSFObjectData(this, ctShape);
    shape.anchor = (XSSFClientAnchor) anchor;
    return shape;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) QName(javax.xml.namespace.QName) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTOleObjects(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObjects) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTOleObject(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject) CTTwoCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor)

Example 43 with POIXMLException

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

the class XSSFFont method getTypeOffset.

/**
     * get normal,super or subscript.
     *
     * @return short - offset type to use (none,super,sub)
     * @see Font#SS_NONE
     * @see Font#SS_SUPER
     * @see Font#SS_SUB
     */
public short getTypeOffset() {
    CTVerticalAlignFontProperty vAlign = _ctFont.sizeOfVertAlignArray() == 0 ? null : _ctFont.getVertAlignArray(0);
    if (vAlign == null) {
        return Font.SS_NONE;
    }
    int val = vAlign.getVal().intValue();
    switch(val) {
        case STVerticalAlignRun.INT_BASELINE:
            return Font.SS_NONE;
        case STVerticalAlignRun.INT_SUBSCRIPT:
            return Font.SS_SUB;
        case STVerticalAlignRun.INT_SUPERSCRIPT:
            return Font.SS_SUPER;
        default:
            throw new POIXMLException("Wrong offset value " + val);
    }
}
Also used : CTVerticalAlignFontProperty(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty) POIXMLException(org.apache.poi.POIXMLException)

Example 44 with POIXMLException

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

the class XSLFSheet method importBlip.

/**
     * Import a picture data from another document.
     *
     * @param blipId        ID of the package relationship to retrieve.
     * @param packagePart   package part containing the data to import
     * @return ID of the created relationship
     */
String importBlip(String blipId, PackagePart packagePart) {
    PackageRelationship blipRel = packagePart.getRelationship(blipId);
    PackagePart blipPart;
    try {
        blipPart = packagePart.getRelatedPart(blipRel);
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    XSLFPictureData data = new XSLFPictureData(blipPart);
    XMLSlideShow ppt = getSlideShow();
    XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
    PackagePart pic = pictureData.getPackagePart();
    RelationPart rp = addRelation(blipId, XSLFRelation.IMAGES, new XSLFPictureData(pic));
    return rp.getRelationship().getId();
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 45 with POIXMLException

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

the class XSLFNotesMaster method prototype.

private static CTNotesMaster prototype() {
    InputStream is = XSLFNotesMaster.class.getResourceAsStream("notesMaster.xml");
    if (is == null) {
        throw new POIXMLException("Missing resource 'notesMaster.xml'");
    }
    try {
        try {
            NotesMasterDocument doc = NotesMasterDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
            CTNotesMaster slide = doc.getNotesMaster();
            return slide;
        } finally {
            is.close();
        }
    } catch (Exception e) {
        throw new POIXMLException("Can't initialize NotesMaster", e);
    }
}
Also used : CTNotesMaster(org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMaster) InputStream(java.io.InputStream) NotesMasterDocument(org.openxmlformats.schemas.presentationml.x2006.main.NotesMasterDocument) POIXMLException(org.apache.poi.POIXMLException) XmlException(org.apache.xmlbeans.XmlException) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException)

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