JavaScript replaces word bookmarks with activexobject controls, export content to Word after printing 1/2 page _javascript tips

Source: Internet
Author: User
Tags stringbuffer

Recently, there is a need to export the data to Word, and then edit the print.
Thought about several scenarios:
1. Use Jacob.
2. Use Apache poi.
3. Use Itext.
Because time is tight, there is not much time to study the above toolkit, now use JavaScript to manipulate the ActiveXObject control, with the substitution of the bookmark in the Word template to resolve.

Prerequisites:
1. The browser security level is reduced and you can use the ActiveXObject control.

2. Equipped with Office Word.

Currently implemented replaces individual bookmarks, multiple lines of table bookmarks, and pictures that basically meet the needs. But there are a lot of operation Word use method is not very clear, most of the online use of VB, there is no clear place, we can communicate.

Here is my design to achieve the idea:

First of all, define the Word template and label it where you want to replace it. Menu-Insert-Bookmark, enter property name, such as Year,date,pic1,volist, etc.
Print page:
You need to remove the printed data from the background, take a single VO (an object) as a group, or replace it with volist (the list of objects) for a group of organized pages.
The data is organized as follows:
<div id= "Export2word" >
<form id= "Singlevo" name= "Singlevo" >
<textarea name= "Jcxcrs" style= "Display:none" ><c:out value= "${zywstjfxbgvo.jcxcrs}"/></textarea>
<textarea name= "XCJHL" style= "Display:none" ><c:out value= "${ZYWSTJFXBGVO.XCJHL}"/></textarea>
<textarea name= "Tbjcxcrs" style= "Display:none" ><c:out value= "${tbjcxcrs}"/></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: This width is set to 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= "Tbjcxcrs" style= "Width:477;display:none" ><c:out value= "${mxvo.tbjcxcrs}"/></ Textarea>
<textarea name= "TBJCXCRSBL" style= "Display:none" ><c:out value= "${MXVO.TBJCXCRSBL}"/></textarea>
</form>
</c:forEach>
</div>

Use:
<input type= "button" id= "Select2" name= "select2" button "class=" Export Data "value=" onclick= (); " >

<script type= "Text/javascript" src= ". /public/scripts/export2word.js "></script>
<script type= "Text/javascript" >
function Print2doc () {
parameter is the template (relative to the page) path
var word = new WordApp ("Test.doc");
parameter is the form name, the attribute to be added in VO (for all attributes in the null form)
var vo = Word.getsinglevo ("Singlevo", ["Jcxcrs", "XCJHL", "Tbjcxcrs"]);
var vo = Word.getsinglevo ("Singlevo");
Organized picture VO
var tpvo = Word.getsinglevo ("Singlevo", ["tptest"]);
The parameter is a form name, and the attributes that need to be added are the order in which the table columns are generated, all the attributes and the order in the form when they are empty.
var volist = word.getvolist ("Mxvoform", ["Tbjcmcrs", "TBJCMCRSBL", "Tbjczsrs"]);
var volist = word.getvolist ("Mxvoform");
Replace a normal bookmark
Word.replacebookmarkusevo (VO);
Replace a picture bookmark
WORD.REPLACEBOOKMARKUSEPICVO (TPVO);
Replace the bookmark jglist and draw a table to form multiple rows of data.
Word.replacebookmarkusevolist ("Jglist", volist);
Document visible
Word.wordobj.visible=true;
Word.closeapp ();
}
</script>
Attention:
The value of the replacement picture needs to be explained:
1. You can set the path relative to this page as ... /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 picture format. If the/.../abc.do?id=123 is replaced by/. /abc.png?id=123
You can use a servlet in the web.xml, such as a *.png request to convert to. do. Such as:

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); Directed pages
Rd.forward (request, response);
}
}

Current 1/2 page 12 Next read the full text
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.