vue+element加入导入Excel文件功能 前后台代码实现

在这里插入图片描述
在这里插入图片描述
废话不多说直接上代码

前台
element 自身就带了 导入的组件

 <el-button size="small"  v-if="isAuth('sys:holiday:holiday:exp')">批量导入</el-button>
 <el-upload
            class="upload-btn"
            :action="uploadUrl"
            accept=".xls, .xlsx"
            :on-success="handleAvatarSuccess"
            :on-progress="uploadProgress"
            :on-error="handleUploadError"
            :show-file-list="false"
          >

这个组件 的action事件 然后里面有url。
这里我们来注意一下分一下excel 导入的具体实现。就是上传excel 表,然后指定路径上,上传成功后,读取excel 表,然后对数据进行处理 实现。
所以1发送url到文件上传地方

export default {
   
    mixins: [requestTime],
    data () {
   
      return {
   
        year: '',
        uploadUrl:  this.$http.adornUrl(
          `/file/normalUpload?token=${
     this.$cookie.get('token')}`
        ),

这里因为项目里多次会用到,就封装起来

 @RequestMapping(path = "/normalUpload")
    @ResponseBody
    public R normalUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
   
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(factory);
        sfu.setHeaderEncoding
  • 5
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中使用TypeScript和ElementUI实现导入Excel需要安装xlsx、@types/xlsx和element-ui两个包,可以使用以下命令进行安装: ``` npm install xlsx @types/xlsx element-ui --save ``` 在Vue组件中,可以使用以下代码来读取Excel文件: ```typescript <template> <el-upload class="upload-excel" :show-file-list="false" :on-change="onFileChange" :before-upload="beforeUpload" > <el-button> <i class="el-icon-upload"></i> 选择文件 </el-button> </el-upload> </template> <script lang="ts"> import { defineComponent } from 'vue'; import * as XLSX from 'xlsx'; export default defineComponent({ data() { return { excelData: [] } }, methods: { beforeUpload(file: any) { const fileType = file.type; const validTypes = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']; if (validTypes.indexOf(fileType) === -1) { this.$message.error('只支持Excel文件'); return false; } return true; }, onFileChange(event: any) { const file = event.file.raw; const reader = new FileReader(); reader.onload = (event: any) => { const data = new Uint8Array(event.target.result); const workbook = XLSX.read(data, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const excelData = XLSX.utils.sheet_to_json(sheet, { header: 1 }); this.excelData = excelData; this.$message.success('文件上传成功'); }; reader.readAsArrayBuffer(file); } } }); </script> ``` 在模板中,使用ElementUI的`<el-upload>`组件来实现文件上传。在`beforeUpload`方法中判断文件类型,只允许上传Excel文件。在`onFileChange`方法中读取Excel文件,并将数据存储在`excelData`中,并使用`this.$message.success`来提示上传成功。 注意:在使用TypeScript的时候,需要对组件中的方法、参数、返回值进行类型定义。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值