ExcelWriter.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import writeXlsxFile from "write-excel-file";
  2. import { Options } from "./ExcelInterface";
  3. const columnNames = getColumnNames();
  4. function getColumnNames() {
  5. let wordList = Array.from(new Array(26), (ele, index) => {
  6. return String.fromCharCode(65 + index);
  7. });
  8. let columnNames = [];
  9. for (let i = 0; i < 26; i++) {
  10. columnNames.push(wordList[i]);
  11. }
  12. for (let i = 0; i < 26; i++) {
  13. for (let j = 0; j < 26; j++) {
  14. columnNames.push(wordList[i] + wordList[j]);
  15. }
  16. }
  17. return columnNames;
  18. }
  19. function convertType(value: any, type: any) {
  20. switch (type) {
  21. case String:
  22. return String(value);
  23. case Number:
  24. let output = Number(value);
  25. if (isNaN(output)) {
  26. output = 0;
  27. }
  28. return output;
  29. case Boolean:
  30. return Boolean(value);
  31. case Array:
  32. return Array.isArray(value) ? value : [value];
  33. case Object:
  34. return Object(value);
  35. case null:
  36. return null;
  37. case undefined:
  38. return undefined;
  39. default:
  40. return String(value);
  41. }
  42. }
  43. const ExcelWriter = {
  44. async getColumnNames() {
  45. return columnNames;
  46. },
  47. async outputExcel(headerConfig: Array<any>, data: Array<any>, options: Options) {
  48. let excelData: Array<any> = [];
  49. data.forEach((row, rowIndex) => {
  50. let rowObject = [] as any;
  51. headerConfig.forEach((headerItem, headerIndex) => {
  52. let column = this._dealCellData(row[headerItem.key], headerItem, options);
  53. rowObject.push(column);
  54. });
  55. excelData.push(rowObject);
  56. });
  57. if (!options.hasOwnProperty("headerRow")) {
  58. // 对象 options 上没有名为 "headerRow" 的属性
  59. options.headerRow = 1;
  60. }
  61. if (options.headerRow > 0) {
  62. let excelDataHeader = [] as any;
  63. headerConfig.forEach((headerItem, headerIndex) => {
  64. let column = this._dealCellData(headerItem.title, headerItem, options);
  65. column.type = String;
  66. column.value = headerItem.title;
  67. excelDataHeader.push(column);
  68. });
  69. excelData.splice(options.headerRow - 1, 0, excelDataHeader);
  70. //excelDataBody.unshift(excelDataHeader);
  71. }
  72. //excelData = [excelDataHeader, ...excelDataBody];
  73. await this._output(excelData, options);
  74. },
  75. //==================私有方法============================
  76. /**
  77. * 生成单元格数据
  78. * @param {*} columnCellValue
  79. * @param {*} header
  80. * @param {*} options
  81. * @returns
  82. */
  83. _dealCellData(columnCellValue: any, header: any, options: Options) {
  84. let cell = {
  85. value: convertType(columnCellValue, header?.type ? header?.type : String) // columnCellValue.toString().trim()
  86. } as any;
  87. if (header?.width) {
  88. cell.width = header.width;
  89. }
  90. if (header?.type) {
  91. cell.type = header.type;
  92. }
  93. if (options?.border) {
  94. //如果有边框
  95. cell.borderColor = options?.borderColor ?? "#000000";
  96. }
  97. return cell;
  98. },
  99. /**
  100. * 导出excel表格
  101. * @param {*} data
  102. * @param {*} options
  103. */
  104. async _output(data: Array<any>, options: Options) {
  105. let excelRows = [];
  106. data.forEach((row: Array<any>, rowIndex) => {
  107. row.forEach((cell, cellIndex) => {
  108. let column = {
  109. value: cell
  110. };
  111. excelRows.push(column);
  112. });
  113. });
  114. let fileName = options?.fileName ? options.fileName : "导出.xlsx";
  115. let columns = options?.columns ? options.columns : [];
  116. let optionsOutput = {
  117. columns, // (optional) column widths, etc.
  118. fileName
  119. };
  120. await writeXlsxFile(data, optionsOutput);
  121. },
  122. /**
  123. * 导出示例
  124. */
  125. async outputTest() {
  126. const HEADER_ROW = [
  127. {
  128. value: "Name",
  129. fontWeight: "bold"
  130. },
  131. {
  132. value: "Date of Birth",
  133. fontWeight: "bold"
  134. },
  135. {
  136. value: "Cost",
  137. fontWeight: "bold"
  138. },
  139. {
  140. value: "Paid",
  141. fontWeight: "bold"
  142. }
  143. ];
  144. const DATA_ROW_1 = [
  145. [
  146. // "Name"
  147. {
  148. type: String,
  149. value: "John Smith",
  150. borderColor: "#000000"
  151. },
  152. // "Date of Birth"
  153. {
  154. type: Date,
  155. value: new Date(),
  156. format: "mm/dd/yyyy"
  157. },
  158. // "Cost"
  159. {
  160. type: Number,
  161. value: 1800
  162. },
  163. // "Paid"
  164. {
  165. type: Boolean,
  166. value: true
  167. }
  168. ],
  169. [
  170. // "Name"
  171. {
  172. type: String,
  173. value: "第2行",
  174. borderColor: "#000000"
  175. },
  176. // "Date of Birth"
  177. {
  178. type: Date,
  179. value: new Date(),
  180. format: "yyyy-mm-dd"
  181. },
  182. // "Cost"
  183. {
  184. type: Number,
  185. value: 1800,
  186. span: 3,
  187. borderColor: "#ff0000"
  188. },
  189. // "Paid"
  190. null,
  191. null,
  192. {
  193. type: String,
  194. value: "第2行00",
  195. borderColor: "#000000",
  196. fontSize: 18
  197. }
  198. ]
  199. ];
  200. const data = [HEADER_ROW, ...DATA_ROW_1] as any;
  201. let options = {
  202. // columns, // (optional) column widths, etc.
  203. fileName: "file.xlsx"
  204. };
  205. await writeXlsxFile(data, options);
  206. }
  207. };
  208. export default ExcelWriter;