Recently, my company has given me more and more interesting tasks. In the past two days, we have to change the jquery style to the tab style. This is not difficult, but the most troublesome thing is the business. Well, of course this is not the focus of today's blog.
A task just completed a few days ago is to use poi to export the word based on the template. It takes one and a half days to complete the task. At first, I thought it was a technical activity, so I was very happy to go online to check the information. I found it all over again. I never understood what was going on. I finally saw a website, this is the case: (paste my Reference URL first)
Http://topic.csdn.net/u/20110415/17/6cfbe1a1-ccdf-420e-b868-38b9a4ca13e0.html (last sentence, save the template as an XML file, and then rewrite the XML)
Save the template as an XML file and then rewrite the XML file. This sentence is the focus of solving the problem. After confirming it, it will not be a technical problem, but a dynamic force. Haha.
The specific process is as follows:
1. Define the Word template file:
Open your template word and save it as an XML format. Add the corresponding tag with an attribute, for example, ID, to facilitate future operations. Put it in a directory (prepare for subsequent calls)
2. Prepare the environment for reading and writing XML files and the poi Environment
I use dom4j, so I can copy the jar package of dom4j to the project. Import the jar package related to poi into the project.
3. Read and Write XML files
The Code is as follows:
HttpServletResponse response = ServletActionContext.getResponse();HttpServletRequest request = ServletActionContext.getRequest();String destFilePath = request.getSession().getServletContext().getRealPath("\\");
// Use dom4j to read and write XML files (template files) saxreader reader = new saxreader (); // The template file is conf \ testreporttemplate. XML document = reader. read (new file (destfilepath + "WEB-INF \ testreporttemplate. XML "); element root = document. getrootelement (); // read the relevant node information (the following labels in the XML template file are in the form of W: body, W: sect, etc, however, you cannot add W: to read the data, and you must add W: to add the content. For example, we usually use W to add P, R, and T: p W: r w: T. Element wbody = root. element ("body"); element wsect = wbody. element ("sect"); // read the information of the related section, that is, the specific content of the template. List <element> sections = wsect, which is to be filled in the data content area. elements ("sub-section"); // loop section. Based on the ID value of the section label, the relevant area is obtained and the data is filled in for (element elesection: sections) {string elesectionid = elesection. attributevalue ("ID"); If ("testmethodsection ". equalsignorecase (elesectionid) {If (testingworkinfovo. gettesting Method ()! = NULL) {elesection. addelement ("W: P "). addelement ("W: R "). addelement ("W: t "). settext (testingworkinfovo. gettestingmethod (); // data read in the database} continue;} If ("testcontentsection ". equalsignorecase (elesectionid) {If (testingworkinfovo. gettestingcontent ()! = NULL) {elesection. addelement ("W: P "). addelement ("W: R "). addelement ("W: t "). settext (testingworkinfovo. gettestingcontent (); // data read from the database} continue ;}}
4. Export POI as word
Bytearrayinputstream BAIS = new bytearrayinputstream (document. asxml (). getbytes ("UTF-8"); poifsfilesystem FS = new poifsfilesystem (); directoryentry directory = FS. getroot (); documententry de = directory. createdocument ("worddocument", BAIS); // file output stream outputstream Fos = response. getoutputstream (); // file output information string outputfilename = NULL; // output file name string destfile = destfilepath + "word_template \"; // output file address; outputfilename = "test report" + ". doc "; destfile = destfilepath + outputfilename; // file response. setcontenttype ("application/octet-stream"); response. setheader ("name", destfile); response. setheader ("content-disposition", "attachment; filename = \" "+ urlencoder. encode (outputfilename, "UTF-8") + "\" "); // FOS. flush (); FS. writefilesystem (FOS); BAIS. close (); FOS. flush (); FOS. close ();
5. It's almost done. Haha. If you have any questions, please feel free to leave a message. I will reply to you soon.
6. Add: the XML code of the Word template in XML format is slightly pasted (different from the above example ):
<W: document MC: ignorable = "W14 wp14" xmlns: WPC = "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: O = "urn: Schemas-Microsoft-com: office: office "xmlns: r =" http://schemas.openxmlformats.org/officeDocument/2006/relationships "xmlns: M =" http://schemas.openxmlformats.org/officeDocument/2006/math "xmlns: V =" urn: Schemas-Microsoft-com: VML "xmlns: wp14 = "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns: Wp = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns: W10 = "urn: Schemas-Microsoft-com: Office: Word" xmlns: W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns: w14 = "http://schemas.microsoft.com/office/word/2010/wordml" xmlns: WPG = "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns: WPI = "http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns: wne = "http://schemas.microsoft.com/office/word/2006/wordml" xmlns: WPS = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"> <W: body> <W: p W: rsidr = "007b7334" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: pstyle W: val = "2"/> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> </W: PPR> <W: r> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> <W: T> 1. </W: t> </W: r> <W: bookmarkstart W: Id = "0" W: Name = "_ Goback"/> <W: bookmarkend W: id = "0"/> <W: r> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> <W: t> test template 1 </W: T> </W: r> </W: P> <W: p W: rsidr = "00fb1288" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> </W: PPR> </W: P> <W: p W: rsidr = "00fb1288" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> </W: PPR> </W: P> <W: p W: rsidr = "00fb1288" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> </W: PPR> </W: p> <W: p W: rsidr = "00fb1288" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> </W: PPR> </W: P> <W: p W: rsidr = "00fb1288" W: rsidrdefault = "00fb1288" W: rsidp = "00fb1288"> <W: PPR> <W: pstyle W: val = "2"/> </W: PPR> <W: r> <W: RPR> <W: rfonts W: hint = "Eastasia"/> </W: RPR> <W: T> 2. test template 2 </W: T> </W: r> </W: P> <W: sectpr W: rsidr = "00fb1288"> <W: pgsz W: W = "11906" W: H = "16838"/> <W: pgmar W: Top = "1440" W: right = "1800" W: Bottom = "1440" W: Left = "1800" W: Header = "851" W: footer = "992" W: gutter = "0"/> <W: Cols W: Space = "425"/> <W: docgrid W: TYPE = "lines" W: linepitch = "312"/> </W: sectpr> </W: Body> </W: document>