Recently, there is a need to export the data to word and edit and print it.
Have thought of several solutions:
1. Use jacob.
2. Use apache poi.
3. Use itext.
Because time is tight, I haven't had much time to learn and study the above toolkit. Now I use javascript to operate the ActiveXObject control and solve the problem by replacing the bookmarks in the word template.
Prerequisites:
1. The browser security level is reduced, and you can use the ActiveXObject control.
2. Install office word.
Currently, it has implemented the replacement of single bookmarks, multi-row form bookmarks, and pictures, which basically meet the needs. However, there are still many ways to use word operation. Most of the VBs are used on the Internet. If there are some unclear aspects, you can communicate.
Let me talk about my design implementation ideas below:
The first thing is to define the word template and add labels where it needs to be replaced. Menu - Insert - Bookmark, enter attribute names, such as year, date, pic1, voList, etc.
Print the page:
You need to take the printed data out of the background, use a single vo (one object) as a group, or use a voList (the list of objects) as a group, organize the page and then get the data and replace it.
The data organization form is as follows:
<div >
<form name="singleVo">
<textarea name="jcxcrs" style="display:none"><c:out value="${ }"/></textarea>
<textarea name="xcjhl" style="display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcxcrs" style="display:none"><c:out value="${tbjcxcrs }"/></textarea>
<textarea name="tptest" style="display:none">../zwgl/?xh=3041</textarea>
</form>
<c:forEach var="mxvo" items="${jgList}" varStatus="s">
<form name="mxvoForm">
<!-- Note: The width here is set to table cell width (cm*100)-->
<textarea name="tbjcmcrs" style="width:349;display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcmcrsbl" style="width:270;display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcxcrs" style="width:477;display:none"><c:out value="${}"/></textarea>
<textarea name="tbjcxcrsbl" style="display:none"><c:out value="${ }"/></textarea>
</form>
</c:forEach>
</div>
use:
<input type="button" name="select2" class="button" value="Export data" onclick="print2doc();">
<script type="text/javascript" src="../public/scripts/"></script>
<script type="text/javascript">
function print2doc(){
//The parameter is the path of the template (relative to the page)
var word = new WordApp("");
//The parameter is the form name, the attributes that need to be added in vo (all attributes in the form when empty)
var vo = ("singleVo",["jcxcrs","xcjhl","tbjcxcrs"]);
//var vo = ("singleVo");
//Pictures organized vo
var tpvo = ("singleVo",["tptest"]);
//The parameter is the form name, the attributes that need to be added (the order is the order in which the table columns are generated, and all attributes and order in the form when empty)
var voList = ("mxvoForm",["tbjcmcrs","tbjcmcrsbl","tbjczsrs"]);
//var voList = ("mxvoForm");
//Replace the normal bookmark
(vo);
//Replace picture bookmark
(tpvo);
//Replace the bookmark jgList and draw the table to form multiple rows of data.
("jgList",voList);
//The document is visible
=true;
//();
}
</script>
Notice:
The value of the replacement image needs to be explained:
1. It can be set to the path relative to this page such as.../zbgl/
2. If it is an output stream, the url of the requested output stream needs to be mapped to end in image format. If /.../?id=123 is replaced by /../?id=123
You can configure a servlet in it, such as converting it to .do with the request of *.png. like:
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 = ();
StringBuffer url = new StringBuffer(());
if(() != null) {
('?');
(());
}
String newUrl = ().replaceAll(".png", ".do");
ServletContext sc = getServletContext();
RequestDispatcher rd = (newUrl); //Targeted page
(request, response);
}
}
Have thought of several solutions:
1. Use jacob.
2. Use apache poi.
3. Use itext.
Because time is tight, I haven't had much time to learn and study the above toolkit. Now I use javascript to operate the ActiveXObject control and solve the problem by replacing the bookmarks in the word template.
Prerequisites:
1. The browser security level is reduced, and you can use the ActiveXObject control.
2. Install office word.
Currently, it has implemented the replacement of single bookmarks, multi-row form bookmarks, and pictures, which basically meet the needs. However, there are still many ways to use word operation. Most of the VBs are used on the Internet. If there are some unclear aspects, you can communicate.
Let me talk about my design implementation ideas below:
The first thing is to define the word template and add labels where it needs to be replaced. Menu - Insert - Bookmark, enter attribute names, such as year, date, pic1, voList, etc.
Print the page:
You need to take the printed data out of the background, use a single vo (one object) as a group, or use a voList (the list of objects) as a group, organize the page and then get the data and replace it.
The data organization form is as follows:
<div >
<form name="singleVo">
<textarea name="jcxcrs" style="display:none"><c:out value="${ }"/></textarea>
<textarea name="xcjhl" style="display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcxcrs" style="display:none"><c:out value="${tbjcxcrs }"/></textarea>
<textarea name="tptest" style="display:none">../zwgl/?xh=3041</textarea>
</form>
<c:forEach var="mxvo" items="${jgList}" varStatus="s">
<form name="mxvoForm">
<!-- Note: The width here is set to table cell width (cm*100)-->
<textarea name="tbjcmcrs" style="width:349;display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcmcrsbl" style="width:270;display:none"><c:out value="${ }"/></textarea>
<textarea name="tbjcxcrs" style="width:477;display:none"><c:out value="${}"/></textarea>
<textarea name="tbjcxcrsbl" style="display:none"><c:out value="${ }"/></textarea>
</form>
</c:forEach>
</div>
use:
<input type="button" name="select2" class="button" value="Export data" onclick="print2doc();">
<script type="text/javascript" src="../public/scripts/"></script>
<script type="text/javascript">
function print2doc(){
//The parameter is the path of the template (relative to the page)
var word = new WordApp("");
//The parameter is the form name, the attributes that need to be added in vo (all attributes in the form when empty)
var vo = ("singleVo",["jcxcrs","xcjhl","tbjcxcrs"]);
//var vo = ("singleVo");
//Pictures organized vo
var tpvo = ("singleVo",["tptest"]);
//The parameter is the form name, the attributes that need to be added (the order is the order in which the table columns are generated, and all attributes and order in the form when empty)
var voList = ("mxvoForm",["tbjcmcrs","tbjcmcrsbl","tbjczsrs"]);
//var voList = ("mxvoForm");
//Replace the normal bookmark
(vo);
//Replace picture bookmark
(tpvo);
//Replace the bookmark jgList and draw the table to form multiple rows of data.
("jgList",voList);
//The document is visible
=true;
//();
}
</script>
Notice:
The value of the replacement image needs to be explained:
1. It can be set to the path relative to this page such as.../zbgl/
2. If it is an output stream, the url of the requested output stream needs to be mapped to end in image format. If /.../?id=123 is replaced by /../?id=123
You can configure a servlet in it, such as converting it to .do with the request of *.png. like:
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 = ();
StringBuffer url = new StringBuffer(());
if(() != null) {
('?');
(());
}
String newUrl = ().replaceAll(".png", ".do");
ServletContext sc = getServletContext();
RequestDispatcher rd = (newUrl); //Targeted page
(request, response);
}
}