最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Exporting an array to excel file with cell formatting - Stack Overflow

programmeradmin2浏览0评论

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

I'm starting off with this code here:

.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)

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./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)

Share Improve this question asked Dec 18, 2014 at 14:04 JohntiJohnti 1332 gold badges2 silver badges8 bronze badges 4
  • If you use that testfile as a basis, make sure to properly include all dependencies – Sirko Commented Dec 18, 2014 at 14:12
  • For this, isn't the dependencies just xlsx.js and jszip.js? I also included jquery.js in case – Johnti Commented Dec 18, 2014 at 14:19
  • Can you include your actual code as is currently? – Sirko Commented Dec 18, 2014 at 14:20
  • Right now, just as a test, this is what I have: notepad/share/AOFCw4Elv8 – Johnti Commented Dec 18, 2014 at 14:26
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布评论

评论列表(0)

  1. 暂无评论