DataTables导出到Excel下载zip文件

j.f*_*ong 5 javascript jquery datatables

我正在使用Buttons扩展在jQuery DataTables上实现导出按钮.我将所有按钮都工作,除了导出到Excel按钮.

包括以下所有脚本:

<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/datatables/media/js/jquery.dataTables.js"></script>
<script src="bower_components/datatables/media/js/dataTables.bootstrap.js"></script>
<script src="bower_components/datatables-buttons/js/dataTables.buttons.js"></script>
<script src="bower_components/datatables-buttons/js/buttons.html5.js"></script>
<script src="bower_components/datatables-buttons/js/buttons.print.js"></script>
<script src="bower_components/datatables-buttons/js/buttons.bootstrap.js"></script>
<script src="bower_components/jszip/dist/jszip.js"></script>
<script src="bower_components/pdfmake/build/pdfmake.js"></script>
<script src="bower_components/pdfmake/build/vfs_fonts.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后我创建按钮并将它们附加到div:

// Create and render buttons
new $.fn.dataTable.Buttons( table, {
    buttons: ['copyHtml5', 'csvHtml5', 'excelHtml5', 'pdfHtml5', 'print']
})
table.buttons().container().appendTo($('.header-right'), self);
Run Code Online (Sandbox Code Playgroud)

单击Excel按钮时,我的应用程序会下载xlsx.zip文件.为什么不下载".xlsx"文件?

我也尝试通过手动添加扩展来扩展按钮,但是我设置的扩展属性最终为"extension-name.zip".

new $.fn.dataTable.Buttons( table, {
    buttons: [
        {
            extend: 'excelHtml5',
            extension: '.xlsx'
        }
    ]
 })
Run Code Online (Sandbox Code Playgroud)

小智 8

设置Title属性对我有用.

以下.xlsx文件扩展名导出

 buttons: [{
            extend: 'excelHtml5',
            title: 'Location Report' 
            }
          ],.....etc
Run Code Online (Sandbox Code Playgroud)

未设置标题时,文件以.zip文件扩展名导出

 buttons: [{
            extend: 'excelHtml5',
            title: '' 
            }
          ],.....etc
Run Code Online (Sandbox Code Playgroud)


Abd*_*ari 5

我也有同样的问题....但我认为它与浏览器有关.

  • 在Mozilla Firefox中单击Excel导出时,它会询问您是要打开还是保存文件.当您单击"打开"时,它会以zip格式临时保存文件并打开zip文件(实际上是.xlsx文件).
  • 如果你将扩展名从zip更改为xlsx,那么你就可以了.
  • 单击"保存"时,它将仅保存.xlsx文件.
  • 在Chrome中,它直接将文件保存为.xlsx.