excelUtils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const Excel = require('exceljs');
  3. const utils = {
  4. // 定义格式: 工作表
  5. // [ {sheetName:''} , {} ]
  6. // 获取excel
  7. getExcel(config) {
  8. if (!config) {
  9. return;
  10. }
  11. const workbook = new Excel.Workbook();
  12. config.forEach((con, index) => {
  13. const ws1 = workbook.addWorksheet(con.sheetName || 'sheet' + index, con.sheetOptions);
  14. con.sheetHeader.forEach(header => {
  15. const row = ws1.addRow([ header.headerName ]);
  16. Object.keys(header.headerConfig).forEach(key => {
  17. row[key] = header.headerConfig[key];
  18. });
  19. });
  20. ws1.mergeCells(con.sheetKey[0].letter + '1:'
  21. + con.sheetKey[con.sheetKey.length - 1].letter + '1');
  22. ws1.addRow(con.sheetKey.map(item => item.label));
  23. con.sheetData.forEach((data, index) => {
  24. const rowData = con.sheetKey.map((item, i) => {
  25. ws1.getColumn(i + 1).width = item.width;
  26. if (item.key == 'num') {
  27. return index + 1;
  28. }
  29. // console.log('====================', item)
  30. // if (item.key == 'birthday') {
  31. // const myDate = new Date();
  32. // const year = myDate.getFullYear();
  33. // const month = (myDate.getMonth() + 1) > 9 ? (myDate.getMonth() + 1) : '0' + (myDate.getMonth() + 1);
  34. // const day = myDate.getDate() > 9 ? (myDate.getDate()) : '0' + (myDate.getDate());
  35. // return item.value = ;
  36. // }
  37. return this.getValue(data, item) || '';
  38. });
  39. ws1.addRow(rowData);
  40. });
  41. const tableKeyHeader = 1;
  42. const tableFirst = 1;
  43. this.rowCenter(ws1, tableFirst,
  44. con.sheetData.length + con.sheetHeader.length + tableKeyHeader);
  45. });
  46. return workbook;
  47. },
  48. rowCenter(arg_ws, arg_start, arg_end) {
  49. for (let i = arg_start; i <= arg_end; i++) {
  50. arg_ws.findRow(i).alignment = { vertical: 'middle', horizontal: 'center' };
  51. arg_ws.findRow(i).eachCell(function(cell, index) {
  52. if (i == 1) {
  53. cell.font = { bold: true };
  54. } else {
  55. cell.font = { bold: false };
  56. }
  57. cell.border = {
  58. top: { style: 'thin' },
  59. left: { style: 'thin' },
  60. bottom: { style: 'thin' },
  61. right: { style: 'thin' },
  62. };
  63. });
  64. }
  65. },
  66. getValue(data, config) {
  67. if (config.key.indexOf('.') != -1) { // 存在子属性
  68. const props = config.key.split('.');
  69. return props.reduce((first, second) => {
  70. if (first) {
  71. return first[second];
  72. }
  73. return null;
  74. }, data);
  75. } // 不存在
  76. return data[config.key];
  77. },
  78. formatName(name) {
  79. if (name === null || name === undefined) {
  80. return ' ';
  81. }
  82. if (name.length < 2 || name.length >= 10) {
  83. return ' ';
  84. } else if (name.length === 2) {
  85. return name[0] + '*';
  86. }
  87. // 如果名字长度大于2位,保留首尾汉字,中间用星号替换
  88. const firstChar = name[0]; // 获取首位汉字
  89. const lastChar = name[name.length - 1]; // 获取末位汉字
  90. const middleStars = '*'.repeat(name.length - 2); // 生成中间的星号字符串
  91. return firstChar + middleStars + lastChar;
  92. },
  93. formatPhone(phone) {
  94. if (phone === null || phone === undefined) {
  95. return ' ';
  96. }
  97. if (phone.length < 8 || phone.length >= 30) {
  98. // 长度小于2位,返回空格
  99. return ' ';
  100. } else if (phone.length === 8) {
  101. const firstChar = phone[0]; // 获取首位
  102. const secondChar = phone[1];
  103. const thirdChar = phone[2];
  104. const lastChar = phone[phone.length - 1];
  105. const last2Char = phone[phone.length - 2];
  106. const middleStars = '*'.repeat(phone.length - 5); // 生成中间的星号字符串
  107. return firstChar + secondChar + thirdChar + middleStars + last2Char + lastChar;
  108. }
  109. const firstChar = phone[0]; // 获取首位
  110. const secondChar = phone[1];
  111. const thirdChar = phone[2];
  112. const lastChar = phone[phone.length - 1]; // 获取末位
  113. const last2Char = phone[phone.length - 2];
  114. const last3Char = phone[phone.length - 3];
  115. const last4Char = phone[phone.length - 4];
  116. const middleStars = '*'.repeat(phone.length - 7); // 生成中间的星号字符串
  117. return firstChar + secondChar + thirdChar + middleStars + last4Char + last3Char + last2Char + lastChar;
  118. },
  119. formatIdCard(idCard) {
  120. if (idCard === null || idCard === undefined) {
  121. return ' ';
  122. }
  123. if (idCard.length === 15 || idCard.length === 18) {
  124. const firstChar = idCard[0]; // 获取首位
  125. const secondChar = idCard[1]; // 获取第二位
  126. const thirdChar = idCard[2]; // 获取第三位
  127. const first4Char = idCard[3]; // 获取第三位
  128. const first5Char = idCard[4]; // 获取第三位
  129. const first6Char = idCard[5]; // 获取第三位
  130. const lastChar = idCard[idCard.length - 1]; // 获取末位
  131. const last2Char = idCard[idCard.length - 2];
  132. const middleStars = '*'.repeat(idCard.length - 8); // 生成中间的星号字符串
  133. return firstChar + secondChar + thirdChar + first4Char + first5Char + first6Char + middleStars + last2Char + lastChar;
  134. }
  135. return ' ';
  136. },
  137. };
  138. module.exports = utils;