小言_互联网的博客

前端实战:Vue实现数据导出导入案例

362人阅读  评论(0)

❤️作者主页:IT技术分享社区

❤️作者简介:大家好,我是IT技术分享社区的博主,从事C#、Java开发九年,对数据库、C#、Java、前端、运维、电脑技巧等经验丰富。

❤️荣誉: CSDN博客专家、数据库优质创作者🏆,华为云享专家🏆,阿里云专家博主🏆 

❤️个人博客:IT技术分享社区

❤️公众号/小程序:IT技术分享社区 

❤️好文章点赞 👍 收藏 ⭐再看,养成习惯

目录

1、创建一个空白的vue2/vue3项目

2、创建Export.vue 示例文件


项目开发当中,列表数据的导出功能基本是每个业务系统必备的功能、另外Excel数据批量导入数据库也是比较常见的功能,一般开发都会采用POI、EasyExcel等后端框架实现,后端服务实现的话,如果涉及业务调整的话,生产环境需要重启后端服务。如果采用前端处理的话,就会方便很多,今天给大家介绍采用Vue框架集成xlsx组件的方式实现数据的导入、导出功能,希望对大家能有所帮助!

1、创建一个空白的vue2/vue3项目

可以通过脚手架方式创建一个vue示例项目。

需要的依赖包如下


      
  1. "dependencies": {
  2. "element-ui": "2.10.1",
  3. "export2excel": "0.0.1",
  4. "file-saver": "^2.0.5",
  5. "vue": "^2.5.2",
  6. "vue-router": "^3.0.1",
  7. "xlsx": "^0.17.0"
  8. },

通过命令安装


      
  1. npm install export2excel @0. 0.1 --save #导出到excel依赖包
  2. npm install file-saver @2. 0.5 --save #文件保存到客户端
  3. npm install xlsx @0. 17.0 --save #操作excel依赖包

2、创建Export.vue 示例文件

主要实现表格内容的导出和文件内容导入的页面的表格当中,具体文件内容完整内容如下:


      
  1. <template>
  2. <div class="hello">
  3. <h1> {{ msg }} </h1>
  4. <el-row>
  5. <el-button size="small" type="primary" @click="exportTest">导出 </el-button>
  6. <el-upload action="/" :on-change="importTest" :show-file-list="false"
  7. accept= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
  8. :auto-upload= "false">
  9. <el-button size="small" icon="el-icon-upload" type="primary">导入数据 </el-button>
  10. </el-upload>
  11. </el-row>
  12. <el-row>
  13. <el-table ref="multipleTable" style="padding-top: 10px;" :data="listData" tooltip-effect="light"
  14. highlight-current-row :header-cell-style= "{
  15. background: '#E6EAF3',
  16. 'font-size': '13px',
  17. padding: '0px',
  18. height: '40px',
  19. }" v-loading= "listLoading" :cell-style= "{ 'font-size': '13px', padding: '0px', height: '34px' }">
  20. <el-table-column label="序号" type="index" width="50"> </el-table-column>
  21. <el-table-column label="姓名" show-overflow-tooltip width="110">
  22. <template slot-scope="scope"> {{ scope.row.name }} </template>
  23. </el-table-column>
  24. <el-table-column label="年龄" show-overflow-tooltip width="">
  25. <template slot-scope="scope"> {{ scope.row.age }} </template>
  26. </el-table-column>
  27. </el-table>
  28. </el-row>
  29. </div>
  30. </template>
  31. <script>
  32. import { export_json_to_excel } from "@/vendor/Export2Excel";
  33. import Xlsx from 'xlsx'
  34. export default {
  35. name: 'HelloWorld',
  36. data( ) {
  37. return {
  38. msg: '导入导出测试',
  39. listData: [
  40. { name: "小明", age: 30 },
  41. { name: "小张", age: 25 },
  42. { name: "小李", age: 29 }
  43. ],
  44. listLoading: false,
  45. xlscTitle: {
  46. "姓名": "name",
  47. "年龄": "age"
  48. },
  49. }
  50. },
  51. methods: {
  52. exportTest( ) {
  53. const header = [
  54. "姓名",
  55. "年龄"
  56. ];
  57. const body = [
  58. "name",
  59. "age",
  60. ];
  61. const data = this. formatJson(body, this. listData);
  62. console. log(data);
  63. export_json_to_excel({
  64. header: header, // 表头
  65. data: data, // 数据列表
  66. filename: "用户表", // 保存文件名
  67. });
  68. },
  69. //格式化json数据为导出数据 过滤掉查询的数据列不在导出的列里面的数据
  70. formatJson( filterVal, jsonData) {
  71. return jsonData. map( (a) => filterVal. map( (b) => a[b]));
  72. },
  73. importTest( file) {
  74. let self = this;
  75. const types = file. name. split( '.')[ 1];
  76. const fileType = [ 'xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv']. some( item => {
  77. return item === types
  78. });
  79. if (!fileType) {
  80. this. $message. error( '文件格式错误,请重新选择文件!')
  81. }
  82. this. file2Xce(file). then( tab => {
  83. // 过滤,转化正确的JSON对象格式
  84. if (tab && tab. length > 0) {
  85. tab[ 0]. sheet. forEach( item => {
  86. let obj = {};
  87. for ( let key in item) {
  88. obj[self. xlscTitle[key]] = item[key];
  89. }
  90. self. listData. push(obj);
  91. });
  92. if (self. listData. length) {
  93. this. $message. success( '上传成功')
  94. // 获取数据后,下一步操作
  95. } else {
  96. this. $message. error( '空文件或数据缺失,请重新选择文件!')
  97. }
  98. }
  99. })
  100. },
  101. // 读取文件
  102. file2Xce( file) {
  103. return new Promise( function ( resolve, reject) {
  104. const reader = new FileReader();
  105. reader. onload = function ( e) {
  106. const data = e. target. result;
  107. //var Xlsx = require("xlsx");
  108. this. wb = Xlsx. read(data, {
  109. type: "binary"
  110. });
  111. const result = [];
  112. this. wb. SheetNames. forEach( sheetName => {
  113. result. push({
  114. sheetName: sheetName,
  115. sheet: Xlsx. utils. sheet_to_json( this. wb. Sheets[sheetName])
  116. })
  117. })
  118. resolve(result);
  119. }
  120. reader. readAsBinaryString(file. raw);
  121. })
  122. }
  123. }
  124. }
  125. </script>
  126. <!-- Add "scoped" attribute to limit CSS to this component only -->
  127. <style scoped>
  128. h1,
  129. h2 {
  130. font-weight: normal;
  131. }
  132. ul {
  133. list-style-type: none;
  134. padding: 0;
  135. }
  136. li {
  137. display: inline-block;
  138. margin: 0 10px;
  139. }
  140. a {
  141. color: #42b983;
  142. }
  143. </style>


转载:https://blog.csdn.net/xishining/article/details/128605915
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场