Hello! Loving everything Syncfusion.
I've got one page with a single treegrid and another with up to 4 that I need to export to excel.
I believe I understand how to get the page with 4 to export each grid to a different sheet in the excel workbook.
My issue is that I'd like to set the sheet NAMES for each (rather than getting the default sheet1, sheet2, etc.).
Is there a way to do so?
Also: just as a little feedback on the issue, it would be great if we could just specify the sheet name as a property somewhere in the grid setup, or just have it use the name of the HTML element if nothing else.
Hi John,
Greetings from Syncfusion support.
Query : My issue is that I'd like to set the sheet NAMES for each (rather than getting the default sheet1, sheet2, etc.).
To set the name for multiple sheets, we need to append the names of the individual worksheet in workbook instance. Please refer to the following code snippet for better understanding.
|
function toolbarClick(args) { var exportData; var names = ["First", "Second", "Third", "Forth", "Fifth"]; var instances = [ document.getElementById("TreeGrid1").ej2_instances[0], document.getElementById("TreeGrid2").ej2_instances[0], document.getElementById("TreeGrid3").ej2_instances[0], document.getElementById("TreeGrid4").ej2_instances[0], document.getElementById("TreeGrid5").ej2_instances[0], ] if (args['item'].text === 'Excel Export') { var appendExcelExportProperties = { multipleExport: { type: 'NewSheet' } }; instances[0].grid.excelExport(appendExcelExportProperties, true).then((a) => { a.worksheets[0].name = names[0]; exportData = a; for (var j = 1; j < instances.length - 1; j++) { instances[j].excelExport(appendExcelExportProperties, true, exportData).then((b) => { exportData = b; if (exportData.worksheets.length === (instances.length - 1)) { for (var k = 0; k < exportData.worksheets.length; k++) { if (!exportData.worksheets[k].name) { exportData.worksheets[k].name = names[k]; } } } }) } var lastGridExport = instances[instances.length - 1].excelExport(appendExcelExportProperties, true, exportData); lastGridExport.then((wb) => { wb.worksheets[wb.worksheets.length - 1].name = names[instances.length - 1]; const book = new ej.excelexport.Workbook(wb, 'xlsx'); book.save('Export.xlsx'); }) }) } } |
Image :
We have provided the sample in treegrid and you can use the same code snippet for grid too. We have attached the sample for your reference.
Kindly get back to us for further assistance.
Regards,
Shek
Thank you so much for the help!
I've implemented your code exactly (except for using my own sheet names and specifying the IDs of my treegrid instances), but I've run into a strange problem.
I actually have 7 different grids loaded in tabs using a Tab control. They display perfectly on screen. I've verified that the instances variable is set correctly with each of the 7 individual grids.
The first two tree grids append perfectly to the export file, but the rest of the grids end up duplicating bits and pieces of the 2nd grid over and over.
I've tried removing some of the names/instances from the lists to see if one was causing a particular problem, but with any combination, every sheet/export starting from the third instance is garbled in some way.
I realize this is fairly vague, but the data is sensitive, so I'd have trouble getting a sample put together that I can share. I'll try if I need to.
I've also verified that if I put ALL of the data into ONE giant tree grid and just run a standard export, it works just fine. No issues at all.
Any thoughts or guidance?
I've managed to get it working by modifying an example from another forum post. The only issues now is it's ugly. I've refactored this into a recursive function to remove the nesting, but here's the "raw code" that I did get working.
I'll see if I can isolate what IS working in this example but ISN'T in the original, but it's at least doing the job now.
instances[0].excelExport(appendExcelExportProperties, true).then((wb1) => {
wb1.worksheets[0].name = names[0];
instances[1].excelExport(appendExcelExportProperties, true, wb1).then((wb2) => {
wb2.worksheets[1].name = names[1];
instances[2].excelExport(appendExcelExportProperties, true, wb2).then((wb3) => {
wb3.worksheets[2].name = names[2];
instances[3].excelExport(appendExcelExportProperties, true, wb3).then((wb4) => {
wb4.worksheets[3].name = names[3];
instances[4].excelExport(appendExcelExportProperties, true, wb4).then((wb5) => {
wb5.worksheets[4].name = names[4];
instances[5].excelExport(appendExcelExportProperties, true, wb5).then((wb6) => {
wb6.worksheets[5].name = names[5];
instances[6].excelExport(appendExcelExportProperties, true, wb6).then((wb7) => {
wb7.worksheets[6].name = names[6];
const book = new ej.excelexport.Workbook(wb7, 'xlsx');
book.save('Export-TransactionAnalysis.xlsx');
});
});
});
});
});
});
});
Hi John,
Thanks for your update. We are glad to hear that the issue has been resolved. Please get back to us for further assistance.
Regards,
Shek