Use JSP to print reports in Word and Excel formats

Source: Internet
Author: User



Use JSP to print reports in Word and Excel formats

Http://www.javaresearch.org/article/showarticle.jsp? Column = 106 & Thread = 8444
Evan original (Participation score: 35753, expert score: 640) published: Update: Version: 1.0 read:9045Times



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:

  1. <% @ Page contenttype = "application/MSWord; charset = GBK" %>
  2. <% @ PageImport= "Java. SQL. *" %>
  3. <HTML xmlns: O = "urn: Schemas-Microsoft-com: Office: office"
  4. Xmlns: W = "urn: Schemas-Microsoft-com: Office: Word"
  5. Xmlns = "http://www.w3.org/TR/REC-html40">
  6. <Head>
  7. <Meta http-equiv = Content-Type content = "text/html; charset = gb2312">
  8. <Meta name = progid content = word. Document>
  9. <Meta name = generator content = "Microsoft Word 9">
  10. <Meta name = originator content = "Microsoft Word 9">
  11. <Title> User Information </title>
  12. <! --[IfGte mso 9]> <XML>
  13. <O: documentproperties>
  14. <O: Author> Evan Zhao </O: Author>
  15. <O: lastauthor> Evan Zhao </O: lastauthor>
  16. <O: Revision> 1 </O: Revision>
  17. <O: totaltime> 1 </O: totaltime>
  18. <O: created> 2003-08-20t16: 26: 00Z </O: created>
  19. <O: lastsaved> 2003-08-20t16: 27: 00Z </O: lastsaved>
  20. <O: pages> 1 </O: pages>
  21. <O: Company> Taiping </O: Company>
  22. <O: lines> 1 </O: lines>
  23. <O: paragraphs> 1 </O: paragraphs>
  24. <O: version> 9.2812 </O: version>
  25. </O: documentproperties>
  26. </XML> <! [Endif] --> <! --[IfGte mso 9]> <XML>
  27. <W: worddocument>
  28. <W: punctuationkerning>
  29. <W: drawinggridverticalspacing> 7.8 lbs </W: drawinggridverticalspacing>
  30. <W: displayhorizontaldrawinggridevery> 0 </W: displayhorizontaldrawinggridevery>
  31. <W: displayverticaldrawinggridevery> 2 </W: displayverticaldrawinggridevery>
  32. <W: Compatibility>
  33. <W: spaceforul>
  34. <W: balancesinglebytedoublebytewidth>
  35. <W: donotleavebackslashalone>
  36. <W: ultrailspace>
  37. <W: donotexpandshiftreturn>
  38. <W: adjustlineheightintable>
  39. <W: usefelayout>
  40. </W: Compatibility>
  41. </W: worddocument>
  42. </XML> <! [Endif] -->
  43. <Style>
  44. <! --
  45.  /* Font Definitions */
  46. @ Font-face
  47. {Font-family:;
  48. Panose-1: 2 1 6 0 3 1 1 1 1 1;
  49. MSO-font-alt: simsun;
  50. MSO-font-charset: 134;
  51. MSO-generic-font-family: auto;
  52. MSO-font-pitch: variable;
  53. MSO-font-Signature: 3 135135232 16 0 262145 0 ;}
  54. @ Font-face
  55. {Font-family: "\ @ ";
  56. Panose-1: 2 1 6 0 3 1 1 1 1 1;
  57. MSO-font-charset: 134;
  58. MSO-generic-font-family: auto;
  59. MSO-font-pitch: variable;
  60. MSO-font-Signature: 3 135135232 16 0 262145 0 ;}
  61.  /* Style definitions */
  62. P. msonormal, Li. msonormal, Div. msonormal
  63. {MSO-style-parent :"";
  64. Margin: 0 cm;
  65. Margin-bottom:. 0001pt;
  66. Text-align: justify;
  67. Text-justify: Inter-ideograph;
  68. MSO-pagination: none;
  69. Font-size: 10.5pt;
  70. MSO-bidi-font-size: 12.0pt;
  71. Font-family: "Times New Roman ";
  72. MSO-Fareast-font-family:;
  73. MSO-font-kerning: 1.0pt ;}
  74.  /* Page Definitions */
  75. @ Page
  76. {MSO-page-border-Surround-header: No;
  77. MSO-page-border-Surround-footer: No ;}
  78. @ Page Section1
  79. {Size: 595.3pt 841.9pt;
  80. Margin: 72.0pt 90.0pt 72.0pt 90.0pt;
  81. MSO-header-margin: 42.55pt;
  82. MSO-footer-margin: 49.6pt;
  83. MSO-paper-Source: 0;
  84. Layout-grid: 15.6pt ;}
  85. Div. Section1
  86. {Page: Section1 ;}
  87. -->
  88. </Style>
  89. </Head>
  90. <Body lang = ZH-CN style = 'tab-interval: 21.0pt; text-justify-Trim: punctuation '>
  91. <DivClass= Section1 style = 'layout-grid: 15.6pt '>
  92. <Table border = 1 cellspacing = 0 cellpadding = 0 style = 'border-collapse: collapse;
  93. Border: none; MSO-border-alt: solid windowtext. 5pt; MSO-padding-alt: 0 cm 5.4pt 0 cm 5.4pt '>
  94. <Tr>
  95. <TD width = 189 valign = top style = 'width: 142.0pt; Border: solid windowtext. 5pt;
  96. Padding: 0 cm 5.4pt 0 cm 5.4pt '>
  97. <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
  98. MSO-Hansi-font-family: "Times New Roman" '> User Name </span> </P>
  99. </TD>
  100. <TD width = 189 valign = top style = 'width: 142.05pt; Border: solid windowtext. 5pt;
  101. Border-left: none; MSO-border-left-alt: solid windowtext. 5pt; padding: 0 cm 5.4pt 0 cm 5.4pt '>
  102. <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
  103. MSO-Hansi-font-family: "Times New Roman" '> real name </span> </P>
  104. </TD>
  105. <TD width = 189 valign = top style = 'width: 142.05pt; Border: solid windowtext. 5pt;
  106. Border-left: none; MSO-border-left-alt: solid windowtext. 5pt; padding: 0 cm 5.4pt 0 cm 5.4pt '>
  107. <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
  108. MSO-Hansi-font-family: "Times New Roman" '> gender </span> </P>
  109. </TD>
  110. </Tr>
  111. <%
  112. Class. Forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
  113. StringUrl = "JDBC: ODBC: mydb ";
  114. // Connect to the mydb Database
  115. Connection con = drivermanager. getconnection (URL ,"","");
  116. Try{
  117. Statement stmt = con. createstatement ();
  118. // Query the employee table
  119. Resultset rs1_stmt.exe cutequery ("select user_name, real_name, gender from employee ");
  120. While(Rs. Next ()){
  121. %>
  122. <Tr>
  123. <TD width = 189 valign = top style = 'width: 142.0pt; Border: solid windowtext. 5pt;
  124. Border-top: none; MSO-border-top-alt: solid windowtext. 5pt; padding: 0 cm 5.4pt 0 cm 5.4pt '>
  125. <PClass= Msonormal> <SPAN lang = en-US> <% = Rs. getstring ("user_name") %> </span> </P>
  126. </TD>
  127. <TD width = 189 valign = top style = 'width: 142.05pt; border-top: none; border-left:
  128. None; border-bottom: solid windowtext. 5pt; border-Right: solid windowtext. 5pt;
  129. MSO-border-top-alt: solid windowtext. 5pt; MSO-border-left-alt: solid windowtext. 5pt;
  130. Padding: 0 cm 5.4pt 0 cm 5.4pt '>
  131. <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
  132. MSO-Hansi-font-family: "Times New Roman" '> <% = Rs. getstring ("real_name") %> </span> </P>
  133. </TD>
  134. <TD width = 189 valign = top style = 'width: 142.05pt; border-top: none; border-left:
  135. None; border-bottom: solid windowtext. 5pt; border-Right: solid windowtext. 5pt;
  136. MSO-border-top-alt: solid windowtext. 5pt; MSO-border-left-alt: solid windowtext. 5pt;
  137. Padding: 0 cm 5.4pt 0 cm 5.4pt '>
  138. <PClass= Msonormal> <span style = 'font-family:; MSO-ascii-font-family: "Times New Roman ";
  139. MSO-Hansi-font-family: "Times New Roman" '> <% = Rs. getstring ("gender") %> </span> </P>
  140. </TD>
  141. </Tr>
  142. <%
  143. }// End while
  144. Rs. Close ();
  145. Stmt. Close ();
  146. }Finally{
  147. Con. Close ();
  148. }
  149. %>
  150. </Table>
  151. <PClass= Msonormal> <SPAN lang = en-US> <! [If! Supportemptyparas]> <! [Endif]> <O: P> </span> </P>
  152. </Div>
  153. </Body>
  154. </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

reviewer: tangzh score: 6 Expert score: 0 published on:
let's talk about your feelings:
I feel no value in using this method to print reports, for example: the special symbols (diagonal bars) in the table cannot be processed, the pages cannot be automated, and the header at the end of each page cannot be automatically added...
however, if you print a report in PDF format, the advantages of the report can be displayed.
reviewer: fetchpro score: 15 expert score: 0 published on:
agree with the upstairs comments! Word is not suitable for report generation.
I use the org.faceless.pdf software package to make PDF reports and use the Adobe Reader function.

COMMENTATOR: Evan score: 35753 expert score: 640 from: Shanghai
published at, December 3,
This is a simple solution and is suitable for lightweight applications without table-preserving tools. In word, you can use the header and footer, or use a paging character to control pages. Formulas can be embedded in Excel to facilitate operations by business departments, in addition, users can re-process this result
the biggest problem with PDF printing is how to design the page layout and font, if you have a convenient visual editor for template creation,

if you want to make professional reports, you still need professional table maintenance tools, various file formats can be easily generated

comments: wanglaobie score: 9 expert score: 0 published on:
in this way, the report is deformed. Is there any way to output the report to word without deformation?
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.