首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用js-xlsx写入时出现"_fs is undefined“错误

使用js-xlsx写入时出现"_fs is undefined“错误
EN

Stack Overflow用户
提问于 2016-07-08 01:02:37
回答 1查看 1.4K关注 0票数 1

我正在尝试使用https://github.com/SheetJS/js-xlsx将一些数组数据导出到文件中。

我举了一个例子,在这里放了一个jsfiddle:https://jsfiddle.net/connyake/y8a9nb7r/

有没有可能在不要求的情况下做到这一点呢?如果是,是如何实现的?

HTML:

代码语言:javascript
复制
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.core.min.js"></script>
<button onclick="arrayToXlsx()">Export</button>

JS:

代码语言:javascript
复制
function arrayToXlsx() {
    /* original data */
    var data = [[1,2,3],[true, false, null, "sheetjs"],["foo","bar","0.3"], ["baz", null, "qux"]];
    var ws_name = "SheetJS";

    /* set up workbook objects -- some of these will not be required in the future */
    var wb = {};
    wb.Sheets = {};
    wb.Props = {};
    wb.SSF = {};
    wb.SheetNames = [];

    /* create worksheet: */
    var ws = {};

    /* the range object is used to keep track of the range of the sheet */
    var range = {s: {c:0, r:0}, e: {c:0, r:0 }};

    /* Iterate through each element in the structure */
    for(var R = 0; R !== data.length; ++R) {
      if(range.e.r < R) range.e.r = R;
      for(var C = 0; C !== data[R].length; ++C) {
        if(range.e.c < C) range.e.c = C;

        /* create cell object: .v is the actual data */
        var cell = { v: data[R][C] };
        if(cell.v === null) continue;

        /* create the correct cell reference */
        var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

        /* determine the cell type */
        if(typeof cell.v === 'number') cell.t = 'n';
        else if(typeof cell.v === 'boolean') cell.t = 'b';
        else cell.t = 's';

        /* add to structure */
        ws[cell_ref] = cell;
      }
    }
    ws['!ref'] = XLSX.utils.encode_range(range);

    /* add worksheet to workbook */
    wb.SheetNames.push(ws_name);
    wb.Sheets[ws_name] = ws;

    /* write file */
    XLSX.writeFile(wb, 'test.xlsx');
};
EN

回答 1

Stack Overflow用户

发布于 2017-04-03 18:08:13

xlsx库有两个用于写入文件writewriteFile的函数。writeFile等式实现了节点fs模块,因此打算在节点环境中使用。或者,write是使用客户端javascript实现的,因此在您的环境中可能会更有用。即:write(wb, {bookType: 'xlsx' , bookSST: false, type: 'binary'});

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38251338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档