当前位置:   article > 正文

vue3实现前端导出Excel_vue3导出excel

vue3导出excel

1、安装依赖

npm install xlsx

2、使用

  1. <template>
  2. <el-card class="mb15">
  3. <template #header>
  4. <span>vue3实现导出excel</span>
  5. </template>
  6. <el-button class="mb15" size="small" @click="startExport" type="primary">导出</el-button>
  7. <el-table :data="tableData" style="width: 100%">
  8. <el-table-column prop="date" label="Date" width="180" />
  9. <el-table-column prop="name" label="Name" width="180" />
  10. <el-table-column prop="address" label="Address" />
  11. </el-table>
  12. </el-card>
  13. </template>
  14. <script setup lang="ts" name="demoView">
  15. import * as XLSX from 'xlsx'
  16. const tableData = [
  17. {
  18. date: '2016-05-03',
  19. name: 'Tom',
  20. address: 'No. 189, Grove St, Los Angeles',
  21. },
  22. {
  23. date: '2016-05-02',
  24. name: 'Tom',
  25. address: 'No. 189, Grove St, Los Angeles',
  26. },
  27. {
  28. date: '2016-05-04',
  29. name: 'Tom',
  30. address: 'No. 189, Grove St, Los Angeles',
  31. },
  32. {
  33. date: '2016-05-01',
  34. name: 'Tom',
  35. address: 'No. 189, Grove St, Los Angeles',
  36. },
  37. ];
  38. interface excelType {
  39. json: object;
  40. name: string;
  41. titleArr: string[];
  42. sheetName: string;
  43. }
  44. const exportExcel = (params: excelType) => {
  45. const data = [];
  46. const keyArray = [];
  47. const getLength = function (obj: object) {
  48. let count = 0;
  49. for (const i in obj) {
  50. if (Object.prototype.hasOwnProperty.call(obj, i)) {
  51. // if (obj.hasOwnProperty(i)) {
  52. count++;
  53. }
  54. }
  55. return count;
  56. };
  57. for (const key1 in params.json) {
  58. if (Object.prototype.hasOwnProperty.call(params.json, key1)) {
  59. const element = (params.json as { [key: string]: object })[key1];
  60. const rowDataArray = [];
  61. for (const key2 in element) {
  62. if (Object.prototype.hasOwnProperty.call(element, key2)) {
  63. const element2 = (element as { [key: string]: object })[key2];
  64. rowDataArray.push(element2);
  65. if (keyArray.length < getLength(element)) {
  66. keyArray.push(key2);
  67. }
  68. }
  69. }
  70. data.push(rowDataArray);
  71. }
  72. }
  73. data.splice(0, 0, keyArray as any, params.titleArr as any);
  74. const ws = XLSX.utils.aoa_to_sheet(data);
  75. const wb = XLSX.utils.book_new();
  76. // 隐藏英文字段表头
  77. const wsrows = [{ hidden: true }];
  78. /* 设置worksheet每列的最大宽度 */
  79. const colWidth = data.map((row) =>
  80. row.map((val) => {
  81. /* 先判断是否为null/undefined */
  82. if (val == null) {
  83. return {
  84. wch: 10,
  85. };
  86. } else if (val.toString().charCodeAt(0) > 255) {
  87. /* 再判断是否为中文 */
  88. return {
  89. wch: val.toString().length * 2,
  90. };
  91. } else {
  92. return {
  93. wch: val.toString().length,
  94. };
  95. }
  96. })
  97. );
  98. /* 以第一行为初始值 */
  99. const result = colWidth[0];
  100. for (let i = 1; i < colWidth.length; i++) {
  101. for (let j = 0; j < colWidth[i].length; j++) {
  102. if (result[j].wch < colWidth[i][j].wch) {
  103. result[j].wch = colWidth[i][j].wch;
  104. }
  105. }
  106. }
  107. ws['!cols'] = result;
  108. ws['!rows'] = wsrows; // ws - worksheet
  109. XLSX.utils.book_append_sheet(wb, ws, params.sheetName);
  110. /* generate file and send to client */
  111. XLSX.writeFile(wb, `${params.name}.xlsx`);
  112. };
  113. const startExport = () => {
  114. exportExcel({
  115. json: tableData,
  116. name: '表格',
  117. titleArr: ['Date','Name','Address'],
  118. sheetName: 'sheet1',
  119. })
  120. }
  121. </script>
  122. <style lang="scss" scoped></style>

3、效果

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号