如何将html表数据导出为xlsx格式 [英] How to export html table data to xlsx format

查看:80
本文介绍了如何将html表数据导出为xlsx格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个asp.net网站...



我在哪里我将html数据导出为ex​​cel(Jquery代码),但是采用xls格式。



Jquery代码

Hi guys,

I have an asp.net website...

Where i am exporting html data to excel(Jquery Code) but in xls format.

Jquery Code

<script type="text/javascript">
     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]--></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) {
             if (!table.nodeType) table = document.getElementById(table)
             var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
             window.location.href = uri + base64(format(template, ctx))

         }
     })()
 </script>





以上代码可以顺利运行xls格式,但我正在寻找xlsx格式...



任何人都可以帮助我。





谢谢



The above code is smoothly working for xls format but i'm looking for xlsx format...

Can any one please help me.


Thanks

推荐答案

首先,尝试导出不是HTML,而是用于创建HTML的数据。您正在使用ASP.NET,这意味着您拥有数据并使用它来填充表格,因此最好使用这些数据。



要写xlsx,我建议使用Microsoft Open XML SDK。例如,请参阅此CodeProject文章:

使用Open XML创建基本的Excel工作簿 [ ^ ]。



注意:Microsoft解释了为什么不鼓励在服务器设置中使用Excel互操作:

http://support.microsoft.com/default.aspx? scid = kb; EN-US; q257757#kb2 [ ^ ],

http: //support.microsoft.com/kb/257757/en-us [ ^ ]。



另见我过去的答案本文中引用的参考文献:如何从MS Visual Studio 2010中的添加参考添加microsoft excel 15.0对象库 [ ^ ]。



-SA
First of all, try to export not HTML, but the data used to create HTML. You are using ASP.NET, it means that you have the data and use it to fill in the table, so better use this data.

To write xlsx, I would recommend to use Microsoft Open XML SDK. For example, please see this CodeProject article:
Creating basic Excel workbook with Open XML[^].

Pay attention: Microsoft explains why using Excel interop in server setting is discouraged:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

See also my past answers referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA


这篇关于如何将html表数据导出为xlsx格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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