我有一些问题与Exceljs生成的xlsx文件.我已经在我的项目中创建了csv文件一段时间没有任何问题.但是现在我正在创建一个xlsx文件,虽然我可以在Ubuntu/LibreOffice Calc上打开它干净,但在Excel上try 做同样的操作时出现了一个错误(在Windows和IOS上都一样).
我的文件名为"export—yyyy—mm—dd.xlsx",其中有两个表分别名为"Total"和"Details".
这两个表在列标题上方都有一个"假"标题行.第二个文件包含13列字符串或数字数据,用于所有用户,而第一个工作表将这些数据聚合为6列数字,标题下只有一行.
这是第一个工作表一行数据是丢失时使用Excel打开文件.
到目前为止我try 的是:
- 将纯数值从字符串格式移动到数字格式
- 将一个百分比的单元格从字符串格式移动到数字格式
- 更新我的Exceljs软件包(从3.9到4.4)
- 在创建时删除图纸设置
- 从两张纸上删除"假"标题,
- go 掉第一张纸
- go 掉第二张纸
我现在没有新的 idea 了,所以:是什么导致了这个问题?
下面是生成第一个工作表的代码的简化版本:
const file = new Workbook();
function createFirstSheetHeaders(): Partial<Column>[] {
return [
{
key: "usersCount",
width: 50,
},
{
key: "ratio",
width: 40,
},
{
key: "firstCount",
width: 40,
},
{
key: "total",
width: 40,
},
{
key: "secondCount",
width: 40,
},
{
key: "saved",
width: 50,
},
];
}
function firstSheetHeadersContent(): {
usersCount: string;
ratio: string;
firstCount: string;
total: string;
secondCount: string;
saved: string;
} {
return {
usersCount: "Users count",
ratio: "Ratio",
firstCount: "First count",
total: "Total",
secondCount: "Second count",
saved: "Saved",
};
}
const header = "Export from 04-03-2024 for the period from 01-01-2024 to 04-07-2024";
const firstSheet = file.addWorksheet("Total", {
pageSetup: { orientation: "landscape" },
});
firstSheet.columns = createFirstSheetHeaders();
firstSheet.mergeCells("A1:F1");
firstSheet.getCell("A1").value = header;
firstSheet.addRow(firstSheetHeadersContent(t));
function formatTotalDataForRow(
usersCountInput: number,
ratioInput: number,
dailyTotal: number,
firstCount: number,
secondCount: number
): {
usersCount: number;
ratio: number;
firstCount: number;
total: number;
secondCount: number;
saved: number;
} {
const parsedTransportEmissionRatio = Number(ratioInput.toFixed(4));
const total = dailyTotal * firstCount;
const saved = dailyTotal * secondCount;
return {
usersCount: usersCountInput,
ratio: parsedTransportEmissionRatio,
firstCount: firstCount,
total: total,
secondCount: secondCount,
saved: saved,
};
const totalData = formatTotalDataForRow(
usersCount,
ratioInput,
dailyTotal,
firstCount,
secondCount
);
firstSheet.addRow(totalData);
firstSheet.getCell("B3").numFmt = "0.00%";
}
编辑:如果我只包括两个表中的一个,它的工作就很好,无论是第一个还是第二个表.因此,问题可能与此表的创建方式/其内容无关.
由于数据的性质和它的潜在巨大的大小,我生成我的文件如下:
- 我创建文件/工作簿
- 我创建了两个工作表,它们的标题和列的标题
- 我循环我的分块数据来填充我的第二个表,一次有200个用户.当我这样做的时候,在增加我的总数用于第一张表.我还在我的文件缓冲区写入
- 我用之前创建的总数来填充我的第一张表.我在我的文件缓冲区写
- 我结束我的穿越
以下是我在第一张纸被填满时所做的:
const firstSheetBuffer = await firstSheet.workbook.xlsx.writeBuffer();
if (Buffer.isBuffer(firstSheetBuffer)) {
passThrough.write(firstSheetBuffer);
} else {
const err = new Error("ExcelJs does not return a Buffer");
passThrough.destroy(err);
throw err;
}
passThrough.end();
return file;