jquery 关于:在Excel中导出表格时出现blank#blocked错误

ivqmmu1c  于 2022-11-03  发布在  jQuery
关注(0)|答案(2)|浏览(592)

Javascript代码表到excel数据

<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]--><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,action) {

        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
        window.location.href = uri + base64(format(template, ctx))
    }
})();
</script>

操作按钮

<input type="button" class="btn btn-success" value="Export" onclick="tableToExcel('tblConsolidate','Report','Fifth Batch')" />

表设计

<table class="table table-bordered" id="tblConsolidate">
<thead>
 <tr>
  <th>
   Head 1
 </th>
 <th>
   Head 2
 </th>
 </tr>
</thead>
<tbody style="text-align:center;">
  <tr>
  <td>
    body 1
 </td>
   <td>
    body 2
 </td>
 </tr>
 </tbody>

问题

当上述***表导出到Excel时***功能适用于小数据,但当*行数超过800行,列数接近20或更多时*则显示关于:blank#blocked错误,导出失败
问题是什么,为什么这个被阻塞了,我的代码应该做什么改变?

kzipqqlq

kzipqqlq1#

我有同样的问题,我解决了它保存为一个斑点。
我相信这个问题是由于达到了数据URL的数据限制(chrome上的2 MB?),通过将其存储为blob会大大增加您的数据限制(2千兆字节?)。
这个回答很有帮助:[1][2][3][4]

var tableToExcel = (function () {

        var myBlob =  new Blob( [table.innerHTML] , {type:'application/vnd.ms-excel'});
        var url = window.URL.createObjectURL(myBlob);
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.href = url;
        a.download = "export.xls";
        a.click();
      //adding some delay in removing the dynamically created link solved the problem in FireFox
        setTimeout(function() {window.URL.revokeObjectURL(url);},0);

});
//and call the function:
tableToExcel();
ssgvzors

ssgvzors2#

"我用这个代码解决了这个问题"

var tableToExcel = (function () {
  var encabezado = '<html><head><meta http-equiv="content-type" 
  content="text/plain; charset=UTF-8"/><style> table, td {border:thin solid 
  black} table {border-collapse:collapse}</style></head><body><table>';

  var dataTable = table.innerHTML
  var piePagina = "</table></body></html>";
  var tabla = encabezado + dataTable + piePagina;
  var myBlob =  new Blob( [tabla] , {type:'text/html'});
  var url = window.URL.createObjectURL(myBlob);
  var a = document.createElement("a");
  document.body.appendChild(a);
  a.href = url;
  a.download = "export.xls";
  a.click();

  setTimeout(function() {window.URL.revokeObjectURL(url);},0);
});

相关问题