溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JSP如何實(shí)現(xiàn)打印報表功能

發(fā)布時間:2021-07-14 14:47:54 來源:億速云 閱讀:205 作者:chen 欄目:編程語言

這篇文章主要講解了“JSP如何實(shí)現(xiàn)打印報表功能”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“JSP如何實(shí)現(xiàn)打印報表功能”吧!

因?yàn)閙s word和excel的文檔都支持html文本格式,因此可以先用word或excel做好模版,另存為Web頁,然后將該html改成jsp,將數(shù)據(jù)部分動態(tài)填入即可,不用很辛苦的調(diào)整格式即可實(shí)現(xiàn)JSP打印報表。

word頁面只要在jsp頭設(shè)置如下指令:

< %@page contentType="application/msword;charset=GBK" %>

excel如下:

< %@page contentType="application/vnd.ms-excel;charset=GBK" %>

使用這種方式客戶端必須安裝有office軟件,用戶訪問時將在ie中直接用word或excel打開該頁面。

使用JSP打印報表的優(yōu)勢是模板設(shè)計(jì)、調(diào)整方便,無需在服務(wù)器端使用復(fù)雜的POI或jxl技術(shù),也無需在客戶端使用ActiveX控件技術(shù),更安全、方便,輕松實(shí)現(xiàn)較好的打印效果。  

JSP打印報表簡單示例:

使用word建立一文檔,畫表格如下:
----------------------------
| 用戶名 | 真實(shí)姓名 | 性別 |
----------------------------
| guest  | 路人甲   | 男   |
----------------------------

保存為Web頁test.htm, 將test.htm改名為test.jsp,修改其中g(shù)uest、路人甲、男為從數(shù)據(jù)庫動態(tài)查詢,如下:

< %@ 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"'>真實(shí)姓名< /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數(shù)據(jù)庫   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打印報表的實(shí)例就這樣完成了。

感謝各位的閱讀,以上就是“JSP如何實(shí)現(xiàn)打印報表功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對JSP如何實(shí)現(xiàn)打印報表功能這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

jsp
AI