为什么'xlsx'和'file-saver'一起使用下载excel?

Why is 'xlsx' used with 'file-saver' to download excel?

我知道'xlsx'可以下载excel 如:

xlsx.writeFile(book, "saveXlsx.xlsx");

但是一些例子,他们使用'file-saver'下载excel

如:

function exportExcel(){ 
// step 1. create workbook
var wb = XLSX.utils.book_new();

// step 2. create sheet 
var newWorksheet = excelHandler.getWorksheet();

// step 3. append sheet to workbook
XLSX.utils.book_append_sheet(wb, newWorksheet, excelHandler.getSheetName());

// step 4. create excel file
var wbout = XLSX.write(wb, {bookType:'xlsx',  type: 'binary'});

// step 5. save excel file used file-saver
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), excelHandler.getExcelFileName());

var excelHandler = {
    getExcelFileName : function(){
        return 'table-test.xlsx';
    },
    getSheetName : function(){
        return 'Table Test Sheet';
    },
    getExcelData : function(){
        return document.getElementById('tableData'); 
    },
    getWorksheet : function(){
        return XLSX.utils.table_to_sheet(this.getExcelData());
    }

} }

他们为什么这样做?

我觉得第5步没必要,可以换成这样:

xlsx.writeFile(book, "saveXlsx.xlsx");

在大多数浏览器中与Readmehttps://www.npmjs.com/package/xlsx/v/0.18.5

Download a file in legacy browsers的信息相同

XLSX.writeFile techniques work for most modern browsers as well as older IE. For much older browsers, there are workarounds implemented by wrapper libraries.

FileSaver.js implements saveAs. Note: XLSX.writeFile will automatically call saveAs if available.

我想这取决于您的 xlsx 库 version.Maybe saveAs 在一些更旧的浏览器中运行良好,而 xlsx.writeFile 则不然。