打开生成的文件

时间:2018-02-19 14:05:59

标签: javascript excel excelbuilder.js

我正在使用excel-builder.js生成一个excel文件,如下所示:

    require([
    './js/LibrariesForXLSX/excel-builder',
    './js/LibrariesForXLSX/Excel/Table'
], function(builder, Table) {
    var book = builder.createWorkbook();

    //creating worksheets

    var userDataList = book.createWorksheet({
        name: 'mysheet'
    });

    //creating tables
    var userTable = new Table();
    userTable.setReferenceRange([1, 1], [2, userData.length]);

    //adding data and tables to sheets        
    userDataList.setData(userData);
    userDataList.addTable(userTable);
    //adding worksheets to workbook 
    book.addWorksheet(userDataList);

    //adding tables to workbook
    boook.addTable(userTable);

    //creating download data;
    var workBook = builder.createFile(book);
    var rawExcelData = "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + workBook;
    var blob = exp_getBlob(workBook, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

    //downloading workbook
    saveAs(blob, 'export.xlsx');
});
}

其中userData是一个2D javascript数组,如

['name', 'John Doe'
 'age', 23
]

saveAs是来自fileSaver.js

的函数

一切正常,文件也会生成,但当我尝试用Microsoft Excel 2016打开文件时,我收到如下错误:

  

我们发现“export.xlsx'”中的某些内容存在问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”。

如果我点击是,一切都很好,并且正确的数据显示在Excel中。 我可以以某种方式摆脱此错误消息吗?

0 个答案:

没有答案