Use JSP to implement Word, Excel format report printing

Source: Internet
Author: User
Tags header implement odbc stmt xmlns
excel|js|word| Report Printing because both MS Word and Excel's documents support HTML text formatting, you can use Word or Excel to do the template, save as a Web page, and then change the HTML to a JSP, filling the data part dynamically, No, it's hard. Adjust format

The word page simply sets the following instruction 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 Office software installed and the page will be opened directly in IE with Word or Excel when the user accesses it.

This method advantage is the template design, the adjustment is convenient, does not need to use the complex poi or the JXL technology on the server side, also does not need to use the ActiveX control technology in the client, is more safe, convenient, easy to achieve the good printing effect.

Microsoft information about server-side dynamic creation of Office documents (ASP example):
http://support.microsoft.com/default.aspx?scid=kb;en-us;301044&

Simple example:

Use Word to create a document, drawing the following table:
----------------------------
| User name | Real name | sex |
----------------------------
| Guest | Passerby A | Male |
----------------------------
Save as a Web page test.htm, test.htm renamed to Test.jsp, modify the guest, passers-by a, male from the database dynamic query, as follows:



<%@ page contenttype= "APPLICATION/MSWORD;CHARSET=GBK"%>
<%@ page import= "java.sql.*"%>
xmlns:w= "Urn:schemas-microsoft-com:office:word"
xmlns= "HTTP://WWW.W3.ORG/TR/REC-HTML40" >

<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>
<!--[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 lb </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: Song body;
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: "\@ song Body";
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: Song body;
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>

<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>
&LT;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: Song body; mso-ascii-font-family: "Times New Roman";
Mso-hansi-font-family: "Times New Roman" ' > Username </span></p>
</td>
&LT;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: Song body; mso-ascii-font-family: "Times New Roman";
Mso-hansi-font-family: "Times New Roman" ' > real name </span></p>
</td>
&LT;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: Song body; mso-ascii-font-family: "Times New Roman";
Mso-hansi-font-family: "Times New Roman" ' > Sex </span></p>
</td>
</tr>
<%
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

String url= "Jdbc:odbc:mydb";

Connecting to the MyDB database
Connection con=drivermanager.getconnection (URL, "", "");

try{
Statement stmt=con.createstatement ();

Query Employee table
ResultSet rs=stmt.executequery ("Select user_name, Real_name, gender from employee");

while (Rs.next ()) {
%>

<tr>
&LT;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>
&LT;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: Song body; mso-ascii-font-family: "Times New Roman";
Mso-hansi-font-family: "Times New Roman" ' ><%=rs.getstring ("Real_name")%></span></p>
</td>
&LT;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: Song body; 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>






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.