Export a table from a Web page to Excel

Source: Internet
Author: User

It's the client's request.

What he means is that the data in the database can be exported to Excel

But the last Sql>>excel function I think is not very good, because the data is exported, but the relevant properties and names of data, each column of data is nothing to see. Must be judged according to the SQL column name, this is more troublesome, the customer will not be satisfied.

So I decided to try to make the table directly from the Web page Excel, although not directly from SQL to Excel, but it is more convenient and user-friendly.

The internet has found a lot of information, found a piece of JavaScript function to implement code, very good:
var eltable = document.getElementById ("TB");
var orangeref = Document.body.createTextRange ();
Orangeref.movetoelementtext (eltable);
Orangeref.execcommand ("Copy");

var appexcel = new ActiveXObject ("Excel.Application");
Appexcel.visible = true;
APPEXCEL.WORKBOOKS.ADD (). Worksheets.item (1). Paste ();
Appexcel = null;

The implementation function is to make the data in the table ID TB into Excel, I tried a feasible, but put to the server to run is the script error prompts, and then ask around the Internet, but also to find some of the Baidu space master friend asked, but still not its law. But thank you very much for their kind help.

No way, I can only use other methods to achieve, in fact, like the above JavaScript function, more humane. Unfortunately, I am at my wits ' wit, can't use it, eh. ~

Since I help the client use JSP to write the program, then I will use JSP to achieve this function bar.

So I found a way to do this:
Original: http://www.jsp.mlc.edu.tw/viewitem.jsp?itemid=0000000065

***************************************************************************************************
First, the Web page data in Excel report to the online browsing method presented
Xls651.jsp The original code is as follows


<%@ page contenttype= "Application/msexcel"%>
<!--above this line to set this page for Excel format pages-->
<%
Response.setheader ("content-disposition", "inline; Filename=test1.xls");
The above line sets the filename to Test1.xls when it is passed to the front-end browser.
This is the line that allows the front-end browser to assume that an Excel file is received
%>
<title>excel File Presentation Mode </title>
<body>
<table border= "1" width= "100%" >
<tr>
<td> name </td><td> ID card size </td><td> birthday </td>
</tr>
<tr>
<td> Coco </td><td>N111111111</td><td>1900/11/12</td>
</tr>
<tr>
<td> Liang </td><td>N222222222</td><td>1923/10/1</td>
</tr>
<tr>
<td> Sherry </td><td>N333333333</td><td>1934/12/18</td>
</tr>
</table>
</body>

Second, the Web page information in the form of Excel report presented
This JSP file is similar to the previous one, only the difference is in this line:
Online Browsing mode: Response.setheader ("content-disposition", "inline; Filename=test1.xls");
Download the way: Response.setheader ("Content-disposition", "attachment; Filename=test2.xls");
Note: 1. The above purely static Web page rendering Excel file, with the database, you will be able to dynamic report form to make Excel report
2. Do not install any components, or only limited to the win platform, a simple way to solve your report requirements
3. Smart you, should be able to extrapolate, know how to the Web page information in Word doc format to show it.
(is not as long as the contenttype changed to "Application/msword" and filename of the secondary file name changed to. doc on it. )

***************************************************************************************************

I tried it and found it good, so I used it to do it. function finally realized.

Think about it, in fact, this method is pretty good, but to achieve the function, the code to write a little more than that javascirpt trouble points. Also fortunately office function is more powerful, hehe

Feeling: After work, according to customer requirements, has been doing this to do that, in fact, it is precisely because of this, learned a lot of things, self-study ability is also enhanced a lot, the work of these few months than in the university in a few years to learn more ... Perhaps it is because of this, there will be a little sense of achievement, but also let me more like to work, do not feel the work boring, hey ~ or this is a kind of motivation

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.