When Export to excel its only export main table data only, I need to export child rows, its not work
When Export to excel its only export main table data only, I need to export child rows, its not work
dwcraj
Posts: 1Questions: 1Answers: 0
{
extend: 'excel',
filename: 'Production_Plan',
exportOptions: {
columns: ':visible'
},
customize: function(xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
var rows = table.rows().data();
var dateGroups = {};
var grandTotal = 0;
// Group data by sewing date
rows.each(function(rowData) {
var sewingDate = rowData[14];
var qty = parseInt(rowData[10]) || 0;
if (sewingDate && sewingDate !== '0000-00-00') {
if (!dateGroups[sewingDate]) {
dateGroups[sewingDate] = {
rows: [],
total: 0
};
}
dateGroups[sewingDate].rows.push(rowData);
dateGroups[sewingDate].total += qty;
grandTotal += qty;
}
});
// Create main sheet with date groups
var $sheetData = $('sheetData', sheet);
var lastRowIndex = $('row', sheet).length;
// Add data with subtotals
Object.keys(dateGroups).sort().forEach(function(date) {
// Add date group rows
dateGroups[date].rows.forEach(function(rowData) {
var $row = $('<row></row>');
for(var i = 0; i < rowData.length; i++) {
$row.append('<c t="inlineStr"><is><t>' + (rowData[i] || '') + '</t></is></c>');
}
$sheetData.append($row);
lastRowIndex++;
});
// Add date subtotal
var $totalRow = $('<row></row>');
$totalRow.append('<c t="inlineStr" s="2"><is><t>Sewing Date: ' + date + ' Total</t></is></c>');
$totalRow.append('<c t="n" s="2"><v>' + dateGroups[date].total + '</v></c>');
for(var i = 2; i < table.columns(':visible').count(); i++) {
$totalRow.append('<c t="inlineStr"><is><t></t></is></c>');
}
$sheetData.append($totalRow);
lastRowIndex++;
});
// Add grand total
var $grandTotalRow = $('<row></row>');
$grandTotalRow.append('<c t="inlineStr" s="2"><is><t>Grand Total</t></is></c>');
$grandTotalRow.append('<c t="n" s="2"><v>' + grandTotal + '</v></c>');
for(var i = 2; i < table.columns(':visible').count(); i++) {
$grandTotalRow.append('<c t="inlineStr"><is><t></t></is></c>');
}
$sheetData.append($grandTotalRow);
// Create line-specific sheets
var lines = ['Line 1', 'Line 2', 'Line 3', 'Line 4', 'Line 5'];
lines.forEach(function(line, index) {
var lineData = rows.filter(function(idx) {
var rowData = table.row(idx).data();
return rowData[18] === line;
});
if (lineData.length > 0) {
var lineSheet = $('<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData></sheetData></worksheet>');
var $lineSheetData = lineSheet.find('sheetData');
// Add headers
var $headerRow = $('<row></row>');
table.columns(':visible').each(function() {
$headerRow.append('<c t="inlineStr"><is><t>' + $(this.header()).text() + '</t></is></c>');
});
$lineSheetData.append($headerRow);
// Group line data by date
var lineDateGroups = {};
var lineTotal = 0;
lineData.each(function(idx) {
var rowData = table.row(idx).data();
var sewingDate = rowData[14];
var qty = parseInt(rowData[10]) || 0;
if (sewingDate && sewingDate !== '0000-00-00') {
if (!lineDateGroups[sewingDate]) {
lineDateGroups[sewingDate] = {
rows: [],
total: 0
};
}
lineDateGroups[sewingDate].rows.push(rowData);
lineDateGroups[sewingDate].total += qty;
lineTotal += qty;
}
});
// Add line data with subtotals
Object.keys(lineDateGroups).sort().forEach(function(date) {
lineDateGroups[date].rows.forEach(function(rowData) {
var $row = $('<row></row>');
for(var i = 0; i < rowData.length; i++) {
$row.append('<c t="inlineStr"><is><t>' + (rowData[i] || '') + '</t></is></c>');
}
$lineSheetData.append($row);
});
// Add date subtotal
var $dateTotalRow = $('<row></row>');
$dateTotalRow.append('<c t="inlineStr" s="2"><is><t>Sewing Date: ' + date + ' Total</t></is></c>');
$dateTotalRow.append('<c t="n" s="2"><v>' + lineDateGroups[date].total + '</v></c>');
$lineSheetData.append($dateTotalRow);
});
// Add line total
var $lineTotalRow = $('<row></row>');
$lineTotalRow.append('<c t="inlineStr" s="2"><is><t>Line Total</t></is></c>');
$lineTotalRow.append('<c t="n" s="2"><v>' + lineTotal + '</v></c>');
$lineSheetData.append($lineTotalRow);
// Add worksheet to workbook
xlsx.xl.worksheets['sheet' + (index + 2) + '.xml'] = lineSheet[0];
}
});
}
},
Answers
The Excel export does not currently support child row data I'm afraid. That is something that I hope to add in future.
Your custom function might be adding it, I'm afraid I don't have the time to debug it for you at the moment.
Allan
See if this example from this thread helps.
Kevin