Generate Word files in BS structure application based on template and XML

Source: Internet
Author: User
Tags format base64 header version uuid xmlns
word|xml| templates

Now the various types of applications often need to dynamically generate Word files, the most common way to solve the two methods are:

1. Use the COM Automation interface provided by Word to control word generation of documents

2. Use Word for HTML support, make HTML file, and set its contenttype to "Application/msword" so that the browser calls word to open

The first method relies on the fact that the word process actually completes the file generation, and Word is designed to handle concurrent calls on the server side, so this method is only suitable for stand-alone applications. In addition, if you want to generate more complex word files, the corresponding code will be very cumbersome, typesetting and debugging also need to spend a lot of time.
The fatal problem with the second approach is that binary data cannot be saved in an HTML document, such as a picture, and if a heap of files is generated on the server side, it is difficult to display in the browser or to allow the user to download directly.

A more complete solution is to generate Word files based on WordML. WordML is the XML schema that Microsoft has designed for word, and its complete syntax can be found on MSDN . By using this syntax to generate an XML file, you can freely get a variety of word-supported formats in your document. In particular, it is easy to embed a picture in a file, and its syntax is basically this:

<w:pict>
<v:shapetype id= "_x0000_t75" ...>
... VML shape Template Definition ...
</v:shapetype>
<w:bindata w:name= "Http://www.pushad.com/Info/wordml://02000001.jpg" >/9j/4aaqskzjrgabagaazabkaad/ 7aarrhvja3kaaqaeaaaapaaa/+4adkfkb2jlagtaaaaaaf/b
Aiqabgqebauebgufbgkgbqyjcwggbggldaokcwokdbamdawmdawqda4pea8odbmtfbqtexwbgxsc
... More BASE6 encoded picture file data
</w:binData>
<v:shape id= "_x0000_i1025" type= "#_x0000_t75"
style= "width: 212.4pt;height: 159pt ">
<v:imagedata src= "Http://www.pushad.com/Info/wordml://02000001.jpg"
o:title= "Image title"/>
</v:shape>
</w:pict>

The key is that the actual data of the picture is Base64 encoded and placed between the <w:binData></w:binData> tags.

If you look at the WordML syntax on MSDN , you'll find it very complex. That's not surprising because word has a lot of functionality, but it's a lot of trouble to write a program that generates documents based on so many definitions. But we can use the template file to solve this problem.

As we dynamically generate Web pages, there are quite a few parts of the word file to be dynamically generated, and complex layouts and formats are generally in these fixed parts. So borrow the idea of creating a Web page, we can in word first according to the required layout and format, combined with some virtual data to make template files, and then save the template file in XML format, and then in the XML file to find those virtual data corresponding to the text, Replace them with data processing instructions that will be used in the background processing technology, so that we can combine template files and database queries on the server side to generate Word files.

Let me give you an example. Let's say we use JSP for Word file generation, we first make the template file as follows:

personal resume of Song Dynasty

My Overview

Name

Lian Song

Date of birth

1937 7 months 7 days

< /f>

Gender

Man

Certificate type

Id

Degree

University degree

Certificate number

41043219700707011

Marital status

Married

Expected monthly salary

Negotiable

Account Location

Then save the file in XML format, and we'll get an XML file that reads:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<?mso-application progid= "Word.Document"?>
<w:worddocument xmlns:w= "Http://schemas.microsoft.com/office/word/2003/wordml"xmlns:v=" urn:schemas-microsoft-com:vml "xmlns:w10=" Urn:schemas-microsoft-com:office:word "xmlns:sl="Http://schemas.microsoft.com/schemaLibrary/2003/core"Xmlns:aml="Http://schemas.microsoft.com/aml/2001/core"Xmlns:wx="Http://schemas.microsoft.com/office/word/2003/auxHint"Xmlns:o=" Urn:schemas-microsoft-com:office:office "xmlns:dt=" uuid:c 2f41010-65b3-11d1-a 29f-00aa 00c14882 "xmlns:st1=" Urn:schemas-microsoft-com:office:smarttags "w:macrospresent=" no "w:embeddedObjPresent=" no "w : ocxpresent= "No" xml:space= "preserve" ><o:smarttagtype o:namespaceuri= "Urn:schemas-microsoft-com:office: SmartTags "O:url=" "O:name=" Chsdate "/><o:documentproperties><o:title> personal resume </o:Title>
... ...
<w:rpr><w:rfonts w:hint= "FAREAST"/><wx:font wx:val= "song Body"/></w:rpr><w:t> Lian song personal resume </w : T></w:r><w:prooferr w:type= "Gramstart"/><w:r><w:rpr><w:rfonts w:hint= "FAREAST"/ ><wx:font wx:val= "Song Body"/></w:rpr>
... ...
<w:pict><v:shapetype id= "_x0000_t75" coordsize= "21600,21600" o:spt= "o:preferrelative=" T "path=" m@4@5l@4@11@9@11@9@5xe"filled=" F "stroked=" F "><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-pixelHe ight "/><v:f eqn=" sum @10 21600 0 "/></v:formulas><v:path o:extrusionok=" F "gradientshapeok=" T "O: Connecttype= "rect"/><o:lock v:ext= "edit" aspectratio= "T"/></v:shapetype><w:bindata w:name= "http ://www.pushad.com/info/wordml://02000001.jpg ">/9j/4aaqskzjrgabagaazabkaad/7aarrhvja3kaaqaeaaaapaaa/+ 4adkfkb2jlagtaaaaaaf/b
... ...
</w:bindata><v:shape id= "_x0000_i1025" type= "#_x0000_t75" style= "width: 75pt;height: 75pt "><v:imagedata src=" http://www.pushad.com/Info/wordml://02000001.jpg "o:title=" Cathy " ></v:shape></w:pict>
... ...
</w:sectPr></wx:sub-section></wx:sect></w:body></w:wordDocument>

Next, the file is formatted in XML format, where you find the text part that needs to be dynamically generated (starting with the <w:t></w:t> tag in WordML), replacing it with a JSP instruction that takes data from the background to generate text. Then add the JSP control instruction to the file header and get the following XML file:

<%@ page language= "java" errorpage= "/error.jsp" pageencoding= "GBK" contenttype= "Application/msword;charset=utf-8" "%><%response.setheader (" content-disposition "," attachment; Filename=resume.doc ");%><?xml version=" 1.0 "encoding=" UTF-8 "standalone=" yes "?>

<%@ taglib uri= "http://java.sun.com/jstl/core" prefix= "C"%>
<%@ taglib uri= "http://java.sun.com/jstl/fmt" prefix= "FMT"%>


<?mso-application progid= "Word.Document"?>
<w:worddocument xmlns:w= "Http://schemas.microsoft.com/office/word/2003/wordml"xmlns:v=" urn:schemas-microsoft-com:vml "xmlns:w10=" Urn:schemas-microsoft-com:office:word "xmlns:sl="Http://schemas.microsoft.com/schemaLibrary/2003/core"Xmlns:aml="Http://schemas.microsoft.com/aml/2001/core"Xmlns:wx="Http://schemas.microsoft.com/office/word/2003/auxHint"Xmlns:o=" Urn:schemas-microsoft-com:office:office "xmlns:dt=" uuid:c 2f41010-65b3-11d1-a 29f-00aa 00c14882 "w:macrospresent=" no "w:embeddedobjpresent=" no "w:ocxpresent=" no "xml:space=" preserve ">
<o:DocumentProperties>
<o:Title> Personal Resume </o:Title>
... ...
<w:body>
<wx:sect>
<wx:sub-section>
<w:p>
<w:pPr>
<w:pstyle w:val= "A5"/>
<w:rPr>
<w:rfonts w:hint= "Fareast"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rfonts w:hint= "Fareast"/>
<wx:font wx:val= "Song Body"/>
</w:rPr>
<w:t><c:out value= "${command.name}"/> Personal resume </w:t>
</w:r>
</w:p>
... ...
<w:pict>
<v:shapetype id= "_x0000_t75" coordsize= "21600,21600" o:spt= "o:preferrelative=" T "path=" m@4@5l@4@11@9@11@9@5xe"filled=" F "stroked=" F ">
<v:stroke joinstyle= "Miter"/>
<v:path o:extrusionok= "F" gradientshapeok= "T" o:connecttype= "rect"/>
<o:lock v:ext= "edit" aspectratio= "T"/>
</v:shapetype>
<w:bindata w:name= "http://www.pushad.com/Info/wordml://02000001.jpg" ><c:out value= "${" COMMAND.PORTRAITBASE64} "/></w:bindata>
<v:shape id= "_x0000_i1025" type= "#_x0000_t75" style= "width: 75pt ; Height:            <v:imagedata src=" http:// Www.pushad.com/Info/wordml://02000001.jpg "o:title=" Portrait/>
           </v:shape>
         </w:pict >
...
 </w:body>
</w:wordDocument>

This file is stored in the JSP suffix, you can in the Java EE system with the background program to generate Word files. If you need a browser to prompt to save this file instead of opening it in the browser, you need to include this sentence in the header of the file:
Response.setheader ("Content-disposition", "attachment; Filename=resume.doc ");
There are a lot of free third-party libraries that can be used to generate BASE64 code for picture files, and BASE64 encoder in the JDK.

An easier way to do this is to enter the JSP directive directly into the dynamically generated part of the template file in Word, and then save it as an XML file, but be careful to check that word has been saved as an XML file to break the JSP instructions or insert formatting tags.

Take Jsp+jstl as an example, change to other background technology, such as velocity, or ASP, PHP, the principle is the same.

In fact, in applications that need to generate word files, most of the time you need to apply a fixed format so that the method explained in this article is more appropriate.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.