引用 ExcelJS
let masterGrid = $("#gridContainer").dxDataGrid("instance");
// Get the selected master rows
let selectedMasterRows = masterGrid.getSelectedRowsData();
if (selectedMasterRows.length == 0) {
selectedMasterRows = masterGrid.option('dataSource');
}
//console.log(selectedMasterRows);
let filterSubData = [];
// Iterate over each selected master row
$.each(selectedMasterRows, function (index, masterRow) {
let matchDetail = fullSubData.filter(obj => obj['Item'] === masterRow.Item);
filterSubData.push(masterRow);
filterSubData.push(...matchDetail);
});
//console.log(filterSubData);
const workbook = new ExcelJS.Workbook();
// Add a worksheet
const worksheet = workbook.addWorksheet('@ViewBag.Title');
let excelcols = [];
const propertyList = Object.keys(filterSubData[0]);
for (let i = 0; i < propertyList.length; i++) {
excelcols.push(
{
header: propertyList[i],
key: propertyList[i],
}
);
}
worksheet.columns = excelcols;
for (let i = 0; i < filterSubData.length; i++) {
worksheet.addRow(filterSubData[i]);
if (filterSubData[i].idx) {
for (col of worksheet.columns) {
const cell = worksheet.getCell(col.letter + (i + 2));
cell.font = { bold: true }
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'D9FC04' }
};
}
}
}
workbook.xlsx.writeBuffer().then((content) => {
const link = document.createElement("a");
const blobData = new Blob([content], {
type: "application/vnd.ms-excel;charset=utf-8;"
});
link.download = '@ViewBag.Title' + moment().format('_YYYYMMDD_HHmmss') + '.xlsx';
link.href = URL.createObjectURL(blobData);
link.click();
});
标签:const,filterSubData,excel,propertyList,导出,js,let,worksheet,selectedMasterRows
From: https://www.cnblogs.com/duixue/p/18211085