使用jsp實現word、excel格式報表列印

來源:互聯網
上載者:User
excel|js|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>





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.