jsp实现针对excel及word文档的打印方法
本文实例讲述了jsp实现针对excel及word文档的打印方法。分享给大家供大家参考,具体如下:
因为msword和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为Web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式
word页面只要在jsp头设置如下指令:
<%@pagecontentType="application/msword;charset=GBK"%>
excel如下:
<%@pagecontentType="application/vnd.ms-excel;charset=GBK"%>
使用这种方式客户端必须安装有office软件,用户访问时将在ie中直接用word或excel打开该页面。
此方法优势是模板设计、调整方便,无需在服务器端使用复杂的POI或jxl技术,也无需在客户端使用ActiveX控件技术,更安全、方便,轻松实现较好的打印效果。
microsoft关于服务器端动态创建office文档的资料(asp示例):
http://support.microsoft.com/default.aspx?scid=KB;en-us;301044&
简单示例:
使用word建立一文档,画表格如下:
----------------------------
|用户名|真实姓名|性别|
----------------------------
|guest |路人甲 |男 |
----------------------------
保存为Web页test.htm,将test.htm改名为test.jsp,修改其中guest、路人甲、男为从数据库动态查询,如下:
<%@pagecontentType="application/msword;charset=GBK"%>
<%@pageimport="java.sql.*"%>
<htmlxmlns:o="urn:schemas-microsoft-com:office:office"xmlns:w="urn:schemas-microsoft-com:office:word"xmlns="http://www.w3.org/TR/REC-html40">
<head>
<metahttp-equiv=Content-Typecontent="text/html;charset=GB2312">
<metaname=ProgIdcontent=Word.Document>
<metaname=Generatorcontent="MicrosoftWord9">
<metaname=Originatorcontent="MicrosoftWord9">
<title>用户信息</title>
<!--[ifgtemso9]><xml>
<o:DocumentProperties>
<o:Author>evanzhao</o:Author>
<o:LastAuthor>evanzhao</o:LastAuthor>
<o:Revision>1</o:Revision>
<o:TotalTime>1</o:TotalTime>
<o:Created>2003-08-20T16:26:00Z</o:Created>
<o:LastSaved>2003-08-20T16:27:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Company>taiping</o:Company>
<o:Lines>1</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:Version>9.2812</o:Version>
</o:DocumentProperties>
</xml><![endif]--><!--[ifgtemso9]><xml>
<w:WordDocument>
<w:PunctuationKerning>
<w:DrawingGridVerticalSpacing>7.8磅</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:Compatibility>
<w:SpaceForUL>
<w:BalanceSingleByteDoubleByteWidth>
<w:DoNotLeaveBackslashAlone>
<w:ULTrailSpace>
<w:DoNotExpandShiftReturn>
<w:AdjustLineHeightInTable>
<w:UseFELayout>
</w:Compatibility>
</w:WordDocument>
</xml><![endif]-->
<style>
<!--
/*FontDefinitions*/
@font-face
{font-family:宋体;
panose-1:2160311111;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:31351352321602621450;}
@font-face
{font-family:"@宋体";
panose-1:2160311111;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:31351352321602621450;}
/*StyleDefinitions*/
p.MsoNormal,li.MsoNormal,div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"TimesNewRoman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/*PageDefinitions*/
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@pageSection1
{size:595.3pt841.9pt;
margin:72.0pt90.0pt72.0pt90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<bodylang=ZH-CNstyle='tab-interval:21.0pt;text-justify-trim:punctuation'>
<divclass=Section1style='layout-grid:15.6pt'>
<tableborder=1cellspacing=0cellpadding=0style='border-collapse:collapse;border:none;mso-border-alt:solidwindowtext.5pt;mso-padding-alt:0cm5.4pt0cm5.4pt'>
<tr>
<tdwidth=189valign=topstyle='width:142.0pt;border:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanstyle='font-family:宋体;mso-ascii-font-family:"TimesNewRoman";mso-hansi-font-family:"TimesNewRoman"'>用户名</span></p>
</td>
<tdwidth=189valign=topstyle='width:142.05pt;border:solidwindowtext.5pt;border-left:none;mso-border-left-alt:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanstyle='font-family:宋体;mso-ascii-font-family:"TimesNewRoman";mso-hansi-font-family:"TimesNewRoman"'>真实姓名</span></p>
</td>
<tdwidth=189valign=topstyle='width:142.05pt;border:solidwindowtext.5pt;border-left:none;mso-border-left-alt:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanstyle='font-family:宋体;mso-ascii-font-family:"TimesNewRoman";mso-hansi-font-family:"TimesNewRoman"'>性别</span></p>
</td>
</tr>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Stringurl="jdbc:odbc:mydb";
//连接mydb数据库
Connectioncon=DriverManager.getConnection(url,"","");
try{
Statementstmt=con.createStatement();
//查询employee表
ResultSetrs=stmt.executeQuery("selectuser_name,real_name,genderfromemployee");
while(rs.next()){
%>
<tr>
<tdwidth=189valign=topstyle='width:142.0pt;border:solidwindowtext.5pt;border-top:none;mso-border-top-alt:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanlang=EN-US><%=rs.getString("user_name")%></span></p>
</td>
<tdwidth=189valign=topstyle='width:142.05pt;border-top:none;border-left:none;border-bottom:solidwindowtext.5pt;border-right:solidwindowtext.5pt;mso-border-top-alt:solidwindowtext.5pt;mso-border-left-alt:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanstyle='font-family:宋体;mso-ascii-font-family:"TimesNewRoman";mso-hansi-font-family:"TimesNewRoman"'><%=rs.getString("real_name")%></span></p>
</td>
<tdwidth=189valign=topstyle='width:142.05pt;border-top:none;border-left:none;border-bottom:solidwindowtext.5pt;border-right:solidwindowtext.5pt;mso-border-top-alt:solidwindowtext.5pt;mso-border-left-alt:solidwindowtext.5pt;padding:0cm5.4pt0cm5.4pt'>
<pclass=MsoNormal><spanstyle='font-family:宋体;mso-ascii-font-family:"TimesNewRoman";mso-hansi-font-family:"TimesNewRoman"'><%=rs.getString("gender")%></span></p>
</td>
</tr>
<%
}//endwhile
rs.close();
stmt.close();
}finally{
con.close();
}
%>
</table>
<pclass=MsoNormal><spanlang=EN-US><![if!supportEmptyParas]><![endif]><o:p></o:p></span></p>
</div>
</body>
</html>
希望本文所述对大家JSP程序设计有所帮助。