JSP generate Excel report document source code

Source: Internet
Author: User

The Excel report method is too simple. One method can only be used on the window platform (because jdbc-odbc bridge is used) and various formulas or methods in Excel cannot be used. Therefore, today, we will introduce an apache component called POI, which can process word or Excel files on UNIX or Windows platforms without relying on window's com, you can also set storage format and printing format. Today, I will introduce the reading, adding, modifying and deleting data functions. If you study other functions, please Email me (ljj@mlc.edu.tw) and share it with you!
I. Need to use the file: jakarta-poi-1.8.0-dev-20020917.jar
The latest version 1.8.0 (but not the official version) is available almost every day. The official version is 1.5.0.
Http://jakarta.apache.org/builds/jakarta-poi/nightly/
Copy the file to the place indicated by classpath

2. If you are interested, refer
Http://jakarta.apache.org/poi/

 

The third party first sets up an Excel file named book1.xls. The content is as follows:
----------------------------------
Total project unit price
CPU 7000 5 35000
Hard Disk 2500 2 5000
Memory 1600 3 4800
----------------------------------
The total column space is set formula, unit price * quantity

Iv. Data Reading examples
<% @ Page contentType = "text/html; charset = MS950" import = "java. util. *, java. io. *" %>.
<% @ Page import = "org. apache. poi. poifs. filesystem. *, org. apache. poi. hssf. usermodel. *" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = MS950">
<Title> read an Excel file </title>
</Head>
<Body>
<Table border = "1" width = "100%">
<%
FileInputStream finput = new FileInputStream (application. getRealPath ("/") "book1.xls ");
// Set FileINputStream to read the Excel file
POIFSFileSystem fs = new POIFSFileSystem (finput );
HSSFWorkbook wb = new HSSFWorkbook (fs );
HSSFSheet sheet = wb. getSheetAt (0 );
// Read the first worksheet and declare it as sheet
Finput. close ();
HSSFRow row = null;
// Declare a column
HSSFCell cell = null;
// Declare a bucket
Short I = 0;
Short y = 0;
// Read data of all cells in a nest-like ring
For (I = 0; I <= sheet. getLastRowNum (); I)
{
Out. println ("<tr> ");
Row = sheet. getRow (I );
For (y = 0; y <row. getLastCellNum (); y)
{
Cell = row. getCell (y );.
Out. print ("<td> ");

// Determine the format of the bucket
Switch (cell. getCellType ())
{
Case HSSFCell. CELL_TYPE_NUMERIC:
Out. print (cell. getNumericCellValue ());
// GetNumericCellValue () returns the double value. If you do not want a decimal point, convert it to int.
Break;
Case HSSFCell. CELL_TYPE_STRING:
Out. print (cell. getStringCellValue ());
Break;
Case HSSFCell. CELL_TYPE_FORMULA:
Out. print (cell. getNumericCellValue ());
// Read the calculated value of the formula bucket.
// To read the formula content, use cell. getCellFormula ()
Break;
Default:
Out. print ("unknown format ");
Break;
}
Out. println ("</td> ");
}
Out. println ("</tr> ");
}
%>
</Table>
</Body>
</Html>

5. Add example
<% @ Page contentType = "text/html; charset = MS950" import = "java. util. *, java. io. *" %>.
<% @ Page import = "org. apache. poi. poifs. filesystem. *, org. apache. poi. hssf. usermodel. *" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = MS950">
<Title> insert data to an Excel file </title>
</Head>
<Body>
<%
FileInputStream finput = new FileInputStream (application. getRealPath ("/") "book1.xls ");
// Set FileINputStream to read the Excel file
POIFSFileSystem fs = new POIFSFileSystem (finput );
HSSFWorkbook wb = new HSSFWorkbook (fs );
HSSFSheet sheet = wb. getSheetAt (0 );
// Read the first worksheet and declare it as sheet
Finput. close ();
HSSFRow row = null;
// Declare a column
HSSFCell cell = null;
// Declare a bucket
Short I = 4;
Row = sheet. createRow (I );
// Create a new column. Note that it is the fifth column (the column and storage space are calculated from 0)
Cell = row. createCell (short) 0 );
Cell. setEncoding (HSSFCell. ENCODING_UTF_16 );
// Set the string of the bucket to store dual-bit Elements
Cell. setCellValue ("display card ");
Cell = row. createCell (short) 1 );
Cell. setCellValue (1700 );!
Cell = row. createCell (short) 2 );
Cell. setCellValue (8 );
Cell = row. createCell (short) 3 );
// Set the storage space as the formula storage space and enter the formula
Cell. setCellFormula ("B" (I 1) "* C" (I 1 ));
Try
{
FileOutputStream fout = new FileOutputStream (application. getRealPath ("/") "book1.xls ");
Wb. write (fout );
// Storage
Fout. close ();
Out. println ("Storage successful <a href1_book1.xls> book1.xls </a> ");
}
Catch (IOException e)
{
Out. println ("error generated, error message:" e. toString ());
}
%>
</Body>
</Html>

6. Delete and modify documents
<% @ Page contentType = "text/html; charset = MS950" import = "java. util. *, java. io. *" %>
<% @ Page import = "org. apache. poi. poifs. filesystem. *, org. apache. poi. hssf. usermodel. *" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = MS950">
Try to generate an Excel file directly from the web page. Although it is not directly from SQL to excel, it is more convenient and user-friendly .. .

I found a lot of information on the Internet and found a piece of javascript function implementation code, which is very good:
Var elTable = document. getElementById ("tb ");
Var oRangeRef = document. body. createTextRange ();
ORangeRef. moveToElementText (elTable );
ORangeRef.exe cCommand ("Copy ");.

Var appExcel = new ActiveXObject ("Excel. Application ");
AppExcel. Visible = true;
AppExcel. Workbooks. Add (). Worksheets. Item (1). Paste ();
AppExcel = null; a successful life needs to be managed by yourself. Don't talk about it again. Don't wait any longer. Now, make plans for your own life and make a light for your life, win at the beginning of life.

The implementation function is to generate all the data in the table with the id of tb into an excel file. I tried it, but put it on the server to run it as a script error prompt, and then I asked questions everywhere on the Internet, I also asked some of Baidu space's experts and friends, but I still cannot do it .. However, I am very grateful for their enthusiastic help.

No way, I can only implement it in other ways. In fact, I like the javascript function above and it is more user-friendly .. Unfortunately, I cannot use it because of my poor skills !~ !

Since I have helped clients write programs using jsp, I should use jsp to implement this function .. Copyright Disclaimer: All articles on this site are from the Internet. If there is any infringement, please contact 028-86262244-215. we will delete it immediately upon receipt. Thank you!

So we found this method:
From: http://www.jsp.mlc.edu.tw/viewitem.jsp? Itemid = 0000000065 according to the expert's observation, this theory and phenomenon are worth pondering. So I hope you can do more research and learn and try to sum up more and better experiences!


**************************************** **************************************** *******************
1. Display webpage data in an excel report in the form of online browsing
The original xls651.jsp code is as follows.
<% @ Page contentType = "application/msexcel" %>
<! -- In the preceding line, set this webpage to an excel webpage -->
<%
Response. setHeader ("Content-disposition", "inline; filename=test1.xls ");
// Set the file name test1.xls to be transferred to the browser.
// This line enables the browser to receive an excel file.
%>
<Html>
<Head>
<Title> Excel file presentation mode </title>
</Head>
<Body>
<Table border = "1" width = "100%">
<Tr>
<Td> name </td> <td> ID card size </td> <td> birthday </td>
</Tr>
<Tr>
<Td> Li Yun </td> <td> N111111111 </td> <td> 1900/11/12 </td>
</Tr>
<Tr>
<Td> Liang jingru </td> <td> N222222222 </td> <td> 1923/10/1 </td>
</Tr>
<Tr>
<Td> Zhang huimei </td> <td> N333333333 </td> <td> 1934/12/18 </td>
</Tr>
</Table> ..
</Body>
</Html>.

2. Use an excel report to download and present webpage Materials
This jsp file is similar to the previous one. Only the difference lies in this line:
Online browsing: response. setHeader ("Content-disposition", "inline; filename=test1.xls ");
Download Method: response. setHeader ("Content-disposition", "attachment; filename=test2.xls ");
Note: 1. the excel files are displayed only on static Web pages. With the help of the database, you can use dynamic reports to make excel reports.
2. There is no need to install any components at all, or it can only be restricted to the win platform. A simple solution to your report needs
3. If you are smart, you should be able to show your website information in word doc format!
(Do you only need to change contentType to "application/msword" and the filename's sub-document name to .doc?

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.