Search in sources :

Example 16 with POIXMLDocumentPart

use of org.apache.poi.POIXMLDocumentPart 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 17 with POIXMLDocumentPart

use of org.apache.poi.POIXMLDocumentPart 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 18 with POIXMLDocumentPart

use of org.apache.poi.POIXMLDocumentPart 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)

Example 19 with POIXMLDocumentPart

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

the class XSSFSheet method getVMLDrawing.

/**
     * Get VML drawing for this sheet (aka 'legacy' drawig)
     *
     * @param autoCreate if true, then a new VML drawing part is created
     *
     * @return the VML drawing of <code>null</code> if the drawing was not found and autoCreate=false
     */
protected XSSFVMLDrawing getVMLDrawing(boolean autoCreate) {
    XSSFVMLDrawing drawing = null;
    CTLegacyDrawing ctDrawing = getCTLegacyDrawing();
    if (ctDrawing == null) {
        if (autoCreate) {
            //drawingNumber = #drawings.size() + 1
            int drawingNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.VML_DRAWINGS.getContentType()).size() + 1;
            RelationPart rp = createRelationship(XSSFRelation.VML_DRAWINGS, XSSFFactory.getInstance(), drawingNumber, false);
            drawing = rp.getDocumentPart();
            String relId = rp.getRelationship().getId();
            //add CTLegacyDrawing element which indicates that this sheet contains drawing components built on the drawingML platform.
            //The relationship Id references the part containing the drawing definitions.
            ctDrawing = worksheet.addNewLegacyDrawing();
            ctDrawing.setId(relId);
        }
    } else {
        //search the referenced drawing in the list of the sheet's relations
        final String id = ctDrawing.getId();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof XSSFVMLDrawing) {
                XSSFVMLDrawing dr = (XSSFVMLDrawing) p;
                String drId = rp.getRelationship().getId();
                if (drId.equals(id)) {
                    drawing = dr;
                    break;
                }
            // do not break here since drawing has not been found yet (see bug 52425)
            }
        }
        if (drawing == null) {
            logger.log(POILogger.ERROR, "Can't find VML drawing with id=" + id + " in the list of the sheet's relationships");
        }
    }
    return drawing;
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart)

Example 20 with POIXMLDocumentPart

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

the class TestXSSFExportToXML method testXmlExportSchemaOrderingBug_Bugzilla_55923.

public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
    boolean found = false;
    for (POIXMLDocumentPart p : wb.getRelations()) {
        if (!(p instanceof MapInfo)) {
            continue;
        }
        MapInfo mapInfo = (MapInfo) p;
        XSSFMap map = mapInfo.getXSSFMapById(4);
        assertNotNull("XSSFMap is null", map);
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xmlData = os.toString("UTF-8");
        assertNotNull(xmlData);
        assertFalse(xmlData.equals(""));
        String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
        String a_b = a.split("<B>")[1].split("</B>")[0].trim();
        String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
        String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("1", a_b_c_e_euro);
        assertEquals("2", a_b_c_e_chf);
        String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
        String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("3", a_b_d_e_euro);
        assertEquals("4", a_b_d_e_chf);
        found = true;
    }
    assertTrue(found);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)42 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)16 MapInfo (org.apache.poi.xssf.model.MapInfo)14 XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 POIXMLException (org.apache.poi.POIXMLException)9 XmlException (org.apache.xmlbeans.XmlException)7 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)3 HashMap (java.util.HashMap)3 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 CTAxDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)2 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)2