excel.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // +---------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +---------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  5. // +---------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +---------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +---------------------------------------------------------------------
  10. /* eslint-disable */
  11. import XLSX from 'xlsx';
  12. function auto_width(ws, data) {
  13. /*set worksheet max width per col*/
  14. const colWidth = data.map((row) =>
  15. row.map((val) => {
  16. /*if null/undefined*/
  17. if (val == null) {
  18. return { wch: 10 };
  19. } else if (val.toString().charCodeAt(0) > 255) {
  20. /*if chinese*/
  21. return { wch: val.toString().length * 2 };
  22. } else {
  23. return { wch: val.toString().length };
  24. }
  25. }),
  26. );
  27. /*start in the first row*/
  28. let result = colWidth[0];
  29. for (let i = 1; i < colWidth.length; i++) {
  30. for (let j = 0; j < colWidth[i].length; j++) {
  31. if (result[j]['wch'] < colWidth[i][j]['wch']) {
  32. result[j]['wch'] = colWidth[i][j]['wch'];
  33. }
  34. }
  35. }
  36. ws['!cols'] = result;
  37. }
  38. function json_to_array(key, jsonData) {
  39. return jsonData.map((v) =>
  40. key.map((j) => {
  41. return v[j];
  42. }),
  43. );
  44. }
  45. // fix data,return string
  46. function fixdata(data) {
  47. let o = '';
  48. let l = 0;
  49. const w = 10240;
  50. for (; l < data.byteLength / w; ++l)
  51. o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
  52. o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
  53. return o;
  54. }
  55. // get head from excel file,return array
  56. function get_header_row(sheet) {
  57. const headers = [];
  58. const range = XLSX.utils.decode_range(sheet['!ref']);
  59. let C;
  60. const R = range.s.r; /* start in the first row */
  61. for (C = range.s.c; C <= range.e.c; ++C) {
  62. /* walk every column in the range */
  63. var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]; /* find the cell in the first row */
  64. var hdr = 'UNKNOWN ' + C; // <-- replace with your desired default
  65. if (cell && cell.t) hdr = XLSX.utils.format_cell(cell);
  66. headers.push(hdr);
  67. }
  68. return headers;
  69. }
  70. export const export_table_to_excel = (id, filename) => {
  71. const table = document.getElementById(id);
  72. const wb = XLSX.utils.table_to_book(table);
  73. XLSX.writeFile(wb, filename);
  74. /* the second way */
  75. // const table = document.getElementById(id);
  76. // const wb = XLSX.utils.book_new();
  77. // const ws = XLSX.utils.table_to_sheet(table);
  78. // XLSX.utils.book_append_sheet(wb, ws, filename);
  79. // XLSX.writeFile(wb, filename);
  80. };
  81. export const export_json_to_excel = ({ data, key, title, filename, autoWidth }) => {
  82. const wb = XLSX.utils.book_new();
  83. data.unshift(title);
  84. const ws = XLSX.utils.json_to_sheet(data, { header: key, skipHeader: true });
  85. if (autoWidth) {
  86. const arr = json_to_array(key, data);
  87. auto_width(ws, arr);
  88. }
  89. XLSX.utils.book_append_sheet(wb, ws, filename);
  90. XLSX.writeFile(wb, filename + '.xlsx');
  91. };
  92. export const export_array_to_excel = ({ key, data, title, filename, autoWidth }) => {
  93. const wb = XLSX.utils.book_new();
  94. const arr = json_to_array(key, data);
  95. arr.unshift(title);
  96. const ws = XLSX.utils.aoa_to_sheet(arr);
  97. if (autoWidth) {
  98. auto_width(ws, arr);
  99. }
  100. XLSX.utils.book_append_sheet(wb, ws, filename);
  101. XLSX.writeFile(wb, filename + '.xlsx');
  102. };
  103. export const read = (data, type) => {
  104. /* if type == 'base64' must fix data first */
  105. // const fixedData = fixdata(data)
  106. // const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
  107. const workbook = XLSX.read(data, { type: type });
  108. const firstSheetName = workbook.SheetNames[0];
  109. const worksheet = workbook.Sheets[firstSheetName];
  110. const header = get_header_row(worksheet);
  111. const results = XLSX.utils.sheet_to_json(worksheet);
  112. return { header, results };
  113. };
  114. export default {
  115. export_table_to_excel,
  116. export_array_to_excel,
  117. export_json_to_excel,
  118. read,
  119. };