0

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.

1 Answers1

2

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.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • can u share the code for the same ? Cant find anything about this on the net. I am new to docx4j. – PraveEn KamaTh Dec 04 '14 at 11:39
  • @PraveEnKamaTh as above: "You can write code to generate the above yourself. Alternatively, *the commercial docx Enterprise edition would do it for you.*" – Ben Dec 04 '14 at 15:52
  • okay i have successfully attached excel to docx ..... now the problem is it needs to accept both xls and xlsx file formats. Code to attach .xls to .docx: OleObjectBinaryPart olePart = new OleObjectBinaryPart(); olePart.setBinaryData(oleFile); Relationship relOleObject = template.getMainDocumentPart().addTargetPart(olePart); Please provide solution to attach .xlsx to .docx – PraveEn KamaTh Jan 15 '15 at 12:37