FileUtil.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. var fs = require("fs");
  2. var xlsx = require('node-xlsx');
  3. const path = require('path')
  4. var compressing = require("compressing");
  5. /**
  6. * 读取excel数据
  7. * @param {*} url_excel excel路径
  8. * @param {*} sheetnum 是否多个sheet
  9. * @returns
  10. */
  11. function getExcelData(url_excel, sheetnum = 1) {
  12. var sheets = xlsx.parse(url_excel);
  13. return sheetnum == 1 ? sheets[0].data : sheets;
  14. }
  15. /**
  16. * 生成json
  17. * @param {*} data json数据
  18. * @param {*} url_json 生成json路劲
  19. */
  20. function createJson(data, url_json) {
  21. console.log(url_json,data);
  22. fs.writeFile(url_json, JSON.stringify(data, "", "\t"), "utf8", function (err) {
  23. if (err) throw err;
  24. console.log(" WriteFile: " + url_json + " OK!");
  25. });
  26. }
  27. /**
  28. *
  29. * @param {*} jsonpath
  30. * @param {*} zippath
  31. */
  32. function createZip(jsonpath, zippath) {
  33. compressing.zip.compressDir(jsonpath, zippath)
  34. .then(() => {
  35. console.log('success');
  36. })
  37. .catch(err => {
  38. console.error(err);
  39. });
  40. // compressing.zip.compressDir('data', 'nodejs-compressing-demo.zip');
  41. }
  42. module.exports = {
  43. getExcelData,
  44. createJson,
  45. createZip,
  46. }