问题描述
大家好,
我尝试在我的js中使用btob将html的表格导出为excel。
i在我的js中添加了这段代码
base64 = function(s){return window.btob(unescape(encodeURIComponent(s)))}
但是对于这段代码,它仅适用于Firefox,Chrome并且它不适用于IE 。
当我调试IE时,它会显示如下错误:对象不支持属性或方法'btob'
我发现这个方法btob是for firefox,chrome只能用。
所以有人知道怎么做或者替换这个方法让IE,Chrome,Firefox工作吗?
谢谢
TONY
Hi all experts,
I try to use btob in my js to export html's table to excel.
i added this code in my js
base64 = function (s) { return window.btob(unescape(encodeURIComponent(s))) }
but for this code, it works only Firefox,Chrome and it doesn't work with IE.
When i debug IE, it displays me an error like this: Object doesn't support property or method 'btob'
I found that this method "btob" is for firefox, chrome to use only.
so does anybody know that how can do or replace this method to let IE,Chrome,Firefox work?
Thanks
TONY
推荐答案
<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 (!t
DEMO
[]
欲了解更多信息,请查看:
[]
更新
使用jQuery将表格数据导出到Excel
[]
注意:如果你需要工作IE上面的代码然后,
DEMO
http://jsfiddle.net/insin/cmewv/[^]
For more info check this :
http://stackoverflow.com/questions/14125819/export-html-table-to-excel-but-select-a-name-for-the-file[^]
UPDATE
Export table data to Excel using jQuery
http://jquerybyexample.blogspot.com/2012/10/export-table-data-to-excel-using-jquery.html[^]
NOTE: If you need to work above code on IE then,
你需要设置响应头。如果你正在使用ASP.NET那么
Response.AddHeader(Content-Disposition,attachment; filename = download.xls);
you need to set response header. If you are using ASP.NET then
Response.AddHeader("Content-Disposition", "attachment;filename=download.xls");
我希望这会对你有所帮助。
I hope this will help to you.
这篇关于对象不支持属性或方法'btob'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!