0

当我尝试编辑 excel 文件并将其上传到服务器时,它会写入文件,但是当我尝试打开它时,我收到一条错误消息“我们发现某些内容存在问题 .xlsx 错误”

const uploadAttachment = async (arrayAttach) => {
    try {
        let sftp = new Client();
        await sftp.connect(config)
        const promises = arrayAttach.map(async attachment => {
            const workbook = new Excel.Workbook();
            await workbook.xlsx.load(attachment.buffer);
            workbook.worksheets[0].name = 'sheet1';
            const buffer = await workbook.xlsx.writeBuffer();
            sftp.put(buffer, `test/${attachment.originalname}.xlsx`);
        });
        Promise.all(promises)
            .then(results => {
                console.log('all files have been uploaded');
            })
            .catch(e => {
                logger.error(e)
                console.error(e);
            });
    } catch (err) {
        logger.error(err)
        console.log(err);
    }
}

错误信息

但是,如果我将文件上传到服务器而不进行编辑,我会做对,我附上了一个示例代码

const uploadAttachmentOld = async (arrayAttach) => {
    try {
        let sftp = new Client();
        await sftp.connect(config)
        const promises = arrayAttach.map(async attachment => {
            logger.info(attachment)
            sftp.put(attachment.buffer, `test/${attachment.originalname}.xlsx`);
        });
        Promise.all(promises)
            .then(results => {
                console.log('all files have been uploaded');
            })
            .catch(e => {
                console.error(e);
            });
    } catch (err) {
        console.log(err);
    }
}

如果有人可以帮助我了解我的错误在哪里,我会很高兴

4

0 回答 0