Multiple DataTable Export in to Multiple worksheet
Multiple DataTable Export in to Multiple worksheet

How to export multiple datatables in to multiple worksheets in single Excel workbook on single click using Datatable export buttons. Please help me.
This discussion has been closed.
Answers
You would need to use the
customize
callback of theexcelHtml5
button type to modify the XLSX XML that Buttons creates. Buttons does not create multiple worksheets out of the box.Allan
I tried tweaking the buttons.html5.js successfully to export multiple datatables in to multiple worksheets in single Excel workbook on single click using Datatable export buttons. Before posting the code here, I would like to have it reviewed by Allan to see if it is correct and if yes, can it be included in the future releases for the benefit of the community
Hi,
Paste it in here, and I'll be happy to take a look
Allan
/*I've tested exporting 6 datatables to 6 separate worksheets in a single workbook.My Buttons config has an extra option 'multitables' specified for the solution to work which basically has TableId:Sheetname and looks like this:
extend: 'excelHtml5',
multitables:{PL:'Profit',CL:'Liabilities',BS:'Assets',FF:'FundFlow',WC:'WkCap',FP:'FinPos'},
Since there seems to be a limitation on the characters allowed in this message unable to paste the entire contents of file buttons.html5.js, I am pasting the entire class DataTable.ext.buttons.excelHtml5 where the changes have been made. Also changed Relationship Id="rId0" instead of rId2 in excelStrings array which is not pasted here due to limitation on pasting max chars here
*/
//====Start of code pasted from buttons.html5.js===
DataTable.ext.buttons.excelHtml5 = {
className: 'buttons-excel buttons-html5',
};
====End of code pasted from buttons.html5.js===
//The "xl/workbook.xml": of the excelStrings object has:
//===
'<sheets>'+
'<sheet name="sheet1" sheetId="1" r:id="rId1"/>'+
'</sheets>'+
//===
And also, the "xl/_rels/workbook.xml.rels":
'<Relationship Id="rId0" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'+
@mukeshp hello. how does this work? I copied your modified code and followed your instructions but I still don't know how to produce multiple worksheets in one .xlsx file.
Did you copy the new buttons.html5.js with the excelStrings changes?
Do you have your code or a sample fiddle to analyse the issue you are facing?
@mukeshp Howdy. Before I begin, I'd like to state that I am a beginner JS programmer. Only recently am I becoming aware of the more advanced programming aspects of Datatables. Please bare with me.
I found this piece of code recently and had a couple of questions. I have implemented the DataTable.ext...into buttons.html5.js but I get an error. JS says Datatable is undefined. My first thought was to define Datatable the same way 'p' is defined. In buttons.html5.js, p = e.fn.datatables. However, this also produces an error, stating that jsZip is not defined. I wondered how this error could be corrected?
Second question deals with the setup on the button. Below is the code I currently have and would like to make sure that I set it up correctly per your instructions:
var table = $('#htmlTable').DataTable({
Thank you for your time.
Updated file based on @mukeshp code
This is how I got this working, in case anyone is having difficulties:
Include the version of buttons.html5.js given by vladslav in the above post in your site (following the main datatables.js file)
Initialise the table extending excelHtml5 to include the multitables option. The multitables option should include an array of the tables you want to export, with the table ID as the key and the value the sheet name as you want it to appear in the Excel export file. eg. here I am exporting tables with ID journalTable, bookTable and databaseTable, which will be exported into worksheets named 'Journals', 'Books' and 'Databases' respectively:
Hope that helps
Hi,
I'm trying your suggest @jt_san I got this error
this.processing is not a function.
Hi @jt_san ,
I tried the way you have told but I am not able to get it done correctly. Now I can export a empty excel file with errors to it.
The errors are as follows:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error160000_01.xml</logFileName>
<summary>Errors were detected in file 'C:\Users\VFC92816\Downloads\download.xlsx'</summary>
-<removedRecords>
<removedRecord>Removed Records: Worksheet properties from /xl/workbook.xml part (Workbook)</removedRecord>
</removedRecords>
-<repairedRecords>
<repairedRecord>Repaired Records: Worksheet properties from /xl/workbook.xml part (Workbook)</repairedRecord>
</repairedRecords>
</recoveryLog>
Hi,
we have a requirement to download multiple sheets in single Excel file. I tried adding above code
buttons: [
{ extend: 'excelHtml5', multitables:{journalTable:'Journals',bookTable:'Books',databaseTable:'Databases'} }
], But each table data is getting downloaded as each individual excel file.
I want all tables data in single excel file, under different sheet
Kevin did an example of that on this thread: https://datatables.net/forums/discussion/58998/excel-export-create-new-sheet-example#latest
Colin