How to export JSP data to EXCEL

Source: Internet
Author: User

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;

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 (the preferred choice for SUN Enterprise applications), I will still use jsp (the preferred choice for SUN Enterprise applications) to implement this function ..

So we found this method:
Http://www.jsp (SUN Enterprise Application preferred) .mlc.edu.tw/viewitem.jsp (SUN Enterprise Application preferred )? Itemid = 0000000065.


**************************************** **************************************** *******************
1. Display webpage data in an excel report in the form of online browsing
The source code of xls651.jsp 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 (preferred for SUN enterprise-level applications) file is similar to the previous one, with only the difference 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 subdocument name to .doc !)

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.