比较巧的是,刚做完一个下载Excel文件的功能,又有一个新的问题,就是需要将一份文件导出到word中,刚开始我找到的方法,无非是将网页中内容用wrod打开,但是网页的版式也复制了进去,显的很不正规。相信大家都遇到了这个问题。还有一种方法是用控件操作com来达到控制word版式的方法,但是太复杂,这个项目不允许。后来找到了一种折中的办法,即可以控制版式,又不会太复杂。
如果您有更好的办法,可以联系我。
MSN:whw_dream(AT)hotmail.com
首先,新建一个word文档:test.doc
建立以下内容:
而且最好在你要生成的内容中加个标记,以便操作(我就用xxxx和12345标注)。
然后另存为web:
然后用文本编辑器打开test.htm文件(我的是editplus)
建立工程什么的就不说,直接创建test.jsp
<%taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:button property="button" onclick="dowloadword()">
downloadword
</html:button>
</html:html>
<script language=“javascript“>
function dowloadword(){ location.href="/downloadword.do"; }
</script>
建立DownloadWordAction.java
package test.whw.upload;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
/**
* <p>Title:DownloadWordAction </p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/
public class DownloadWordAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
DynaActionForm testForm = (DynaActionForm)form;
testForm.set("name","测试的word文件");
ArrayList list = new ArrayList();
HashMap row = new HashMap();
row.put("a","aaaaa");
row.put("b","aaaaa");
row.put("c","aaaaa");
row.put("d","aaaaa");
row.put("e","aaaaa");
list.add(row);
row.put("a","bbbbb");
row.put("b","bbbbb");
row.put("c","bbbbb");
row.put("d","bbbbb");
row.put("e","bbbbb");
list.add(row);
request.setAttribute("list",list);
return mapping.findForward("display");
}
}
然后建立downloadword.jsp
把用文本编辑器打开的test.htm的内容拷贝到downloadword.jsp中。
为了查看显示结果,本例子会直接打开downloadword.jsp,需要下载的话,在文件头加入以下代码就可以:
<%@ page contentType="application/msword;charset=gb2312" %>
<%
response.setHeader("Content-disposition","attachment;filename=文件名.doc");
%>
就可以直接下载了。
然后在downloadword.jsp中word生成的html中找到相应的代码进行修改就可以了。
downloadword.jsp代码如下:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page contentType="text/html; charset=GB2312" %>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 10">
<meta name=Originator content="Microsoft Word 10">
<link rel=File-List href="test.files/filelist.xml">
<style>
</style>
</head>
<body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>
<div class=Section1 style='layout-grid:15.6pt'>
<p class=MsoNormal><span lang=EN-US><span style='mso-tab-count:7'> </span><span
style='mso-spacerun:yes'> </span><bean:write name="DownWordForm" property="name" /></span></p>
<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>
<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0
style='border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
mso-yfti-tbllook:480;mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-border-insideh:
.5pt solid windowtext;mso-border-insidev:.5pt solid windowtext'>
<logic:notEmpty name="list" >
<logic:iterate id="row" name="list">
<tr style='mso-yfti-irow:0;mso-yfti-lastrow:yes'>
<td width=114 valign=top style='width:85.2pt;border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
<p class=MsoNormal><span lang=EN-US><bean:write name="row" property="a" /></span></p>
</td>
<td width=114 valign=top style='width:85.2pt;border:solid windowtext 1.0pt;
border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
<p class=MsoNormal><span lang=EN-US><bean:write name="row" property="b" /></span></p>
</td>
<td width=114 valign=top style='width:85.2pt;border:solid windowtext 1.0pt;
border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
<p class=MsoNormal><span lang=EN-US><bean:write name="row" property="c" /></span></p>
</td>
<td width=114 valign=top style='width:85.25pt;border:solid windowtext 1.0pt;
border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
<p class=MsoNormal><span lang=EN-US><bean:write name="row" property="d" /></span></p>
</td>
<td width=114 valign=top style='width:85.25pt;border:solid windowtext 1.0pt;
border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
<p class=MsoNormal><span lang=EN-US><bean:write name="row" property="e" /></span></p>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
<p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p>
</div>
</body>
</html>
最后是struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="DownWordForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="name" type="java.lang.String" />
</form-bean>
</form-beans>
<action-mappings>
<action name="DownWordForm" type="test.whw.upload.DownloadWordAction" validate="false" scope="request" path="/downloadword">
<forward name="display" path="/downloadword.jsp" />
</action>
</action-mappings>
</struts-config>
大概内容就是这样,显示效果如下: