js 导出excel,设置下载的标题
来源:互联网 发布:licecap for mac 编辑:程序博客网 时间:2024/06/05 02:01
在网上找到了js导出为excel的方法,可就是找不到如何修改导出的excel标题的方式,找到了如下的网站: 参考国外的网站:http://stackoverflow.com/questions/17126453/html-table-to-excel-JavaScript.他是利用a标签,可以设置download属性,设置下载的文件标题,这里是a标签的详细解释http://www.runoob.com/tags/att-a-download.html: 二 英文的原文和大概翻译如下:I'm trying to use this script to save a html table to an Excel file, and it works fine, however it doesn't come up in the proper name, but rather with a random string. And I can't see why .I call it with:<input type="button" onclick="tableToExcel('tablename', 'name')" value="Export to Excel">codevar 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))}})()答案如下:意思是:可以使用a标签的download属性来设置下载的文件标题:You can use download attribute supported by modern browsera for a anchor element. First modify your HTML by adding an invisible anchor://这里是在你点击导出的按钮上方增加一个隐藏的a标签,只是为了更改标题内容<a id="dlink" style="display:none;"></a><input type="button" onclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel">Notice also that the call to function tableToExcel now has 3rd parameter - where you specify file name.Now use this modified code of your original function://这里是将html内容转换成excel的方法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, filename) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } document.getElementById("dlink").href = uri + base64(format(template, ctx)); document.getElementById("dlink").download = filename;//这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了 document.getElementById("dlink").click(); } })()
0 0
- js 导出excel,设置下载的标题
- js 导出excel,设置下载的标题
- 关于js导出excel的标题日期问题
- excel的下载导出
- js 使用前端导出excel,设置标题和页脚,实现特殊表格需求
- excel导出插件失败解决(原标题JS new ActiveXObject("Excel.Application")产生的问题)
- js导出excel到客户端下载
- Excel的导出与下载
- nodejs Excel的下载导出
- excel的导出和下载
- 导出Excel--处理标题
- js导出excel的实现
- JS导出Excel时设置Excel格式为字符串格式
- 简单的导出EXCEL并下载
- poi的导出excel(直接下载)
- C# Excel 的导入 导出 下载
- dbutils的导出下载功能(excel)
- jxl导出excel标题乱码
- ORCLE 知识总结
- LeetCode-35. Search Insert Position
- 优化MySchool 数据库设计 第二章 上机
- 死后的灵魂,如何看待生前的人生
- 如何快速转载CSDN中的博客
- js 导出excel,设置下载的标题
- Linux Socket套接字
- mysql忘记密码修改方法
- 如何搭建局域网共享文件
- C++:STL标准入门汇总
- 整理图书
- deepLink iOS 应用到自己APP 记录
- Cohen-Sutherland直线段的裁剪算法
- Linux网络配置及SecureCRT终端的使用