Home > Web Front-end > JS Tutorial > body text

Method for exporting Excel (CSV) files using JS-compatible browsers_javascript skills

WBOY
Release: 2016-05-16 16:50:07
Original
1221 people have browsed it

A common way to export a table from JS to an Excel file is to call: ActiveXObject("Excel.Application"), but this method has limitations and can only be implemented in browsers under the IE series, and the compatibility is not ideal.

After testing, the method recommended in this article can export table contents to Excel files with good compatibility.

Copy code The code is as follows:

var str = "Blog, domain name nBlog, 2njb51.net , 3";
var uri = 'data:text/csv;charset=utf-8,' str;

var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "export.csv";

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);


This method is used in Google/Firefox series browsers It's OK, but in IE there will be an error message "The data area passed to the system call is too small" because the value specified by href has too many bytes.

So, for IE browser, I have to make a judgment and use ActiveXObject.

In this way, you can better use Javascript to export HTML content to Excel files.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template