简体   繁体   中英

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. Now, I need to attach an excel sheet on a particular placeholder on the .docx. Please suggest me some technique to do the same.

Thanks.

You should add the xlsx as an EmbeddedPackagePart

The main document part will need to point to it, using XML similar to:

            <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; 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).

You can write code to generate the above yourself. Alternatively, the commercial docx Enterprise edition would do it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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