如何将xlsx文件添加到powerpoint演示文稿?

时间:2015-04-13 14:39:25

标签: java xml powerpoint docx4j

我想将现有的excel表添加到幻灯片中。使用OleObjectBinaryPart将excel文件成功添加到演示文稿中。现在我想使用以下xml代码将其添加到幻灯片中......

<p:graphicFrame xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
            xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
            xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
<p:nvGraphicFramePr>
    <p:cNvPr id="3" name="Objekt 2"/>
    <p:cNvGraphicFramePr>
        <a:graphicFrameLocks noChangeAspect="1"/>
    </p:cNvGraphicFramePr>
    <p:nvPr>
        <p:extLst>
            <p:ext uri="{D42A27DB-BD31-4B8C-83A1-F6EECF244321}">
                <p14:modId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main"
                           val="3436193848"/>
            </p:ext>
        </p:extLst>
    </p:nvPr>
</p:nvGraphicFramePr>
<p:xfrm>
    <a:off x="1524000" y="1433513"/>
    <a:ext cx="6096000" cy="3989387"/>
</p:xfrm>
<a:graphic>
    <a:graphicData uri="http://schemas.openxmlformats.org/presentationml/2006/ole">
        <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
            <mc:Choice xmlns:v="urn:schemas-microsoft-com:vml" Requires="v">
                <p:oleObj spid="_x0000_s1026" name="Arbeitsblatt" r:id="${OLEObjectRid}" imgW="18573822"
                          imgH="12154029" progId="Excel.Sheet.12">
                    <p:embed/>
                </p:oleObj>
            </mc:Choice>
            <mc:Fallback>
                <p:oleObj name="Arbeitsblatt" r:id="${OLEObjectRid}" imgW="18573822" imgH="12154029"
                          progId="Excel.Sheet.12">
                    <p:embed/>
                    <p:pic>
                        <p:nvPicPr>
                            <p:cNvPr id="0" name=""/>
                            <p:cNvPicPr/>
                            <p:nvPr/>
                        </p:nvPicPr>
                        <p:blipFill>
                            <a:blip r:embed="${ImageId}"/>
                            <a:stretch>
                                <a:fillRect/>
                            </a:stretch>
                        </p:blipFill>
                        <p:spPr>
                            <a:xfrm>
                                <a:off x="1524000" y="1433513"/>
                                <a:ext cx="6096000" cy="3989387"/>
                            </a:xfrm>
                            <a:prstGeom prst="rect">
                                <a:avLst/>
                            </a:prstGeom>
                        </p:spPr>
                    </p:pic>
                </p:oleObj>
            </mc:Fallback>
        </mc:AlternateContent>
    </a:graphicData>
</a:graphic>
</p:graphicFrame>

...使用这行java代码:

        OleObjectBinaryPart olePart;
    Relationship relOleObject = null;
    BinaryPartAbstractImage imagePart = null;

    // OleObject
    try {
        olePart = new OleObjectBinaryPart(new PartName("/ppt/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx"));
        // get the excel-file
        olePart.setBinaryData(getXlsxAsInputStream());
        relOleObject = presentationMLPackage.getMainPresentationPart().addTargetPart(olePart);
    } catch (InvalidFormatException e) {
        log.error("Could not create OlePart. ", e);
        e.printStackTrace();
    }

    // Preview image
    InputStream inputStream = this.getClass().getResourceAsStream("/images/beispielbild_gross1.jpg");
    byte[] bytes;
    try {
        bytes = IOUtils.toByteArray(inputStream);
        imagePart = BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, bytes);
    } catch (IOException e) {
        log.error("Could not convert image to byte[]. ", e);
    } catch (Exception e) {
        log.error("Could not add image to presentation. ", e);
    }

    // The image the user sees, that they click on to open the object
    Relationship relImage = null;
    try {
        relImage = presentationMLPackage.getMainPresentationPart().addTargetPart(imagePart);
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    }

    // get content of xml-file
    String ml = readFile("/xml/oleObject.xml");

    java.util.HashMap<String, String> mappings = new java.util.HashMap<String, String>();
    mappings.put("ImageId", relImage.getId());
    mappings.put("OLEObjectRid", relOleObject.getId());

    try {
        CTGraphicalObjectFrame sample = (CTGraphicalObjectFrame) XmlUtils.unmarshallFromTemplate(ml, mappings, Context.jcPML, CTGraphicalObjectFrame.class);
        slidePart.getContents().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
    } catch (JAXBException e) {
        log.error("Could not add content to slide.", e);
    }

但解组xml-Code(替换正常)我收到以下错误:

  

javax.xml.bind.UnmarshalException:意外元素   (URI: “http://schemas.openxmlformats.org/presentationml/2006/main”,   本地: “graphicFrame”)。预期的元素是[......很多元素,但是   不是graphicFrame ...]

那么我必须使用哪个元素而不是graphicFrame?或者是我的问题在其他地方?

更新:CTGraphicalObjectFrame.class添加到解组函数可修复问题。但我无法打开已完成的文件,MS Office表示它已损坏。任何提示?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您需要为JAXB提供一个提示:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.docx4j.XmlUtils;
import org.pptx4j.jaxb.Context;
import org.pptx4j.pml.CTGraphicalObjectFrame;

public class FileToPML {
public static void main(String[] args) throws Exception {

    String inputfilepath = System.getProperty("user.dir") + "/pml.xml";
    java.io.FileInputStream fin = new java.io.FileInputStream(inputfilepath);

    // Create Unmarshaller
    JAXBContext jc = Context.jcPML;
    Unmarshaller u = jc.createUnmarshaller();
    u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());

    // Unmarshall, giving JAXB a hint
    Object o = u.unmarshal(new javax.xml.transform.stream.StreamSource(fin), 
            CTGraphicalObjectFrame.class);

    // Prove it worked
    System.out.println(o.getClass().getName());
    System.out.println(XmlUtils.marshaltoString(o, Context.jcPML));

}

}