欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp实现针对excel及word文档的打印方法

程序员文章站 2023-08-24 16:40:15
本文实例讲述了jsp实现针对excel及word文档的打印方法。分享给大家供大家参考,具体如下: 因为ms word和excel的文档都支持html文本格式,因此可以先用...

本文实例讲述了jsp实现针对excel及word文档的打印方法。分享给大家供大家参考,具体如下:

因为ms word和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式

word页面只要在jsp头设置如下指令:

复制代码 代码如下:
<%@page contenttype="application/msword;charset=gbk" %>

excel如下:
复制代码 代码如下:
<%@page contenttype="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、路人甲、男为从数据库动态查询,如下:

<%@ page contenttype="application/msword;charset=gbk" %>
<%@ page import="java.sql.*" %>
<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 9">
<meta name=originator content="microsoft word 9">
<title>用户信息</title>
<!--[if gte mso 9]><xml>
 <o:documentproperties>
 <o:author>evan zhao</o:author>
 <o:lastauthor>evan zhao</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]--><!--[if gte mso 9]><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>
<!--
 /* font definitions */
@font-face
  {font-family:宋体;
  panose-1:2 1 6 0 3 1 1 1 1 1;
  mso-font-alt:simsun;
  mso-font-charset:134;
  mso-generic-font-family:auto;
  mso-font-pitch:variable;
  mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
  {font-family:"@宋体";
  panose-1:2 1 6 0 3 1 1 1 1 1;
  mso-font-charset:134;
  mso-generic-font-family:auto;
  mso-font-pitch:variable;
  mso-font-signature:3 135135232 16 0 262145 0;}
 /* style definitions */
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:"times new roman";
  mso-fareast-font-family:宋体;
  mso-font-kerning:1.0pt;}
 /* page definitions */
@page
  {mso-page-border-surround-header:no;
  mso-page-border-surround-footer:no;}
@page section1
  {size:595.3pt 841.9pt;
  margin:72.0pt 90.0pt 72.0pt 90.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>
<body lang=zh-cn style='tab-interval:21.0pt;text-justify-trim:punctuation'>
<div class=section1 style='layout-grid:15.6pt'>
<table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse; border:none;mso-border-alt:solid windowtext .5pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>
 <tr>
 <td width=189 valign=top style='width:142.0pt;border:solid windowtext .5pt; padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span style='font-family:宋体;mso-ascii-font-family:"times new roman"; mso-hansi-font-family:"times new roman"'>用户名</span></p>
 </td>
 <td width=189 valign=top style='width:142.05pt;border:solid windowtext .5pt;border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span style='font-family:宋体;mso-ascii-font-family:"times new roman"; mso-hansi-font-family:"times new roman"'>真实姓名</span></p>
 </td>
 <td width=189 valign=top style='width:142.05pt;border:solid windowtext .5pt; border-left:none;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span style='font-family:宋体;mso-ascii-font-family:"times new roman"; mso-hansi-font-family:"times new roman"'>性别</span></p>
 </td>
 </tr>
<%
class.forname("sun.jdbc.odbc.jdbcodbcdriver");
string url="jdbc:odbc:mydb";
//连接mydb数据库
connection con=drivermanager.getconnection (url, "", "");
try{
 statement stmt=con.createstatement();
 //查询employee表
 resultset rs=stmt.executequery("select user_name, real_name, gender from employee ");
 while(rs.next()){
%>
 <tr>
 <td width=189 valign=top style='width:142.0pt;border:solid windowtext .5pt;border-top:none;mso-border-top-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span lang=en-us><%=rs.getstring("user_name")%></span></p>
 </td>
 <td width=189 valign=top style='width:142.05pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span style='font-family:宋体;mso-ascii-font-family:"times new roman";mso-hansi-font-family:"times new roman"'><%=rs.getstring("real_name")%></span></p>
 </td>
 <td width=189 valign=top style='width:142.05pt;border-top:none;border-left:none;border-bottom:solid windowtext .5pt;border-right:solid windowtext .5pt;mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;padding:0cm 5.4pt 0cm 5.4pt'>
 <p class=msonormal><span style='font-family:宋体;mso-ascii-font-family:"times new roman";mso-hansi-font-family:"times new roman"'><%=rs.getstring("gender")%></span></p>
 </td>
 </tr>
<%
 } // end while
 rs.close();
 stmt.close();
} finally {
 con.close();
}
%>
</table>
<p class=msonormal><span lang=en-us><![if !supportemptyparas]> <![endif]><o:p></o:p></span></p>
</div>
</body>
</html>

希望本文所述对大家jsp程序设计有所帮助。