分享
 
 
 

使用jsp实现word、excel格式报表打印

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

因为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

ocumentProperties>

<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

ages>1</o
ages>

<o:Company>taiping</o:Company>

<o:Lines>1</o:Lines>

<o

aragraphs>1</o
aragraphs>

<o:Version>9.2812</o:Version>

</o

ocumentProperties>

</xml><![endif]--><!--[if gte mso 9]><xml>

<w:WordDocument>

<w

unctuationKerning/>

<w

rawingGridVerticalSpacing>7.8 磅</w
rawingGridVerticalSpacing>

<w

isplayHorizontalDrawingGridEvery>0</w
isplayHorizontalDrawingGridEvery>

<w

isplayVerticalDrawingGridEvery>2</w
isplayVerticalDrawingGridEvery>

<w:Compatibility>

<w:SpaceForUL/>

<w:BalanceSingleByteDoubleByteWidth/>

<w

oNotLeaveBackslashAlone/>

<w:ULTrailSpace/>

<w

oNotExpandShiftReturn/>

<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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有