Use the ActiveXObject control in javascript to replace word bookmarks. Export the content to word and print page 1/2.

Source: Internet
Author: User

Recently, you need to export data to word and edit and print the data.
I have thought of several solutions:
1. Use jacob.
2. Use apache poi.
3. Use itext.
Because of the tight schedule, I have to study the above toolkit in a few cases. Now I use javascript to operate the ActiveXObject control and replace the bookmarks in the word template.

Prerequisites:
1. the browser security level is reduced. You can use the ActiveXObject control.

2. Install office word.

Currently, you can replace a single bookmarks, multi-row table bookmarks, and images to meet your needs. However, there are still many ways to use word that are unclear. Most of the Web applications use VB. If you have any questions, you can contact us.

The following describes my design implementation ideas:

First of all, define the word template and add the tag to the place to be replaced. Menu-insert-bookmarks, INPUT attribute names, such as year, date, pic1, and voList.
Print page:
The printed data needs to be taken out from the background, and taken a single vo (an object) as a group, or voList (a list set of objects) after the data is obtained for a group of organized pages, replace the data.
The data is organized as follows:
<Div id = "export2word">
<Form id = "singleVo" name = "singleVo">
<Textarea name = "jcxs" style = "display: none"> <c: out value = "$ {zywstjfxbgVO. jcxs}"/> </textarea>
<Textarea name = "xcjhl" style = "display: none"> <c: out value = "$ {zywstjfxbgVO. xcjhl}"/> </textarea>
<Textarea name = "tbjcxs" style = "display: none"> <c: out value = "$ {tbjcxs}"/> </textarea>
<Textarea name = "tptest" style = "display: none"> ../zwgl/zw008-ZwMkjbxxCTRL-showWxytp.png? Xh = 3041 </textarea>
</Form>

<C: forEach var = "mxvo" items = "$ {jgList}" varStatus = "s">
<Form name = "mxvoForm">
<! -- Note: The width here is set to the table cell width (cm * 100) -->
<Textarea name = "tbjcmcrs" style = "width: 349; display: none"> <c: out value = "$ {mxvo. tbjcmcrs}"/> </textarea>
<Textarea name = "tbjcmcrsbl" style = "width: 270; display: none"> <c: out value = "$ {mxvo. tbjcmcrsbl}"/> </textarea>
<Textarea name = "tbjcxs" style = "width: 477; display: none"> <c: out value = "$ {mxvo. tbjcxs}"/> </textarea>
<Textarea name = "tbjcxsbl" style = "display: none"> <c: out value = "$ {mxvo. tbjcxsbl}"/> </textarea>
</Form>
</C: forEach>
</Div>

Usage:
<Input type = "button" id = "select2" name = "select2" class = "button" value = "export data" onclick = "print2doc ();">

<Script type = "text/javascript" src = "../public/scripts/export2word. js"> </script>
<Script type = "text/javascript">
Function print2doc (){
// The parameter is the template (relative to the page) path.
Var word = new WordApp ("test.doc ");
// The parameter is the form name, and the attributes to be added in vo (all attributes in form are empty)
Var vo = word. getSingleVo ("singleVo", ["jcxs", "xcjhl", "tbjcxs"]);
// Var vo = word. getSingleVo ("singleVo ");
// Organize the image into vo
Var tpvo = word. getSingleVo ("singleVo", ["tptest"]);
// The parameter is the form name and the attributes to be added. (The order is the order in which table columns are generated. If the parameter is empty, all attributes and sequences in form are added)
Var voList = word. getVoList ("mxvoForm", ["tbjcmcrs", "tbjcmcrsbl", "tbjczsrs"]);
// Var voList = word. getVoList ("mxvoForm ");
// Replace normal bookmarks
Word. replaceBookmarkUsevo (vo );
// Replace the image bookmarks
Word. replaceBookmarkUsepicvo (tpvo );
// Replace the bookmark jgList and draw a table to form multiple rows of data.
Word. replaceBookmarkUsevolist ("jgList", voList );
// Visible to the document
Word. wordObj. visible = true;
// Word. closeApp ();
}
</Script>
Note:
To replace the image value, you need to explain the following:
1. You can set the path relative to this page, for example, ../zbgl/abc.png.
2. If it is an output stream, you need to map the url of the Request output stream to the end of the image format. For example,/.../abc. do? Change id = 123 to/../abc.png? Id = 123
You can configure a servlet in web. xml, for example, converting a *. png request to. do. For example:

Public class PngDispatcherServlet extends HttpServlet {

Private static final long serialVersionUID = 6230740581031996144L;

Public void init () throws ServletException {

}

Public void doPost (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
DoGet (request, response );
}

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

// StringBuffer url = request. getRequestURL ();
StringBuffer url = new StringBuffer (request. getRequestURI ());
If (request. getQueryString ()! = Null ){
Url. append ('? ');
Url. append (request. getQueryString ());
}
String newUrl = url. toString (). replaceAll (". png", ". do ");
ServletContext SC = getServletContext ();
RequestDispatcher rd = SC. getRequestDispatcher (newUrl); // targeted page
Rd. forward (request, response );
}
}

Related Article

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.