Search in sources :

Example 26 with POIXMLDocumentPart

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

the class XDGFMasters method onDocumentRead.

@Override
protected void onDocumentRead() {
    try {
        try {
            _mastersObject = MastersDocument.Factory.parse(getPackagePart().getInputStream()).getMasters();
        } catch (XmlException e) {
            throw new POIXMLException(e);
        } catch (IOException e) {
            throw new POIXMLException(e);
        }
        Map<String, MasterType> masterSettings = new HashMap<String, MasterType>();
        for (MasterType master : _mastersObject.getMasterArray()) {
            masterSettings.put(master.getRel().getId(), master);
        }
        // create the masters
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart part = rp.getDocumentPart();
            String relId = rp.getRelationship().getId();
            MasterType settings = masterSettings.get(relId);
            if (settings == null) {
                throw new POIXMLException("Master relationship for " + relId + " not found");
            }
            if (!(part instanceof XDGFMasterContents)) {
                throw new POIXMLException("Unexpected masters relationship for " + relId + ": " + part);
            }
            XDGFMasterContents contents = (XDGFMasterContents) part;
            contents.onDocumentRead();
            XDGFMaster master = new XDGFMaster(settings, contents, _document);
            _masters.put(master.getID(), master);
        }
    } catch (POIXMLException e) {
        throw XDGFException.wrap(this, e);
    }
}
Also used : HashMap(java.util.HashMap) XmlException(org.apache.xmlbeans.XmlException) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) IOException(java.io.IOException) MasterType(com.microsoft.schemas.office.visio.x2012.main.MasterType)

Example 27 with POIXMLDocumentPart

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

the class TestXSLFChart method testFillChartTemplate.

/**
     * a modified version from POI-examples
     */
@Test
public void testFillChartTemplate() throws IOException {
    // first line is chart title
    String chartTitle = "Apache POI";
    XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("pie-chart.pptx");
    XSLFSlide slide = pptx.getSlides().get(0);
    // find chart in the slide
    XSLFChart chart = null;
    for (POIXMLDocumentPart part : slide.getRelations()) {
        if (part instanceof XSLFChart) {
            chart = (XSLFChart) part;
            break;
        }
    }
    if (chart == null)
        throw new IllegalStateException("chart not found in the template");
    // embedded Excel workbook that holds the chart data
    POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    CTChart ctChart = chart.getCTChart();
    CTPlotArea plotArea = ctChart.getPlotArea();
    CTPieChart pieChart = plotArea.getPieChartArray(0);
    //Pie Chart Series
    CTPieSer ser = pieChart.getSerArray(0);
    // Series Text
    CTSerTx tx = ser.getTx();
    tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
    sheet.createRow(0).createCell(1).setCellValue(chartTitle);
    String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
    tx.getStrRef().setF(titleRef);
    // Category Axis Data
    CTAxDataSource cat = ser.getCat();
    CTStrData strData = cat.getStrRef().getStrCache();
    // Values
    CTNumDataSource valSrc = ser.getVal();
    CTNumData numData = valSrc.getNumRef().getNumCache();
    // unset old axis text
    strData.setPtArray(null);
    // unset old values
    numData.setPtArray(null);
    Map<String, Double> pieModel = new LinkedHashMap<String, Double>();
    pieModel.put("First", 1.0);
    pieModel.put("Second", 3.0);
    pieModel.put("Third", 4.0);
    // set model
    int idx = 0;
    int rownum = 1;
    for (String key : pieModel.keySet()) {
        double val = pieModel.get(key);
        CTNumVal numVal = numData.addNewPt();
        numVal.setIdx(idx);
        numVal.setV("" + val);
        CTStrVal sVal = strData.addNewPt();
        sVal.setIdx(idx);
        sVal.setV(key);
        idx++;
        XSSFRow row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(key);
        row.createCell(1).setCellValue(val);
    }
    numData.getPtCount().setVal(idx);
    strData.getPtCount().setVal(idx);
    String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
    valSrc.getNumRef().setF(numDataRange);
    String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
    cat.getStrRef().setF(axisDataRange);
    // updated the embedded workbook with the data
    OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
    wb.write(xlsOut);
    xlsOut.close();
    wb.close();
}
Also used : CTStrData(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData) CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) OutputStream(java.io.OutputStream) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) CTSerTx(org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx) CellReference(org.apache.poi.ss.util.CellReference) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource) LinkedHashMap(java.util.LinkedHashMap) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CTNumData(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData) CTPieChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart) CTPieSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer) CTStrVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal) CTNumVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Example 28 with POIXMLDocumentPart

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

the class XWPFPicture method getPictureData.

/**
     * Get the PictureData of the Picture, if present.
     * Note - not all kinds of picture have data
     */
public XWPFPictureData getPictureData() {
    CTBlipFillProperties blipProps = ctPic.getBlipFill();
    if (blipProps == null || !blipProps.isSetBlip()) {
        // return null if Blip data is missing
        return null;
    }
    String blipId = blipProps.getBlip().getEmbed();
    POIXMLDocumentPart part = run.getParent().getPart();
    if (part != null) {
        POIXMLDocumentPart relatedPart = part.getRelationById(blipId);
        if (relatedPart instanceof XWPFPictureData) {
            return (XWPFPictureData) relatedPart;
        }
    }
    return null;
}
Also used : CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart)

Example 29 with POIXMLDocumentPart

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

the class TestXSSFExportToXML method testXmlExportCompare_Bug_55923.

public void testXmlExportCompare_Bug_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);
        assertEquals(0, exporter.compare("", ""));
        assertEquals(0, exporter.compare("/", "/"));
        assertEquals(0, exporter.compare("//", "//"));
        assertEquals(0, exporter.compare("/a/", "/b/"));
        assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR", "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1: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)

Example 30 with POIXMLDocumentPart

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

the class XSLFSlideMaster method getTheme.

@Override
public XSLFTheme getTheme() {
    if (_theme == null) {
        for (POIXMLDocumentPart p : getRelations()) {
            if (p instanceof XSLFTheme) {
                _theme = (XSLFTheme) p;
                CTColorMapping cmap = _slide.getClrMap();
                if (cmap != null) {
                    _theme.initColorMap(cmap);
                }
                break;
            }
        }
    }
    return _theme;
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) CTColorMapping(org.openxmlformats.schemas.drawingml.x2006.main.CTColorMapping)

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