Support Ie,firefox,chrome three major browsers, through Js+flash way to export the table Excel file

Source: Internet
Author: User

Today in the project, encountered the front-end download Excel function, the result of the original code, as follows:

function Generate_excel (tableid) {
var table = document.getElementById (TableID);
var html = table.outerhtml;
window.open (' data:application/vnd.ms-excel;base64, ' + base64_encode (HTML));
}

This kind of writing, can only support ff,chrome, cannot support under IE. Search on the Internet, you can not find a better jquery plug-in, the ability to cross three platforms simultaneously, through the jquery way to export HTML table Excel.

Later, a chance coincidence, found a flash, can support a variety of browsers at the same time. For sites that do not repel the use of flash, it may be a solution.

However, currently used, this method has two drawbacks

You need to allow the flash to run in 1.firefox

Export in 2.IE, Excel line slightly somewhat problematic

Here's how to use

Flash: https://github.com/dcneiner/Downloadify

Example code:

In HTML

<p id= "Downloadify" >

<object id= "downloadify_obj" width= "" "height=" "type=" Application/x-shockwave-flash "name=" Downloadify_obj " Data= ". /.. /scripts/tableexport/downloadify.swf ">
<param name= "allowscriptaccess" value= "Always" >
<param name= "wmode" value= "Transparent" >
<param name= "Flashvars" value= "queue_name=downloadify&width=100&height=30&downloadimage=images/ Download.png ">
</object>
</p>

Among them, Data= ". /.. /scripts/tableexport/downloadify.swf ", downloadimage=images/download.png" need to change to their own corresponding path

JS in

<script type= "Text/javascript" >//<! [cdata[
Downloadify.create (' downloadify ', {
Filename:function () {
return "Data.xls";
},
Data:function () {
var table = document.getElementById (' table ');
var html = table.outerhtml;
return HTML;
},
Oncomplete:function () {
Alert (' Your File has Been saved! ');
},
Oncancel:function () {
Alert (' You are cancelled the saving of this file. ');
},
Onerror:function () {
Alert (' You must put something in the File Contents or there would be is nothing to save! ');
},
Transparent:false,
SWF: '.. /.. /scripts/tableexport/downloadify.swf ',
Downloadimage: '. /.. /scripts/tableexport/download.png ',
WIDTH:100,
Height:30,
Transparent:true,
Append:false
});
]]></script>

which

The name of filename was modified by itself,

In data, write the name of your table

SWF: '.. /.. /scripts/tableexport/downloadify.swf ',
Downloadimage: '. /.. /scripts/tableexport/download.png ',

Write your own corresponding path. Others can find their own online API

Support Ie,firefox,chrome three major browsers, through Js+flash way to export the table Excel file

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.