简体   繁体   English

使用docx4j(或其他任何api)将excel附加到.docx

[英]attaching excel to .docx using docx4j (or any other api)

Currently I am working on a project which requires automating document generation. 目前,我正在从事一个需要自动生成文档的项目。 So far, i am successful in replacing placeholders with user-entered fields on the .docx using docx4j and also adding a .png image on the placeholder. 到目前为止,我已成功使用docx4j在.docx上用用户输入的字段替换了占位符,并在占位符上添加了.png图像。 Now, I need to attach an excel sheet on a particular placeholder on the .docx. 现在,我需要在.docx上的特定占位符上附加一个excel工作表。 Please suggest me some technique to do the same. 请建议我一些技巧来做同样的事情。

Thanks. 谢谢。

You should add the xlsx as an EmbeddedPackagePart 您应该将xlsx添加为EmbeddedPackagePart

The main document part will need to point to it, using XML similar to: 主要文档部分需要使用类似于以下内容的XML指向它:

            <w:p>
                <w:r>
                    <w:object w:dxaOrig="23793" w:dyaOrig="13287">
                        <v:shapetype stroked="f" filled="f" o:spt="75.0" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" coordsize="21600,21600" id="_x0000_t75">
                            <v:stroke joinstyle="miter"/>
                            <v:formulas>
                                <v:f eqn="if lineDrawn pixelLineWidth 0"/>
                                <v:f eqn="sum @0 1 0"/>
                                <v:f eqn="sum 0 0 @1"/>
                                <v:f eqn="prod @2 1 2"/>
                                <v:f eqn="prod @3 21600 pixelWidth"/>
                                <v:f eqn="prod @3 21600 pixelHeight"/>
                                <v:f eqn="sum @0 0 1"/>
                                <v:f eqn="prod @6 1 2"/>
                                <v:f eqn="prod @7 21600 pixelWidth"/>
                                <v:f eqn="sum @8 21600 0"/>
                                <v:f eqn="prod @7 21600 pixelHeight"/>
                                <v:f eqn="sum @10 21600 0"/>
                            </v:formulas>
                            <v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f"/>
                            <o:lock aspectratio="t" v:ext="edit"/>
                        </v:shapetype>
                        <v:shape type="#_x0000_t75" style="width:1189.5pt;height:664.5pt" id="_x0000_i1025" o:ole="">
                            <v:imagedata o:title="" r:id="rId5"/>
                        </v:shape>
                        <o:OLEObject Type="Embed" ProgID="Excel.Sheet.12" ShapeID="_x0000_i1025" DrawAspect="Content" ObjectID="_1479233503" r:id="rId6"/>
                    </w:object>
                </w:r>
            </w:p>

Note the 2 rel ids; 注意2个rel ids; in this example rId6 points to the Emmbedded part, and rId5 points to the image to appear on the document surface (you need to create that yourself). 在此示例中,rId6指向“嵌入”部分,而rId5指向要显示在文档表面上的图像(您需要自己创建)。

You can write code to generate the above yourself. 您可以编写代码自己生成上述代码。 Alternatively, the commercial docx Enterprise edition would do it for you. 或者,商业docx企业版将为您完成此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM