在将 header 选项与 XLSX.utils.json_to_sheet 一起使用时,header 不会覆盖
while using header option with XLSX.utils.json_to_sheet , headers not overriding
虽然我尝试通过将标题数组传递给如下选项来更改 header 标题,但它不会覆盖 header。相反,它首先从下一个单元格写入新的 headers 和旧的 headers 的原始数据。我传递了相同数量的 header 个标题。
这是我的代码
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json, {header: headerColumns});
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Transactions');
const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
this.saveAsExcelFile(excelBuffer, excelFileName);
输出如下所示。
“header”选项的基本工作不是覆盖,而只是移动列的起始选项。
即在 header 选项中传递的任何值都将被视为第一列,前提是该值应与数据中的现有键匹配。
XLSX.utils.json_to_sheet([{A:1,B:2},{B:2,C:3}], {header:['C']})
此处“C”列将是 excel 中的第一列。
如需更多信息,请在此处查看详细说明:https://docs.sheetjs.com/#sheetjs-js-xlsx
这就是我实现类似行为的方式。
const XLSX = require('xlsx');
const wb = XLSX.utils.book_new();
const Heading = [
['Sr No', 'User Name', 'Department', 'Bank', 'Country', 'Region', 'Amount']
];
// creating sheet and adding data from 2nd row of column A.
// Leaving first row to add Heading
const ws = XLSX.utils.json_to_sheet(data, { origin: 'A2', skipHeader: true });
// adding heading to the first row of the created sheet.
// sheet already have contents from above statement.
XLSX.utils.sheet_add_aoa(ws, Heading, { origin: 'A1' });
// appending sheet with a name
XLSX.utils.book_append_sheet(wb, ws, 'Records');
const fileContent = XLSX.write(wb, { bookType: 'xlsx', type: 'buffer' });
very traditional approach but working, please see complete code below :
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.releaseDateWiseCountList);
worksheet.A1.v = "Pick Release Date";
worksheet.B1.v = "Task Type";
worksheet.C1.v = "First Shift";
worksheet.D1.v = "Second Shift";
worksheet.E1.v = "Total";
worksheet.F1.v = "Grand Total";
worksheet.G1.v = "Pick %";
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const data: Blob = new Blob([buffer], {type: EXCEL_TYPE});
FileSaver.saveAs(data , 'Result_export_' + new Date().getTime() + EXCEL_EXTENSION);
虽然我尝试通过将标题数组传递给如下选项来更改 header 标题,但它不会覆盖 header。相反,它首先从下一个单元格写入新的 headers 和旧的 headers 的原始数据。我传递了相同数量的 header 个标题。
这是我的代码
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json, {header: headerColumns});
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Transactions');
const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
this.saveAsExcelFile(excelBuffer, excelFileName);
输出如下所示。
“header”选项的基本工作不是覆盖,而只是移动列的起始选项。 即在 header 选项中传递的任何值都将被视为第一列,前提是该值应与数据中的现有键匹配。
XLSX.utils.json_to_sheet([{A:1,B:2},{B:2,C:3}], {header:['C']})
此处“C”列将是 excel 中的第一列。 如需更多信息,请在此处查看详细说明:https://docs.sheetjs.com/#sheetjs-js-xlsx
这就是我实现类似行为的方式。
const XLSX = require('xlsx');
const wb = XLSX.utils.book_new();
const Heading = [
['Sr No', 'User Name', 'Department', 'Bank', 'Country', 'Region', 'Amount']
];
// creating sheet and adding data from 2nd row of column A.
// Leaving first row to add Heading
const ws = XLSX.utils.json_to_sheet(data, { origin: 'A2', skipHeader: true });
// adding heading to the first row of the created sheet.
// sheet already have contents from above statement.
XLSX.utils.sheet_add_aoa(ws, Heading, { origin: 'A1' });
// appending sheet with a name
XLSX.utils.book_append_sheet(wb, ws, 'Records');
const fileContent = XLSX.write(wb, { bookType: 'xlsx', type: 'buffer' });
very traditional approach but working, please see complete code below :
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(this.releaseDateWiseCountList);
worksheet.A1.v = "Pick Release Date";
worksheet.B1.v = "Task Type";
worksheet.C1.v = "First Shift";
worksheet.D1.v = "Second Shift";
worksheet.E1.v = "Total";
worksheet.F1.v = "Grand Total";
worksheet.G1.v = "Pick %";
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const data: Blob = new Blob([buffer], {type: EXCEL_TYPE});
FileSaver.saveAs(data , 'Result_export_' + new Date().getTime() + EXCEL_EXTENSION);