Search in sources :

Example 66 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSLFTextRun method getFontColor.

@Override
public PaintStyle getFontColor() {
    final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null;
    CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()) {

        public boolean fetch(CTTextCharacterProperties props) {
            if (props == null) {
                return false;
            }
            XSLFShape shape = _p.getParentShape();
            CTShapeStyle style = shape.getSpStyle();
            CTSchemeColor phClr = null;
            if (style != null && style.getFontRef() != null) {
                phClr = style.getFontRef().getSchemeClr();
            }
            XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(props);
            XSLFSheet sheet = shape.getSheet();
            PackagePart pp = sheet.getPackagePart();
            XSLFTheme theme = sheet.getTheme();
            PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder);
            if (ps != null) {
                setValue(ps);
                return true;
            }
            return false;
        }
    };
    fetchCharacterProperty(fetcher);
    return fetcher.getValue();
}
Also used : CTShapeStyle(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeStyle) CharacterPropertyFetcher(org.apache.poi.xslf.model.CharacterPropertyFetcher) CTTextCharacterProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XSLFFillProperties(org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFFillProperties)

Example 67 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSSFSheet method commit.

@Override
protected void commit() throws IOException {
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    write(out);
    out.close();
}
Also used : OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 68 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSSFSheet method createPivotTable.

/**
     * Creates an empty XSSFPivotTable and sets up all its relationships
     * including: pivotCacheDefinition, pivotCacheRecords
     * @return returns a pivotTable
     */
@SuppressWarnings("resource")
@Beta
private XSSFPivotTable createPivotTable() {
    XSSFWorkbook wb = getWorkbook();
    List<XSSFPivotTable> pivotTables = wb.getPivotTables();
    int tableId = getWorkbook().getPivotTables().size() + 1;
    //Create relationship between pivotTable and the worksheet
    XSSFPivotTable pivotTable = (XSSFPivotTable) createRelationship(XSSFRelation.PIVOT_TABLE, XSSFFactory.getInstance(), tableId);
    pivotTable.setParentSheet(this);
    pivotTables.add(pivotTable);
    XSSFWorkbook workbook = getWorkbook();
    //Create relationship between the pivot cache defintion and the workbook
    XSSFPivotCacheDefinition pivotCacheDefinition = (XSSFPivotCacheDefinition) workbook.createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, XSSFFactory.getInstance(), tableId);
    String rId = workbook.getRelationId(pivotCacheDefinition);
    //Create relationship between pivotTable and pivotCacheDefinition without creating a new instance
    PackagePart pivotPackagePart = pivotTable.getPackagePart();
    pivotPackagePart.addRelationship(pivotCacheDefinition.getPackagePart().getPartName(), TargetMode.INTERNAL, XSSFRelation.PIVOT_CACHE_DEFINITION.getRelation());
    pivotTable.setPivotCacheDefinition(pivotCacheDefinition);
    //Create pivotCache and sets up it's relationship with the workbook
    pivotTable.setPivotCache(new XSSFPivotCache(workbook.addPivotCache(rId)));
    //Create relationship between pivotcacherecord and pivotcachedefinition
    XSSFPivotCacheRecords pivotCacheRecords = (XSSFPivotCacheRecords) pivotCacheDefinition.createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, XSSFFactory.getInstance(), tableId);
    //Set relationships id for pivotCacheDefinition to pivotCacheRecords
    pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setId(pivotCacheDefinition.getRelationId(pivotCacheRecords));
    wb.setPivotTables(pivotTables);
    return pivotTable;
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Beta(org.apache.poi.util.Beta)

Example 69 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class XSSFWorkbook method getAllPictures.

/**
     * Gets all pictures from the Workbook.
     *
     * @return the list of pictures (a list of {@link XSSFPictureData} objects.)
     * @see #addPicture(byte[], int)
     */
@Override
public List<XSSFPictureData> getAllPictures() {
    if (pictures == null) {
        List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
        pictures = new ArrayList<XSSFPictureData>(mediaParts.size());
        for (PackagePart part : mediaParts) {
            pictures.add(new XSSFPictureData(part));
        }
    }
    //YK: should return Collections.unmodifiableList(pictures);
    return pictures;
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 70 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class UpdateEmbeddedDoc method updateEmbeddedDoc.

/**
     * Called to update the embedded Excel workbook. As the format and structire
     * of the workbook are known in advance, all this code attempts to do is
     * write a new value into the first cell on the first row of the first
     * worksheet. Prior to executing this method, that cell will contain the
     * value 1.
     *
     * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
     *                             Rather
     *                             than use the specific classes (HSSF/XSSF) to handle the embedded
     *                             workbook this method uses those defeined in the SS stream. As
     *                             a result, it might be the case that a SpreadsheetML file is
     *                             opened for processing, throwing this exception if that file is
     *                             invalid.
     * @throws java.io.IOException Thrown if a problem occurs in the underlying
     *                             file system.
     */
public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    for (PackagePart pPart : embeddedDocs) {
        String ext = pPart.getPartName().getExtension();
        if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
            // Get an InputStream from the package part and pass that
            // to the create method of the WorkbookFactory class. Update
            // the resulting Workbook and then stream that out again
            // using an OutputStream obtained from the same PackagePart.
            InputStream is = pPart.getInputStream();
            Workbook workbook = null;
            OutputStream os = null;
            try {
                workbook = WorkbookFactory.create(is);
                Sheet sheet = workbook.getSheetAt(SHEET_NUM);
                Row row = sheet.getRow(ROW_NUM);
                Cell cell = row.getCell(CELL_NUM);
                cell.setCellValue(NEW_VALUE);
                os = pPart.getOutputStream();
                workbook.write(os);
            } finally {
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(workbook);
                IOUtils.closeQuietly(is);
            }
        }
    }
    if (!embeddedDocs.isEmpty()) {
        // Finally, write the newly modified Word document out to file.
        FileOutputStream fos = new FileOutputStream(this.docFile);
        this.doc.write(fos);
        fos.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Row(org.apache.poi.ss.usermodel.Row) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5