Implementation of jsp Printing Method for excel and word Documents

  • 2021-09-05 00:37:55
  • OfStack

In this paper, an example of jsp implementation for excel and word document printing method. Share it for your reference, as follows:

Because the documents of ms, word and excel all support the text format of html, you can first use word or excel to make a template and save it as an Web page, then change the html to jsp, and fill in the data part dynamically without having to adjust the format very hard

The word page simply sets the following command 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 user will open the page directly with word or excel in ie when visiting.

This method has the advantages of convenient template design and adjustment, no need to use complex POI or jxl technology at the server end, and no need to use ActiveX control technology at the client end, thus achieving better printing effect more safely, conveniently and easily.

microsoft Information on Server-side Dynamic Creation of office Documents (asp Sample):

http://support.microsoft.com/default.aspx?scid=KB;en-us;301044 &

Simple example:

Use word to create a 1 document and draw the following table:

----------------------------
User name Real name Gender
----------------------------
guest passerby a man
----------------------------

Save as Web page test. htm, rename test. htm as test. jsp, modify guest, Passerby A and Male to query dynamically from database, as follows:


<%@ 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> 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  Pounds </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 Style ;
  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 Style ";
  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 Style ;
  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: Song Style ;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:0cm 5.4pt 0cm 5.4pt'>
 <p class=MsoNormal><span style='font-family: Song Style ;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:0cm 5.4pt 0cm 5.4pt'>
 <p class=MsoNormal><span style='font-family: Song Style ;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");
String url="jdbc:odbc:mydb";
// Connect 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>
 <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: Song Style ;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: Song Style ;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>

I hope this article is helpful to everyone's JSP programming.


Related articles: