Title: use JSP to print reports in Word and Excel formats Author: Evan Email: evan_zhao@hotmail.com Date: 2003-08-21 Because documents in MS Word and Excel support HTML text format, you can use Word or Excel to make a template, save it as a Web page, change the HTML to JSP, and enter the data part dynamically, no need to adjust the format
On the word page, you only need to set the following commands in the JSP header:
<% @ Page contenttype = "application/MSWord; charset = GBK" %>
Excel is as follows:
<% @ Page contenttype = "application/vnd. MS-Excel; charset = GBK" %> In this way, the client must have the office software installed. When you access the client, you can directly open the page in word or excel in IE. The advantage of this method is that it is easy to design and adjust templates. You do not need to use complicated poi or jxl technology on the server side, or use ActiveX Control Technology on the client side, which is safer and more convenient, easy to achieve better printing results. Microsoft documents on Dynamically creating an office on the server (ASP example ):
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 301044 &
Simple Example: Use Word to create a document, as shown in the following table:
----------------------------
| User name | real name | gender |
----------------------------
| Guest | passer-by | male |
----------------------------
Save it as webpage test.htm, change test.htm to test. jsp, and modifyGuest, Lu renjia, maleFor dynamic queries from the database, as follows:
-
-
- <% @ Page contenttype = "application/MSWord; charset = GBK" %>
-
- <% @ PageImport= "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> User Information </title>
-
- <! --[IfGte 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] --> <! --[IfGte mso 9]> <XML>
-
- <W: worddocument>
-
- <W: punctuationkerning>
- <W: drawinggridverticalspacing> 7.8 lbs </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: 0 cm;
-
- 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 '>
-
-
- <DivClass= 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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <Tr>
-
- <TD width = 189 valign = top style = 'width: 142.0pt; Border: solid windowtext. 5pt;
-
- Padding: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
-
- MSO-Hansi-font-family: "Times New Roman" '> User Name </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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
-
- MSO-Hansi-font-family: "Times New Roman" '> real name </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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
- MSO-Hansi-font-family: "Times New Roman" '> gender </span> </P>
-
- </TD>
-
- </Tr>
-
- <%
-
- Class. Forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
-
-
- StringUrl = "JDBC: ODBC: mydb ";
-
-
- // Connect to the mydb Database
-
- Connection con = drivermanager. getconnection (URL ,"","");
-
-
- Try{
-
- Statement stmt = con. createstatement ();
-
-
- // Query the employee table
-
- Resultset rs1_stmt.exe cutequery ("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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= 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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= 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: 0 cm 5.4pt 0 cm 5.4pt '>
-
- <PClass= 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>
-
- <PClass= Msonormal> <SPAN lang = en-US> <! [If! Supportemptyparas]> <! [Endif]> <O: P> </span> </P>
-
-
- </Div>
-
-
- </Body>
-
-
- </Html>
-
-
Related links:
In good J2EEProgramThe database query may be written in a special Dao (Data Access Object). For how to conveniently query data and pass the list to the JSP page, see:
Common JSP page query display modes Reference: how to print reports in Web Applications
Copyright Notice to the author |
This article ArticleIs it helpful to you? Vote: Yes NoVoting Result: 2 0 |
|
|
Other articles by the author:
- JSP paging technology implementation
- Common JSP page query display modes
Author all articles |
|