https://www.npmjs.com/package/json2csv
https://www.npmjs.com/package/xlsx
https://www.npmjs.com/package/js-xlsx

MIME类型列表

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

npm install json2csv xlsx

例子一 json2csv

  1. var {Parser} = require('json2csv');
  2. var fs = require('fs');
  3. var fields = ['car', 'price'];
  4. var myCars = [
  5. {
  6. "car": "Audi",
  7. "price": 40000,
  8. "color": "blue"
  9. }, {
  10. "car": "BMW",
  11. "price": 35000,
  12. "color": "black"
  13. }, {
  14. "car": "Porsche",
  15. "price": 60000,
  16. "color": "green"
  17. }
  18. ];
  19. var opts = {
  20. fields: fields,
  21. // quotes: '',
  22. excelStrings: true
  23. };
  24. try {
  25. var parser = new Parser(opts);
  26. var csv = parser.parse(myCars);
  27. fs.writeFile('file.csv', csv, function(err) {
  28. if (err) throw err;
  29. console.log('file saved');
  30. });
  31. fs.writeFile('file.xls', csv, function(err) {
  32. if (err) throw err;
  33. console.log('file saved');
  34. });
  35. } catch (err) {
  36. console.log(err);
  37. }
  1. debugger;
  2. var XLSX = require('xlsx');
  3. var workbook = XLSX.readFile('./file.csv');
  4. // var workbook = XLSX.readFile('./file.xls'); // 两种都可以使用
  5. var fs = require('fs');
  6. XLSX.writeFile(workbook, 'out.xlsx');
  7. var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
  8. var wbout = XLSX.write(workbook, wopts);
  9. function s2ab(s) {
  10. var buf = new ArrayBuffer(s.length);
  11. var view = new Uint8Array(buf);
  12. for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  13. return buf;
  14. }
  15. let buffer = Buffer.from(s2ab(wbout));
  16. fs.writeFile('file2.xls', buffer, {encoding: "utf-8"},function(err) {
  17. if (err) throw err;
  18. console.log('file saved');
  19. });
  20. debugger;
  1. var {Parser} = require('json2csv');
  2. var XLSX = require('xlsx');
  3. var fields = ['car', 'price'];
  4. var myCars = [
  5. {
  6. "car": "Audi",
  7. "price": 40000,
  8. "color": "blue"
  9. }, {
  10. "car": "BMW",
  11. "price": 35000,
  12. "color": "black"
  13. }, {
  14. "car": "Porsche",
  15. "price": 60000,
  16. "color": "green"
  17. }
  18. ];
  19. var opts = {
  20. fields: fields,
  21. // quotes: '',
  22. excelStrings: true
  23. };
  24. try {
  25. var parser = new Parser(opts);
  26. var csv = parser.parse(myCars);
  27. // let buffer = Buffer.from(csv);
  28. // var workbook = XLSX.read(buffer.toString(), {type:"binary"});
  29. var workbook = XLSX.read(csv, {type:"binary"});
  30. XLSX.writeFile(workbook, 'out2.xlsx');
  31. } catch (err) {
  32. console.log(err);
  33. }

例子二 xlsx

  1. const XLSX = require('xlsx');
  2. const filename = './output.xlsx';
  3. const workbook = XLSX.readFile(filename);
  4. // 获取 Excel 中所有表名
  5. var sheetNames = workbook.SheetNames; // 返回 ['sheet1', 'sheet2',……]
  6. // 根据表名获取对应某张表
  7. var worksheet = workbook.Sheets[sheetNames[0]];
  8. // 将表转换为json
  9. var workjson = XLSX.utils.sheet_to_json(worksheet);
  10. console.log(workjson);

  1. const XLSX = require('xlsx');
  2. const fs = require('fs');
  3. var json = [{ key: 'aaa', value: 'bbb' }];
  4. var workbook = XLSX.utils.json_to_sheet(json);
  5. // 构建 workbook 对象
  6. var wb = {
  7. SheetNames: ['mySheet'],
  8. Sheets: {
  9. 'mySheet': workbook
  10. }
  11. };
  12. XLSX.writeFile(wb, './output.xlsx');