如何使用 NestJS 填充 xlsx 文件,同时保持原始风格?

问题描述 投票:0回答:1

我正在开发一个 API,它接收两个日期,一个用作开始日期,另一个用作结束日期。收到它们后,我过滤数据库以获取两个日期之间的信息,然后,我需要用之前检索到的信息填充 Excel 文件。

问题是,当我保存包含信息的 Excel 文件时,所有样式都消失了。原始文件位于项目文件夹内,它对单元格有一些配置(字体大小、背景颜色等)。

我使用的是npm xlsx包,使用saveFile在本地保存新的xlsx后,这个新文件的单元格都是白色背景颜色。

我尝试手动设置单元格样式,但没有成功,所有单元格都保持白色背景和相同的字体。我还尝试创建一个干净的工作簿并复制原始工作簿信息,但它也没有改变任何内容。

有谁知道如何在保留所有原始样式的情况下填充xlsx文件?

我正在运行的代码示例:

 fillSheet(documents: Documents[]) {
        const wb = readFile("./src/files/payment_workbook.xlsx", { cellStyles: true });
        const sheet = wb.Sheets["sheet_1"];

        const todayDate = new Date().toLocaleDateString("pt-BR", {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric',
        });

        let row_index = 6;
        for (const document of documents) {
            const bankInfo: BankInfoDto = document.bank_info;
            const bank_name = bankInfo.bank_name;
            const member_account = bankInfo.account_number;

            const document_creation_date = new Date(document.request_date);
            const creation_date = document_creation_date.toLocaleDateString("pt-BR",
                {
                    year: 'numeric',
                    month: 'numeric',
                    day: 'numeric',
                });

            const due_and_pay_date = new Date(document_creation_date);
            due_and_pay_date.setDate(due_and_pay_date.getDate() + 30);
            const due_and_pay_date_formatted = due_and_pay_date.toLocaleDateString("pt-BR",
                {
                    year: 'numeric',
                    month: 'numeric',
                    day: 'numeric',
                });

            const observation = document.id;
            const bank_code = bankInfo.bank_code;
            const account_agency = bankInfo.agency;
            const member_document = bankInfo.document;
            const category = "Category 1";
            const total_value = document.total_value;

            utils.sheet_add_aoa(sheet,
                [
                    [
                        observation,                //cell B
                        member_document,            //cell C
                        category,                   //cell D
                        bank_name,                  //cell E
                        total_value,                //cell F
                        ,                           //cell G
                        ,                           //cell H
                        creation_date,              //cell I
                        todayDate,                  //cell J
                        due_and_pay_date_formatted, //cell K
                        ,                           //cell L
                        ,                           //cell M
                        ,                           //cell N
                        ,                           //cell O
                        ,                           //cell P
                        ,                           //cell Q
                        ,                           //cell R
                        observation,                //cell S
                        ,                           //cell T
                        observation,                //cell U
                        ,                           //cell V
                        ,                           //cell W
                        ,                           //cell X
                        observation,                //cell Y
                        ,                           //cell Z
                        "Bank Transfer",            //cell AA
                        ,                           //cell AB
                        ,                           //cell AC
                        ,                           //cell AD
                        bank_code,                  //cell AE
                        account_agency,             //cell AF
                        member_account,             //cell AG
                        member_document,            //cell AH
                        ,                           //cell AI
                        ,                           //cell AJ
                    ]
                ],
                { origin: `B${row_index}`, cellStyles: true }
            );
            row_index++;
        }

        writeFile(wb, `filled_file.xlsx`, { cellStyles: true });
   
node.js excel http nestjs xlsx
1个回答
0
投票

当您修改工作表数据并将其保存回来时,xlsx包(在代码中使用)不会自动保留单元格格式(例如字体大小,颜色等)

常量{ 读取文件, 实用程序, 写文件 } = 要求('xlsx');

函数 fillSheet(文档) { const wb = readFile("./src/files/ payment_workbook.xlsx", { 单元格样式:true }); const Sheet = wb.Sheets["sheet_1"]; 常量引用行 = 1; const 引用样式 = {}; const ReferenceRowRange = utils.decode_range(sheet["!ref"]); for (让 col = referenceRowRange.s.c; col <= referenceRowRange.e.c; col++) { const cellAddress = utils.encode_cell({ r: referenceRow - 1, c: col }); const cell = sheet[cellAddress]; if (cell && cell.s) { referenceStyles[cellAddress] = cell.s; } }

const todayDate = new Date().toLocaleDateString("pt-BR", {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
});

let row_index = 6;

for (const document of documents) {
    const bankInfo = document.bank_info;
    const bank_name = bankInfo.bank_name;
    const member_account = bankInfo.account_number;
    const document_creation_date = new Date(document.request_date);
    const creation_date = document_creation_date.toLocaleDateString("pt-BR", {
        year: 'numeric',
        month: 'numeric',
        day: 'numeric',
    });

    const due_and_pay_date = new Date(document_creation_date);
    due_and_pay_date.setDate(due_and_pay_date.getDate() + 30);
    const due_and_pay_date_formatted = due_and_pay_date.toLocaleDateString("pt-BR", {
        year: 'numeric',
        month: 'numeric',
        day: 'numeric',
    });

    const observation = document.id;
    const bank_code = bankInfo.bank_code;
    const account_agency = bankInfo.agency;
    const member_document = bankInfo.document;
    const category = "Category 1";
    const total_value = document.total_value;



    utils.sheet_add_aoa(sheet, [rowData], {
        origin: `B${row_index}`,
        cellStyles: true
    });


    for (let col = referenceRowRange.s.c; col <= referenceRowRange.e.c; col++) {
        const cellAddress = utils.encode_cell({
            r: row_index - 1,
            c: col
        });
        if (referenceStyles[cellAddress]) {
            sheet[cellAddress] = sheet[cellAddress] || {};
            sheet[cellAddress].s = referenceStyles[cellAddress];
        }
    }

    row_index++;
}
writeFile(wb, `filled_file.xlsx`, {
    cellStyles: true
});

}

- 确保工作表[“!ref”]涵盖您需要的所有列,并且 如果样式随行或列变化,请调整处理动态样式的方法 对于每个单元格。 - 您可以替换您的 fillSheet 与上面更新的功能

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.