使用JSP实现WORD、EXCEL格式报表打印
来源:互联网 发布:网络管理的五大功能 编辑:程序博客网 时间:2024/04/18 13:03
title: 使用JSP实现WORD、EXCEL格式报表打印
author: evan
date:
因为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、路人甲、男为从数据库动态查询,如下:
1.
2. <%@ page contentType="application/msword;charset=GBK" %>
3. <%@ page import="java.sql.*" %>
4. <html xmlns:o="urn:schemas-microsoft-com:office:office"
5. xmlns:w="urn:schemas-microsoft-com:office:word"
6. xmlns="http://www.w3.org/TR/REC-html40">
7.
8. <head>
9. <meta http-equiv=Content-Type content="text/html; charset=GB2312">
10. <meta name=ProgId content=Word.Document>
11. <meta name=Generator content="Microsoft Word 9">
12. <meta name=Originator content="Microsoft Word 9">
13. <title>用户信息</title>
14. <!--[if gte mso 9]><xml>
15. <o:DocumentProperties>
16. <o:Author>evan zhao</o:Author>
17. <o:LastAuthor>evan zhao</o:LastAuthor>
18. <o:Revision>1</o:Revision>
19. <o:TotalTime>1</o:TotalTime>
20. <o:Created>
21. <o:LastSaved>
22. <o:Pages>1</o:Pages>
23. <o:Company>taiping</o:Company>
24. <o:Lines>1</o:Lines>
25. <o:Paragraphs>1</o:Paragraphs>
26. <o:Version>9.2812</o:Version>
27. </o:DocumentProperties>
28. </xml><![endif]--><!--[if gte mso 9]><xml>
29. <w:WordDocument>
30. <w:PunctuationKerning>
31. <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>
32. <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
33. <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
34. <w:Compatibility>
35. <w:SpaceForUL>
36. <w:BalanceSingleByteDoubleByteWidth>
37. <w:DoNotLeaveBackslashAlone>
38. <w:ULTrailSpace>
39. <w:DoNotExpandShiftReturn>
40. <w:AdjustLineHeightInTable>
41. <w:UseFELayout>
42. </w:Compatibility>
43. </w:WordDocument>
44. </xml><![endif]-->
45. <style>
46. <!--
47. /* Font Definitions */
48. @font-face
49. {font-family:宋体;
50. panose-1:2 1 6 0 3 1 1 1 1 1;
51. mso-font-alt:SimSun;
52. mso-font-charset:134;
53. mso-generic-font-family:auto;
54. mso-font-pitch:variable;
55. mso-font-signature:3 135135232 16 0 262145 0;}
56. @font-face
57. {font-family:"/@宋体";
58. panose-1:2 1 6 0 3 1 1 1 1 1;
59. mso-font-charset:134;
60. mso-generic-font-family:auto;
61. mso-font-pitch:variable;
62. mso-font-signature:3 135135232 16 0 262145 0;}
63. /* Style Definitions */
64. p.MsoNormal, li.MsoNormal, div.MsoNormal
65. {mso-style-parent:"";
66. margin:
67. margin-bottom:
68. text-align:justify;
69. text-justify:inter-ideograph;
70. mso-pagination:none;
71. font-size:
72. mso-bidi-font-size:
73. font-family:"Times New Roman";
74. mso-fareast-font-family:宋体;
75. mso-font-kerning:
76. /* Page Definitions */
77. @page
78. {mso-page-border-surround-header:no;
79. mso-page-border-surround-footer:no;}
80. @page Section1
81. {size:
82. margin:
83. mso-header-margin:
84. mso-footer-margin:
85. mso-paper-source:0;
86. layout-grid:
87. div.Section1
88. {page:Section1;}
89. -->
90. </style>
91. </head>
92.
93. <body lang=ZH-CN style='tab-interval:
94.
95. <div class=Section1 style='layout-grid:
96.
97.
98. <table border=1 cellspacing=0 cellpadding=0 style='border-collapse:collapse;
99. border:none;mso-border-alt:solid windowtext
100. <tr>
101. <td width=189 valign=top style='width:
102. padding:
103. <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
104. mso-hansi-font-family:"Times New Roman"'>用户名</span></p>
105. </td>
106. <td width=189 valign=top style='width:
107. border-left:none;mso-border-left-alt:solid windowtext
108. <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
109. mso-hansi-font-family:"Times New Roman"'>真实姓名</span></p>
110. </td>
111. <td width=189 valign=top style='width:
112. border-left:none;mso-border-left-alt:solid windowtext
113. <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
114. mso-hansi-font-family:"Times New Roman"'>性别</span></p>
115. </td>
116. </tr>
117. <%
118. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
119.
120. String url="jdbc:odbc:mydb";
121.
122. //连接mydb数据库
123. Connection con=DriverManager.getConnection (url, "", "");
124.
125. try{
126. Statement stmt=con.createStatement();
127.
128. //查询employee表
129. ResultSet rs=stmt.executeQuery("select user_name, real_name, gender from employee ");
130.
131. while(rs.next()){
132. %>
133.
134. <tr>
135. <td width=189 valign=top style='width:
136. border-top:none;mso-border-top-alt:solid windowtext
137. <p class=MsoNormal><span lang=EN-US><%=rs.getString("user_name")%></span></p>
138. </td>
139. <td width=189 valign=top style='width:
140. none;border-bottom:solid windowtext
141. mso-border-top-alt:solid windowtext
142. padding:
143. <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
144. mso-hansi-font-family:"Times New Roman"'><%=rs.getString("real_name")%></span></p>
145. </td>
146. <td width=189 valign=top style='width:
147. none;border-bottom:solid windowtext
148. mso-border-top-alt:solid windowtext
149. padding:
150. <p class=MsoNormal><span style='font-family:宋体;mso-ascii-font-family:"Times New Roman";
151. mso-hansi-font-family:"Times New Roman"'><%=rs.getString("gender")%></span></p>
152. </td>
153. </tr>
154.
155. <%
156. } // end while
157.
158. rs.close();
159. stmt.close();
160. } finally {
161. con.close();
162. }
163. %>
164.
165. </table>
166.
167. <p class=MsoNormal><span lang=EN-US><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></p>
168.
169. </div>
170.
171. </body>
172.
173. </html>
174.
- 使用jsp实现word、excel格式报表打印
- 使用jsp实现word、excel格式报表打印
- 使用jsp实现word、excel格式报表打印
- 使用jsp实现word、excel格式报表打印(二)
- 使用jsp实现word、excel格式报表打印
- 使用JSP实现WORD、EXCEL格式报表打印
- 使用jsp实现word、excel格式报表打印-JSP教程,Jsp/Servlet
- 利用Word打印报表(三)----在jsp中利用word打印指定格式的会议记录
- 12、借助Jacob实现Java打印报表(Excel、Word)
- JSP 实现报表打印
- jsp打印,导出word/excel
- Grid++Report6 B/S报表打印之Java,使用jsp打印报表实现
- WOrd ,excel实现打印功能
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- VC利用EXCEL模板实现报表打印
- 使用 水晶报表 实现打印
- 水晶报表导出为word,excel和pdf格式
- 水晶报表导出为word,excel和pdf格式
- 出门
- LInux DHCP 配置
- 常用+实用的SQL语句
- java集合类
- Apache和iis都启动不了,80端口被httpd.exe占用
- 使用JSP实现WORD、EXCEL格式报表打印
- 开始学习c++
- 接口
- 0804周一
- 抽象类和接口
- DNS攻击代码
- ARM Linux启动过程分析
- TOMCAT多域名绑定实现
- Java初学者容易混淆的几个问题