一个包含两个工作表的Excel文件(TAB1,TAB2)。
如何将下面的Excel中的“TAB1”另存为一个名为tab1.xlsx的新Excel文件?
我正在尝试使用exceljs解决此问题,但不确定如何将其保存为新文件。如果无法使用exceljs完成此操作,请建议其他方法。
const ExcelJS = require('exceljs');
const wb = new ExcelJS.Workbook();
//const newwork = new ExcelJS.Workbook();
filename = "abc.xlsx";
const fl = async () => {
wb.xlsx.readFile(filename)
.then(async function () {
wb.eachSheet(async function (worksheet, sheetId) {
console.log("worksheet", worksheet, "sheetid", sheetId);
if(worksheet.name.toLocaleLowerCase() ==='tab1'){
var data = wb.getWorksheet(worksheet.name);
// how to save worksheet as tab1.xlsx
// let dt =await newwork.xlsx.writeFile('tab1.xlsx',data);
}
});
});
}
fl();
var newWorkBook = new ExcelJS.Workbook();
2. 创建新工作表var newWorkSheet = newWorkBook.addWorksheet(worksheet.name);
3. 对于每一行,将原始工作表中的值设置到新工作表中:worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) { newWorkSheet.getRow(rowNumber).values = row.values; });
4. 现在你就可以保存工作簿了await newWorkBook.xlsx.writeFile(worksheet.name + '.xlsx');
- Artúr Manó MarschalnewWorkbook.worksheets[0] = worksheet;
- Artúr Manó Marschal