Home >
Article > Web Front-end > Use ActiveXObject control to replace word bookmarks under javascript, export the content to word and print it_javascript skills
Use ActiveXObject control to replace word bookmarks under javascript, export the content to word and print it_javascript skills
PHP中文网Original
2016-05-16 19:03:391669browse
Due to tight time, I don’t have much time to study the above toolkit. Now I use javascript to operate the ActiveXObject control and replace the bookmark in the word template.
Recently, there is a need to export data to word, and then edit and print it. I have thought of several options: 1. Use jacob. 2. Use apache’s poi. 3. Use itext. Due to tight time, I don’t have much time to study the above toolkit. Now I use javascript to operate the ActiveXObject control and replace the bookmark in the word template.
Prerequisites: 1. The browser security level is reduced and ActiveXObject control can be used.
2. Office word is installed.
Currently, it is possible to replace single bookmarks, multi-line table bookmarks, and pictures, which basically meets the needs. However, there are still many ways to use Word that are not clear. Most people on the Internet use VB. If there is anything unclear, everyone can share it.
Let me talk about my design implementation ideas below:
First of all, of course, define the word template and add tags where needed to be replaced. Menu-Insert-Bookmark, enter the attribute name, such as year, date, pic1, voList, etc. Print page: You need to take out the printed data from the background, organize it into a group with a single vo (an object), or use a voList (a list collection of objects) to organize the page and then get the data. Make a substitution. The data is organized as follows:
< form name="mxvoForm">
Use:
Note: The value of replacing the image needs explanation: 1. It can be set to a path relative to this page, such as../zbgl/abc.png 2. If it is Output stream, you need to map the URL requesting the output stream to end in image format. For example, /.../abc.do?id=123 is replaced by /../abc.png?id=123 You can configure a servlet in web.xml, such as converting *.png request to .do of. Such as:
public class PngDispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 6230740581031996144L;
Title: Use word bookmark replacement to export content to word
*
Description: **
*
Copyright: Copyright (c) 2007-2010
*
Company: **
* @author zhu * @version 1.0 */ var baseVoListObj = function (){ this.volist = new Array(); this.cols = new Array(); this.widths = new Array(); }
var WordApp = function(wordTplPath){ var wordObj = new ActiveXObject("Word.Application"); if(wordObj==null){ alert( "Cannot create Word object! "); } wordObj.visible=false; this.wordObj = wordObj; this.docObj = this.wordObj.Documents.Open(getRootPath() wordTplPath); }
WordApp.prototype.closeApp = function(){ if (this.wordObj !=null){ this.wordObj.Quit(); } }
WordApp.prototype.replaceBookmark = function(strName,content,type){ if (this.wordObj.ActiveDocument.BookMarks.Exists(strName)) { if (type != null && type = = "pic") {//Picture var objDoc = this.wordObj.ActiveDocument.BookMarks(strName).Range.Select(); var objSelection = this.wordObj.Selection; objSelection.TypeParagraph(); //alert(getRootPath() content); var objShape = objSelection.InlineShapes.AddPicture(getRootPath() content); } else { this .wordObj.ActiveDocument.BookMarks(strName).Range.Select(); this.wordObj.Application.selection.Text = content; } }else{ //alert("tag Does not exist"); } }
WordApp.prototype.replaceBookmarkUsevo = function(voObj){ if(typeof voObj != "object"){ alert(" Please enter the correct vo object"); }else{ for(var i in voObj){ this.replaceBookmark(i,voObj[i]); } } }
WordApp.prototype.replaceBookmarkUsepicvo = function(voObj){ if(typeof voObj !="object"){ alert("Please enter the correct vo object"); }else{ for(var i in voObj){ this.replaceBookmark(i,voObj[i],"pic"); } } }
WordApp.prototype.replaceBookmarkUsevolist = function(strName,voListObj){ if(typeof voListObj != "object"){ alert("The parameter should be an array type"); }else{ var row = voListObj.volist.length; var col = voListObj.cols.length; var objDoc = this.wordObj.ActiveDocument.BookMarks(strName).Range; var objTable = this .docObj.Tables.Add(objDoc,row,col);//Insert table for (var i = 0; i < row; i ) { for(var j=0; j
//If there is an image type in the todo list that is not supported, you need to determine objTable.Cell(i 1,j 1).Range.InsertAfter(voListObj.volist[i][voListObj.cols[j ]]); var width = voListObj.widths[j]; if(width.indexOf("px")!=-1){ objTable.Cell(i 1,j 1). Width = (width.substr(0,width.length-2)/100) * 28.35; //1 cm = 28.35 pounds } } } //objTable.AutoFormat(16 ; //The second parameter can be empty. If not filled in, it will default to all elements in the form var formObj = document.forms[formName]; if(formObj!=null){ if(arrayObj !=null){ if(arrayObj instanceof Array){ var vo = {}; for(var i=0;i if(formObj. elements[arrayObj[i]]!= undefined ){ eval("vo." arrayObj[i] " = formObj.elements[arrayObj[i]].value;"); } } //alert(objToString(vo)); return vo; }else{ alert("The two parameters should be array types"); } } else{ var vo = {}; for(var i=0;i eval("vo." formObj.elements[i].name " = formObj.elements[i].value;"); } return vo; } }else{ alert("The form represented by the first parameter does not exist") ; return null; } }
WordApp.prototype.getVoList = function (formName, arrayObj){//Form name, attribute array (can be empty) / /var formArray = document.forms[formName]; var formArray = document.getElementsByName(formName); if (formArray != null) { if (arrayObj instanceof Array) { var voListObj = new baseVoListObj(); for(var i=0;i var vo = {}; for(var j=0;j if(formArray[i].elements[arrayObj[j]]!= undefined ){ eval("vo." arrayObj[j] " = formArray[i].elements[arrayObj[j]].value;"); if(i==0){//第一次的时候定义有效属性和宽度 voListObj.cols.push(arrayObj[j]); voListObj.widths.push(formArray[i].elements[arrayObj[j]].style.width); } } } voListObj.volist.push(vo); } return voListObj; }else{ var voListObj = new baseVoListObj(); for(var i=0;i var vo = {}; for(var j=0;j eval("vo." formArray[i].elements[j].name " = formArray[i].elements[j].value;"); if(i==0){//第一次的时候定义宽度 voListObj.cols.push(formArray[i].elements[j].name); voListObj.widths.push(formArray[i].elements[j].style.width); } } voListObj.volist.push(vo); } return voListObj; } }else{ return null; } }
function objToString(obj){ if(obj instanceof Array){ var str=""; for(var i=0;i str ="["; for(var j in obj[i]){ str =j "=" obj[i][j] " "; } str ="]n"; } return str; }else if(obj instanceof Object){ var str=""; for(var i in obj){ str =i "=" obj[i] " "; } return str; } }
function getRootPath() { var location=document.location; if ("file:" == location.protocol) { var str = location.toString(); return str.replace(str.split("/").reverse()[0], ""); } var pathName=location.pathname.split("/"); return location.protocol "//" location.host "/" pathName[1] "/"; } 先说到这里吧,以后有更好的再更新,希望对大家有用。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn