Use JSP to print Word and Excel reports (2)[Date:] Source: http://www.gs008.com/blog/blogview.asp? Logid = 325
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
Method 2
1. Create a report style in word or Excel.
The Row Height and column width in Excel cannot be expressed in mm or cm.
Computing required. In 1024*768, 1CM is about 38 pixels.
Based on this, you can calculate and control the row and column positions of reports.
2. Set "table-duplicate header row" in word ".
In Excel, set "file-page settings-worksheet-Print Title ".
3. Save it as a web page and change the suffix "htm" to "jsp ".
4. The word report file header is:
<% @ Page contenttype = "application/MSWord; charset = GBK" Language = "Java" %>
The Excel report file header is:
<% @ Page contenttype = "application/vnd. MS-Excel; charset = GBK" Language = "Java" %>
5. Define the print parameter variables in the file header section as follows (take excel as an example ):
<%
// Number of records to be printed
Int printrowcount = rowcount;
// Number of records printed per page
Int pagerowcount = 16;
// The number of empty rows to be printed on the last page
Int loopnum = pagerowcount-printrowcount % pagerowcount;
// Print the height of the area. 2 indicates the number of header rows that need to be repeated on each page.
Int printareaheight = (printrowcount % pagerowcount = 0 )? Printrowcount + 2: printrowcount + loopnum + 2;
%>
6. After <style> </style>, modify <! -- [If gte mso 9] and <! [Endif] --> the XML tag content in the tag, mainly to set the dynamic printing area (take excel as an example ):
<%
Out. Print ("......
"<X: excelname>" +
"<X: Name> print_area </X: Name>" +
"<X: sheetindex> 1 </X: sheetindex>" +
"<X: formula> = sheet1! $ A $1: $ G $ "+ printareaheight +" </X: formula> "+
"</X: excelname>" +
......");
%>
7. Other JSP codes.
The following section uses excel as an example to describe how to print the subject cyclically ):
<%
For (INT I = 1; I <= printrowcount; I ++)
{
Out. Print ("<tr>" <TD> "+ I +" </TD> </tr> "); // content available
}
If (loopnum! = Pagerowcount) // if there are empty rows, the blank rows are printed to fill in the last page.
For (Int J = 1; j <= loopnum; j ++)
{
Out. Print ("<tr> <TD> </tr>"); // NO content
}
%>
8. Test and continue Modification
1. Create a report style in word or Excel.
The Row Height and column width in Excel cannot be expressed in mm or cm.
Computing required. In 1024*768, 1CM is about 38 pixels.
Based on this, you can calculate and control the row and column positions of reports.
2. Set "table-duplicate header row" in word ".
In Excel, set "file-page settings-worksheet-Print Title ".
3. Save it as a web page and change the suffix "htm" to "jsp ".
4. The word report file header is:
<% @ Page contenttype = "application/MSWord; charset = GBK" Language = "Java" %>
The Excel report file header is:
<% @ Page contenttype = "application/vnd. MS-Excel; charset = GBK" Language = "Java" %>
5. Define the print parameter variables in the file header section as follows (take excel as an example ):
<%
// Number of records to be printed
Int printrowcount = rowcount;
// Number of records printed per page
Int pagerowcount = 16;
// The number of empty rows to be printed on the last page
Int loopnum = pagerowcount-printrowcount % pagerowcount;
// Print the height of the area. 2 indicates the number of header rows that need to be repeated on each page.
Int printareaheight = (printrowcount % pagerowcount = 0 )? Printrowcount + 2: printrowcount + loopnum + 2;
%>
6. After <style> </style>, modify <! -- [If gte mso 9] and <! [Endif] --> the XML tag content in the tag, mainly to set the dynamic printing area (take excel as an example ):
<%
Out. Print ("......
"<X: excelname>" +
"<X: Name> print_area </X: Name>" +
"<X: sheetindex> 1 </X: sheetindex>" +
"<X: formula> = sheet1! $ A $1: $ G $ "+ printareaheight +" </X: formula> "+
"</X: excelname>" +
......");
%>
7. Other JSP codes.
The following section uses excel as an example to describe how to print the subject cyclically ):
<%
For (INT I = 1; I <= printrowcount; I ++)
{
Out. Print ("<tr>" <TD> "+ I +" </TD> </tr> "); // content available
}
If (loopnum! = Pagerowcount) // if there are empty rows, the blank rows are printed to fill in the last page.
For (Int J = 1; j <= loopnum; j ++)
{
Out. Print ("<tr> <TD> </tr>"); // NO content
}
%>
8. Test and continue Modification
1. Create a report style in word or Excel.
The Row Height and column width in Excel cannot be expressed in mm or cm.
Computing required. In 1024*768, 1CM is about 38 pixels.
Based on this, you can calculate and control the row and column positions of reports.
2. Set "table-duplicate header row" in word ".
In Excel, set "file-page settings-worksheet-Print Title ".
3. Save it as a web page and change the suffix "htm" to "jsp ".
4. The word report file header is:
<% @ Page contenttype = "application/MSWord; charset = GBK" Language = "Java" %>
The Excel report file header is:
<% @ Page contenttype = "application/vnd. MS-Excel; charset = GBK" Language = "Java" %>
5. Define the print parameter variables in the file header section as follows (take excel as an example ):
<%
// Number of records to be printed
Int printrowcount = rowcount;
// Number of records printed per page
Int pagerowcount = 16;
// The number of empty rows to be printed on the last page
Int loopnum = pagerowcount-printrowcount % pagerowcount;
// Print the height of the area. 2 indicates the number of header rows that need to be repeated on each page.
Int printareaheight = (printrowcount % pagerowcount = 0 )? Printrowcount + 2: printrowcount + loopnum + 2;
%>
6. After <style> </style>, modify <! -- [If gte mso 9] and <! [Endif] --> the XML tag content in the tag, mainly to set the dynamic printing area (take excel as an example ):
<%
Out. Print ("......
"<X: excelname>" +
"<X: Name> print_area </X: Name>" +
"<X: sheetindex> 1 </X: sheetindex>" +
"<X: formula> = sheet1! $ A $1: $ G $ "+ printareaheight +" </X: formula> "+
"</X: excelname>" +
......");
%>
7. Other JSP codes.
The following section uses excel as an example to describe how to print the subject cyclically ):
<%
For (INT I = 1; I <= printrowcount; I ++)
{
Out. Print ("<tr>" <TD> "+ I +" </TD> </tr> "); // content available
}
If (loopnum! = Pagerowcount) // if there are empty rows, the blank rows are printed to fill in the last page.
For (Int J = 1; j <= loopnum; j ++)
{
Out. Print ("<tr> <TD> </tr>"); // NO content
}
%>
8. Test and continue Modification
III,
<Object classid = "CLSID: 0002e510-0000-0000-c000-000000000046" id = "spreadsheet1"
Style = "height: 100%; width: 100%" width = "14" Height = "14">
<Param name = "htmlurl" value = "<% = strurl %>">
<Param name = "htmldata" value = "">
<Param name = "datatype" value = "htmlurl">
<Param name = "autofit" value = "0">
<Param name = "displaycolheaders" value = "0">
<Param name = "displaygridlines" value = "0">
<Param name = "displayhorizontalscrollbar" value = "-1">
<Param name = "displayrowheaders" value = "0">
<Param name = "displaytitlebar" value = "0">
<Param name = "displaytoolbar" value = "-1">
<Param name = "displayverticalscrollbar" value = "-1">
<Param name = "enableautocalculate" value = "-1">
<Param name = "enableevents" value = "0">
<Param name = "moveafterreturn" value = "0">
<Param name = "moveafterreturndirection" value = "0">
<Param name = "righttoleft" value = "0">
<Param name = "viewablerange" value = "1:100">
</Object>