javascript 使用单元格格式将数组导出到 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27548378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:37:08  来源:igfitidea点击:

Exporting an array to excel file with cell formatting

javascriptexcelxlsx

提问by Johnti

I'm currently trying to export an array to an excel file with cell formatting.

我目前正在尝试使用单元格格式将数组导出到 excel 文件。

I'm starting off with this code here:

我从这里开始使用这段代码:

https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js

But the problem is that whenever I'm trying to export it (save the file as an xlsx file) this is the error that shows up in the console:

但问题是,每当我尝试导出它(将文件另存为 xlsx 文件)时,就会出现在控制台中的错误:

Uncaught TypeError: Cannot read property 'writeFileSync' of undefined    xlsx.js:5182 
writeSync                 xlsx.js:5182 
writeFileSync             xlsx.js:5173 
process_xlsx              Test.html:379 
reader.onload             Test.html:438 

The last 2 lines are basically the part of the code which says

最后两行基本上是代码的一部分,它说

XLSX.writeFile(wb, 'sheetjs.xlsx');

I know wb is not undefined as if I try and do console.log of it, the excel spreadsheet shows up properly :|

我知道 wb 不是未定义的,就好像我尝试执行它的 console.log 一样,excel 电子表格正确显示:|

Can someone help me with this? I'm also trying to have each cell have a different formatting (IE different color/bolded/filled/etc)

有人可以帮我弄这个吗?我也试图让每个单元格有不同的格式(即不同的颜色/粗体/填充/等)

回答by Sirko

You base your code on a node.js test. The documentationstates:

您的代码基于 node.js 测试。该文件规定:

Writing Workbooks

For writing, the first step is to generate output data. The helper functions write and writeFile will produce the data in various formats suitable for dissemination. The second step is to actual share the data with the end point. Assuming workbook is a workbook object:

nodejs write to file:

写作练习册

对于写入,第一步是生成输出数据。辅助函数 write 和 writeFile 将生成适合传播的各种格式的数据。第二步是与端点实际共享数据。假设工作簿是一个工作簿对象:

nodejs 写入文件:

/* output format determined by filename */
XLSX.writeFile(workbook, 'out.xlsx');
/* at this point, out.xlsx is a file that you can distribute */

write to binary string (using FileSaver.js):

写入二进制字符串(使用 FileSaver.js):

/* bookType can be 'xlsx' or 'xlsm' or 'xlsb' */
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };

var wbout = XLSX.write(workbook,wopts);

function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}

/* the saveAs call downloads a file on the local machine */
saveAs(new Blob([s2ab(wbout)],{type:""}), "test.xlsx")

So to sum up: You try to use node.js internal functions in the browser, which fails. If you try to follow the seconds approach ( XLSX.write()instead of XLSX.writeFile()), you should be fine.

所以总结一下:您尝试在浏览器中使用 node.js 内部函数,但失败了。如果您尝试遵循秒方法(XLSX.write()而不是XLSX.writeFile()),则应该没问题。