How to export data in excel using jquery

Source: Internet
Author: User

Because javascript itself does not have the permission to operate local files, unless ActiveX is used, it is troublesome and insecure and does not need to be used at all. therefore, it is impossible to obtain the local file for saving data from the table on the page.

What we want to export is the data in the table, and the data in the table is from the server, so we can save the data on the server to a local file.

Server implementation code:

Copy codeThe Code is as follows:
ServletOutputStream out = null;
Try {
// Set the output csv Header
Response. setContentType ("text/csv ");
String disposition = "attachment; fileName=data.csv ";
Response. setHeader ("Content-Disposition", disposition );
// Obtain the output object
Out = response. getOutputStream ();
// Obtain data
Byte [] blobData = CSVParser. parseCsv (rs). getBytes ();
Out. write (blobData );
Out. flush ();
Out. close ();
} Catch (Exception e ){
Throw e;
} Finally {
If (out! = Null)
Out. close ();
}

It is necessary to explain the code in CSVParser. parseCsv (rs ). CSVParser is a class that I implement elsewhere to convert the ResultSet object found in the database to CSV data. the rs parameter of the parseCsv method is the ResultSet object. returns string data in csv format.

I used iframe to download the client. I wrote a general method. You can put this function in a js file and call it directly on the page.


Copy codeThe Code is as follows:
// The csv file is returned Based on the queried data.
Function bsuExportCsv (url ){
// If the iframe is not downloaded on the page, add the iframe to the page.
If ($ ('# downloadcsv'). length <= 0)
$ ('Body'). append ("<iframe id = \" downloadcsv \ "style = \" display: none \ "> </iframe> ");
$ ('# Downloadcsv'). attr ('src', url );
}

The url is the servlet address to request data. The url must be in csv format.

First, determine whether there is an iframe with the id of downloadcsv in the page. If the iframe is not added to the body tag, then set the src attribute of iframe to the passed url address.

You can call bsuExportCsv ("http: // localhost: 8080/csvservelt") on the page to export data.

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.