打开ExcelJS生成的XLSX文件时出现警告

问题描述

我能够生成并保存一个xlsx文件,但是当我打开该文件时,出现以下错误警告。我可以查看该文件,但是每次都会出现此警告。

   <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>Repair Result to fileName-111.xml</logFileName><summary>Errors were detected in file ’/Users/yosefgamble/Downloads/fileName-11.xlsx’</summary><removedFeatures summary="Following is a list of removed features:"><removedFeature>Removed Feature: AutoFilter from /xl/tables/table1.xml part (Table)</removedFeature><removedFeature>Removed Feature: Table from /xl/tables/table1.xml part (Table)</removedFeature></removedFeatures></recoveryLog>

这是我的JavaScript代码Vue.Js。我正在将对象映射到不同的行,并使用写缓冲区和文件保护程序导出为XLSX。我使用合并可能会导致问题,但是注释掉似乎并没有解决文件。有什么办法可以找到问题所在?

async exportToExcel() { // On Click Excel download button
        const workbook = new ExcelJS.Workbook();

        workbook.modified = new Date();
        workbook.creator = 'ProactivComp';
        workbook.lastModifiedBy = "ProactivComp";
        workbook.created = new Date();

        const ws = workbook.addWorksheet('Timesheet Report',{
            headerfooter:{firstHeader: "Hello Exceljs"}
        });

        ws.columns = [
            { header: 'Date',key: 'DATE',width: 12 },{ header: 'Description',key: 'name',width: 57},{ header: 'Employee',key: 'EMP',width: 15,outlineLevel: 1 },{ header: 'Start',key: 'STAR',width: 12,{ header: 'Finish',key: 'FIN',width: 20,{ header: '',key: 'TIME',width: 10,];

        let count = 1;

        ws.addTable({
            name: 'Job @R_206_4045@ion',ref: 'A1',headerRow: true,totalsRow: false,style: {
                theme: 'TableStyleDark3',showRowStripes: true,},columns: [
                {name: 'JOB NUMBER ',filterButton: false},{name: 'Client',{name: 'Attention',{name: 'Job Date',{name: 'Rate',{name: 'Hours Total',],rows: [
            ],});

        this.jobsL.forEach(data =>{

            const jobHeader = ws.addRow([data.job_number,data.client_name,data.attention,data.job_date,data.rate]);
            jobHeader.getCell(1).alignment = { vertical: 'top',horizontal: 'left'};
            jobHeader.getCell(1).font = {  size: 16,bold: true};
            jobHeader.getCell(2).font = {  size: 14};
            jobHeader.commit()

            const descriptionHeader = ws.addRow(['',data.description,'','']);
            count += 2;
            ws.mergeCells('B'+count+':F'+count)
            // count += 2;
            // ws.mergeCells('A'+count+':E'+count);

            ws.addRow(['Date','Description','Employee','Start','Finish','Time (in hours)']);
            data.timesheets.forEach(data => {
                const timesheetRow = ws.addRow([data.date,data.employee_code,data.start_time,data.end_time,data.total_time])
                timesheetRow.getCell(1).alignment = { vertical: 'top',horizontal: 'left'};
                timesheetRow.getCell(2).alignment = { vertical: 'top',horizontal: 'left',wrapText: true };
                timesheetRow.getCell(3).alignment = { vertical: 'top',horizontal: 'left'};
                timesheetRow.getCell(4).alignment = { vertical: 'top',horizontal: 'left'};
                timesheetRow.getCell(5).alignment = { vertical: 'top',horizontal: 'left'};
                timesheetRow.getCell(6).alignment = { vertical: 'top',horizontal: 'left'};
                timesheetRow.commit();
            });

            ws.addRow(['','Total Hours'])

            data.employee_hours.forEach(data =>{
                const hoursRow = ws.addRow(['',data.total_time])
                hoursRow.getCell(5).alignment = { vertical: 'top',horizontal: 'left'};
                hoursRow.getCell(6).alignment = { vertical: 'top',horizontal: 'left'};
                hoursRow.commit();
            });
            ws.addRow(['',''])


            count += (data.timesheets.length+data.employee_hours.length+3)
            console.log(data)
        });

        const reportedTimeHeader1 = ws.addRow(['','Total Reported Time',''])
        reportedTimeHeader1.getCell(5).font = {  size: 11,bold: true};
        reportedTimeHeader1.commit();

        const reportedTimeHeader2 = ws.addRow(['','Hours'])
        reportedTimeHeader2.getCell(5).font = {bold: true};
        reportedTimeHeader2.getCell(6).font = {bold: true};
        reportedTimeHeader2.commit();

        this.report_totals.forEach(data =>{
            const totalsRow = ws.addRow(['',data.total_time])
            totalsRow.getCell(5).alignment = { vertical: 'top',horizontal: 'left'};
            totalsRow.getCell(6).alignment = { vertical: 'top',horizontal: 'left'};
            totalsRow.commit();
        });
        
        
        const buffer = await workbook.xlsx.writeBuffer();
        const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        const fileExtension = '.xlsx';

        const blob = new Blob([buffer],{type: fileType});

        await workbook.xlsx.writeBuffer(blob);

        FileSaver.saveAs(blob,'protime_timesheet_export' + fileExtension);
    },

解决方法

问题是 name 属性上的空格,删除字符串上的空格,它不会给你警告:

....
  ws.addTable({
    name: 'Job_Information',ref: 'A1',headerRow: true,...

事实证明,excel是这样做的,修复文件时,在表属性上添加了下划线,而不是在选项卡名称上。