简体   繁体   中英

DataTables Export to Excel downloading a zip file

I am implementing export buttons on jQuery DataTables using the Buttons extension. I have all the buttons working except for the export to Excel button.

All the below scripts are included:

<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>

Then I create the buttons and append them to a div:

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

When clicking on the Excel button, my app downloads a xlsx.zip file. Why is it not downloading a ".xlsx" file?

I've also tried to extend the button by manually adding the extension but whatever I set to the extension property ends up as "extension-name.zip".

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

Setting the Title property worked for me.

The following exported with the .xlsx file extension

 buttons: [{
            extend: 'excelHtml5',
            title: 'Location Report' 
            }
          ],.....etc

When the title was not set, the file exported with the .zip file extension

 buttons: [{
            extend: 'excelHtml5',
            title: '' 
            }
          ],.....etc

I also had same problem....but i think it is related to browser.

  • In Mozilla Firefox when you click on Excel export, it asks whether you want to Open or Save the file. When you click on Open, it saves files temporarily in zip format and opens the zip file(Which is actually .xlsx file).
  • If you change the extension from zip to xlsx then you are good to go.
  • Where as when you click Save, it will save .xlsx file only.
  • And in Chrome, it directly saves the file as .xlsx only.

I also had same issue. Working perfectly with the chrome but not with the Mozila Firefox.....

Finally after so much efforts and research. i found the solution.

Please include below file in your script after datatable js file. //cdn.datatables.net/buttons/1.1.2/js/buttons.html5.min.js

It's working with me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM