前端vue excel文件流导入两种方式

说明: 两种导入都可以使用, 第二种方法更简洁

1. 第一种导入文件方式

1-1. 使用组件

                          <div class="upload-excel">
                                <el-upload id="eccel"
                                           :disabled="buttonDis?true:false"
                                           class="upload"
                                           ref="upload"
                                           accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                                           :before-upload="beforeAvatarUpload"
                                           :action="uploadUrl()"
                                           :on-success="uploadSuccess"
                                           :headers="headers"
                                           :multiple="false"
                                           :limit="1">
                                    <el-button :disabled="buttonDis" size="small" type="primary"><i
                                            class="el-icon-upload2"></i>&nbsp;导入数据
                                    </el-button>&nbsp;
                                </el-upload>
                            </div>

1-2. 导入事件

            /************导入文件**************/
            //对导入文件校验
            beforeAvatarUpload(file) {
                let excelData = file.name.substring(file.name.lastIndexOf('.') + 1)
                const extension = excelData === 'xls'
                const extension2 = excelData === 'xlsx'
                const isLt2M = file.size / 1024 / 1024 < 10;

                if (!extension && !extension2) {
                    this.$message.error('请上传 .xlsx .xls格式的文件!');
                    return false;
                }
                if (!isLt2M) {
                    this.$message.error('上传的文件大小不能超过 10MB!');
                    return false;
                }
                const clearTime = 3;
                let num = 0;
                this.buttonDis = true;
                const timer = setInterval(() => {
                    if (num < clearTime) {
                        num++
                    } else {
                        clearInterval(timer)
                        this.$refs.upload.clearFiles()
                        this.buttonDis = false;
                    }
                }, 1000);
                return (extension || extension2) && isLt2M
            },


            //导入excel路径
            uploadUrl() {
                return (process.env.VUE_APP_BASE_API + '/system/feedingcurve/excel');
            },


            //导入成功返回信息
            uploadSuccess(file) {
                if (file.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '导入成功,请等待数据解析!'
                    });
                    this.$refs.mychild.getAllCurves(0, "", this.majorType);
                } else {
                    this.$message.error(file.desc);
                }
            },

2. 第二种导入文件方式

2-1.使用组件

        <div class="moban">
          <el-upload class="upload-demo" :on-change='fileUpload' :auto-upload="false" action='' accept=".xlsx" :show-file-list="false">
            <el-button icon="el-icon-circle-plus-outline" type="primary">导入设备</el-button>
          </el-upload>
          
        </div>

2-2. 导入事件

import { uploadBatchDeviceTemplate } from '@/axios/api'

    //导入事件
    fileUpload (file) {
      console.log(file)
      if (!file) return;
      uploadBatchDeviceTemplate({ file: file.raw }).then((res) => {
        if (res.code == 200) {
          this.$message.success('导入成功!')
          this.fetchDataSearch()
        } else {
          this.$message.error('导入失败!')
        }

      })
    },

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端可以使用FileReader API将Excel文件读取为二进制数据,然后将其发送到后端Java服务器。后端可以使用Apache POI或JExcelAPI等Java库来解析Excel文件并将其换为数据。以下是一个简单的代码示例: 前端代码: ``` <template> <div> <input type="file" @change="handleFileChange"> </div> </template> <script> export default { methods: { handleFileChange(event) { const file = event.target.files[0] const reader = new FileReader() reader.onload = () => { const data = reader.result this.uploadFile(data) } reader.readAsBinaryString(file) }, uploadFile(data) { // 将数据发送到后端Java服务器 } } } </script> ``` 后端代码: ``` @PostMapping("/upload") public List<List<String>> uploadExcel(@RequestParam("file") MultipartFile file) throws IOException { Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); List<List<String>> data = new ArrayList<>(); for (Row row : sheet) { List<String> rowData = new ArrayList<>(); for (Cell cell : row) { rowData.add(cell.toString()); } data.add(rowData); } return data; } ``` 这个示例使用Spring Boot和Apache POI来处理Excel文件。在这个例子中,我们将Excel文件作为MultipartFile对象上传到后端,并使用WorkbookFactory创建一个Workbook对象。然后,我们使用getSheetAt方法获取第一个Sheet对象,并使用for循环遍历每一行和每一列,将每个单元格的值添加到一个字符串列表中,并将该列表添加到数据列表中。最后,我们返回数据列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值