在网上找到了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" οnclick="tableToExcel('tablename', 'name')" value="Export to Excel">
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]--></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" οnclick="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();

}
    })()

js 导出excel,设置下载的标题相关推荐

  1. 浏览器端JS导出EXCEL

    浏览器端JS导出EXCEL FileSaver.js 实现了在本身不支持 HTML5 W3C saveAs() FileSaver 接口的浏览器支持文件保存.FileSaver.js 在客户端保存文件 ...

  2. js导出excel(带边框以及文本样式)

    js导出excel 假定有这样一个页面 <div class="content-table"><table lay-filter="queryTable ...

  3. 通过javascript获取sharepoint数据,使用JS导出Excel

    通过网页将数据导出到Excel是常见需求,使用服务器端导出,开发周期长,部署麻烦.这里推荐一种客户端导出方法. 客户端导出与Sharepoint无关.这里附带使用场景基于SP,因为SP自带的导出功能比 ...

  4. easypoi导出excel不设置样式_EasyPOI 导出excel设置边框,背景颜色,字体样式

    EasyPOI 导出excel设置边框,背景颜色,字体样式 EasyPOI 导出代码示例ExportParams exportParams = new ExportParams(); exportPa ...

  5. 四种利用js导出Excel的方法(兼容IE6+、主流浏览器、支持复杂表头和合并单元格)

    因为项目需求变更,最后决定使用做JS导出Excel,网上看了很多的帖子和例子,很多的例子并不能满足需求( 处理复杂表头,兼容主流浏览器,兼容IE等等).所以,自己找了几个比较不错的例子,在其基础上结合 ...

  6. 关于IE8浏览器JS导出excel,要使导出列宽度按自己控制。

    关于IE8浏览器JS导出excel,要使导出列宽度按自己控制. function getExplorer() { var explorer = window.navigator.userAgent ; ...

  7. js导出EXCEL js导出EXCEL

    参考一:http://wenku.baidu.com/view/7b81f3eb6294dd88d0d26b57.html 参考二: js导出EXCEL js导出EXCEL <html> ...

  8. POI导出EXCEL设置高度和宽度

    -------------------------------------------------------------------------------SSFRow hssfRow = shee ...

  9. NPOI导出excel设置打印为A4纸张

    NPOI导出excel设置打印为A4纸张: Sheet sheet1 = hssfworkbook.CreateSheet("表(横版)");                 sh ...

最新文章

  1. 字节二面 | 26图揭秘线程安全
  2. Apache服务器配置技巧
  3. android studio 将文件打包成jar文件
  4. Dockerfile多阶段构建
  5. lopa分析_HAZOP : 保护层分析之失效使能分析导则
  6. python学习笔记系列-方便自我学习
  7. python 逻辑回归权重_Python 逻辑回归
  8. c实现三角形角度大于一个值_初中数学三角形知识点小结
  9. 数据3分钟丨Apache Doris谴责DorisDB违背开源精神;HC2021下周召开;openGauss训练营第二期圆满落幕
  10. 三大电信运营商号码段
  11. List常用方法总结
  12. PHP中的数组(拆分与合并)
  13. win10-linux双系统安装教程
  14. nodejs读取服务器json文件,如何使用Node.js读取JSON文件
  15. 均值(average)与期望(mean)
  16. 详解FAT12文件系统
  17. .find()的用法
  18. 长发变短发,卷发变直发,坚持每天梳头
  19. js提示“未结束的字符串常量”
  20. dhcp服务器(dhcp服务器怎么设置)

热门文章

  1. python如何将数据写入excel_使用python将数据写入excel
  2. 短信通知-阿里大鱼 申请AK 发送Until
  3. 公司规定所有接口都用 POST 请求,这是为什么?
  4. 魔众抽奖系统 v2.0.0 上线超级砸金蛋抽奖方式
  5. 计算机硬盘 打开很慢,电脑硬盘运行速度慢如何解决 电脑硬盘运行速度慢解决方法【介绍】...
  6. PureMVC游戏框架解析 理解其中包含的设计模式
  7. 阿里云天池学习赛-零基础入门数据分析-学术前沿趋势分析(task1)
  8. ATFX:道琼斯指数的反弹,11月能否突破35000关口?
  9. Python使用Pillow库进行图像
  10. Python写网络爬虫(三)