gpt4 book ai didi

javascript - 使用 js-xlsx 写入给出 "_fs is undefined"错误

转载 作者:行者123 更新时间:2023-11-27 22:48:15 27 4
gpt4 key购买 nike

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

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

是否可以在没有 requirejs 的情况下做到这一点?如果是的话,怎么办?

HTML:

<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:

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');
};

最佳答案

xlsx 库有两个写入文件的函数 writewriteFile。 writeFile 方程实现了节点 fs 模块,因此旨在在节点环境中使用。另外,write 是使用客户端 JavaScript 实现的,因此在您的情况下可能会更有用。 IE:
write(wb, {bookType: 'xlsx' , bookSST: false, 类型: '二进制'});

关于javascript - 使用 js-xlsx 写入给出 "_fs is undefined"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251338/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com