jsp导出为word文档的几个技巧

写道
1、步骤:
第一步:编辑好word模版,然后另存为*.htm,比如:liukun.htm。
技巧:在需要填写数据的地方最好预填入一些易识别的数据,这样方便后面填写jsp代码。
第二步:把htm后缀改为jsp,比如:liukun.jsp。
第三步:添加jsp的头,比如:
<%@page contentType="application/msword;charset=GBK"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
如果有import,也要在这里导入。
技巧:application/msword;这个参数很重要,有了这个参数,调用这个页面时,就会把页面内容存为word。当然,本地必须安装office。
第三步:添加jsp标签。(我用的是struts框架,所以添加的是struts的标签,也可以添加<%%>)
......
<logic:present name="liukun" scope="request">
......
<bean:write name="liukun" property="xm" scope="request" />
......
</logic:present>
......
当然,也可以加循环,这样生成多页(份):
......
<logic:notEmpty name="bmklist" scope="request">
<logic:iterate id="examManage" name="bmklist" type="ujn.ems.liukun.Info" scope="request">
......
<bean:write name="examManage" property="xm" scope="page" />
......
</logic:iterate>
</logic:notEmpty>
......
第四步:写action,把查询数据库后的结果通过request给页面传参数。
......
public ActionForward saveZY2word(ActionMapping mapping, ActionForm. form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
ArrayList<Info> bmklist = new ArrayList<Info>();
String bkzydm = request.getParameter("bkzydm");
bmklist = Info.listAllInfobyZY(bkzydm);
if(bmklist!=null){
request.setAttribute("bmklist",bmklist);
return mapping.findForward("saveAll2word");
}
return null;
}
......
2、其他技巧(别人我不告诉他啊,呵呵):
A、横向页面导出:
<style>
<!--
......
/* Page Definitions */
@page Section1
{size:841.9pt 595.3pt;
mso-page-orientation:landscape;
margin:89.85pt 72.0pt 89.85pt 3.0cm;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style>
B、分页:
......
<span lang=EN-US style='font-size:10.5pt;font-family:黑体;mso-hansi-font-family:
"Times New Roman";mso-bidi-font-family:"Times New Roman";mso-ansi-language:
EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all
style='mso-special-character:line-break;page-break-before:always'>
</span>
......
C、缩小字体填充:
......
<td width=84 nowrap valign=top style='width:63.0pt;border-top:none;
border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
padding:0cm 5.4pt 0cm 5.4pt;text-fit:100%;height:19.65pt'>
<p class=MsoNormal align=center style='text-align:center'>
<span style='font-family:宋体;color:black;letter-spacing:.75pt'>
<bean:write name="examManage" property="xm" scope="page" />
</span>
</p>
</td>
......

 

你可能感兴趣的:(jsp,bean,Web,struts,Office)