将HTML导出为ex​​cel [英] Export HTML to excel

查看:81
本文介绍了将HTML导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用javascript将html表导出为ex​​cel。我在打开excel文件之前弹出这个:

如果你尝试打开test.xls,格式与扩展文件指定的格式不同



我尝试过:



这是我的代码:

 var tableToExcel =(function(){
var uri ='data:application / vnd.ms-excel; base64,'
,template ='< html xmlns:o =urn:schemas- microsoft-com:office:officexmlns:x =urn:schemas-microsoft-com:office:excel'+
'xmlns =http://www.w3.org/TR/REC-html40 >< head><! - [if gte mso 9]>< xml>< x:ExcelWorkbook>< x:ExcelWorksheets>< x:ExcelWorksheet>'+
'< ; X:名称> {工作表}< / X:名称>< X:WorksheetOptions>< X:DisplayGridlines />< / X:WorksheetOptions>< / X:ExcelWorksheet>< / X:ExcelWorksheets> '+
'< / x:ExcelWorkbook>< / xml><![endif] - >< meta http-equiv =content-typecontent = text / plain; charset = UTF-8/>< / head> '+
'< body>< table> {table}< / table>< / body>< / html>'
,base64 = function(s){return window.btoa (unescape(encodeURIComponent(s)))}
,format = function(s,c){return s.replace(/ {(\w +)} / g,function(m,p){return c [ p];})}
返回函数(表,名称,文件名){
if(!table.nodeType)table = document.getElementById(table)
var ctx = {worksheet:name || 'worksheet',table:table.innerHTML}
var a = document.createElement('a');
a.href = uri + base64(格式(模板,ctx));
a.download = filename +'.xls';
a.click();
}
})()

解决方案

这里:如何在ASP.Net中将GridView导出为Word / Excel / PDF / CSV ASP.NET论坛 [ ^ ]

I try to export html table to excel using javascript. The I got this pop up before open the excel file:
if you try open test.xls, is in different format than specified by the extension file

What I have tried:

This is my code:

var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,'
          , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" ' +
            'xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet> ' +
            '<x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets> ' +
            '</x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head> ' +
            '<body><table>{table}</table></body></html>'
            , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
      , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    return function (table, name, filename) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }      
        var a = document.createElement('a');
        a.href = uri + base64(format(template, ctx));
        a.download = filename + '.xls';
        a.click();
    }
})()

解决方案

Here: How to Export GridView To Word/Excel/PDF/CSV in ASP.Net | The ASP.NET Forums[^]


这篇关于将HTML导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆