使用单元格格式将数组导出到excel文件 [英] Exporting an array to excel file with cell formatting
问题描述
我正在尝试将数组导出到具有单元格格式的Excel文件。
https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js
但问题是,每当我尝试导出它(将文件保存为xlsx文件),这是显示的错误在控制台中:
未捕获TypeError:无法读取未定义的xlsx.js的属性writeFileSync:5182
writeSync xlsx Test.js:5182
p $ p>
最后2行基本上是代码的一部分,说明
XLSX.writeFile(wb,'sheetjs.xlsx');
我知道wb不是未定义,就好像我尝试和做console.log一样,excel电子表格正确显示:|
有人可以帮我吗?我也试图让每个单元格有不同的格式(IE不同的颜色/加粗/填充/等等)
解决方案你将您的代码基于node.js测试。 文档指出:
编写工作簿
对于写作,第一步是生成输出数据。助手
函数write和writeFile将生成各种格式的数据
适用于传播。第二步是将
数据与终点实际共享。假设工作簿是一个工作簿对象:
nodejs写入文件:
/ *由文件名* /
XLSX.writeFile(工作簿,'out.xlsx')确定的输出格式;
/ *在这一点上,out.xlsx是一个可以分发的文件* /
写入二进制字符串(使用FileSaver.js):
/ * bookType可以是'xlsx'或'xlsm'或'xlsb'* /
var wopts = {bookType:'xlsx',bookSST:false,type:'binary'} ;
var wbout = XLSX.write(workbook,wopts);
函数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;
}
/ * saveAs调用在本地机器上下载一个文件* /
saveAs(new Blob([s2ab(wbout)],{type:}) ,test.xlsx)
所以总结一下:你尝试使用node.js内部函数在浏览器中,哪个失败。如果你尝试按照秒的方法( XLSX.write()
而不是 XLSX.writeFile()
),你应该很好。
I'm currently trying to export an array to an excel file with cell formatting.
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:
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 :|
Can someone help me with this? I'm also trying to have each cell have a different formatting (IE different color/bolded/filled/etc)
解决方案 You base your code on a node.js test. The documentation states:
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:
/* 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):
/* 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.
这篇关于使用单元格格式将数组导出到excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!