How to export an Excel (CSV) file using a JS-compatible Browser

Source: Internet
Author: User

One common way to export a table as an Excel file in Js is to call ActiveXObject ("Excel. application "), but this method has limitations and can only be implemented in browsers in the IE series, but the compatibility is not ideal.

After testing, the recommended method can export the table content to an Excel file with good compatibility.
Copy codeThe Code is as follows:
Var str = "blog, domain name \ nBlog, 2 \ njb51.net, 3 ";
Var uri = 'data: text/csv; charset = UTF-8, '+ str;

Var downloadLink = document. createElement ("");
DownloadLink. href = uri;
DownloadLink. download = "export.csv ";

Document. body. appendChild (downloadLink );
DownloadLink. click ();
Document. body. removeChild (downloadLink );
This method is OK in Google/Firefox browsers, but an error message "The data area passed to the system call is too small" appears in IE, the reason is that the number of bytes specified by href is too large.

So, the IE browser has to judge that the ActiveXObject format is used.

In this way, you can use Javascript to export HTML content to an Excel file.

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.