SpringBoot+Vue实现简单的文件上传(Excel篇)

SpringBoot+Vue实现简单的文件上传

1 环境 SpringBoot 3.2.1,Vue 2,ElementUI
2 页面

3 效果 :只能上传xls文件且大小限制为2M,选择文件后自动上传。
4 前端代码

javascript 复制代码
<template>
  <div class="container">
    <el-upload
        class="upload-demo"
        drag
        action="/xml/fileUpload"
        multiple
        accept=".xls"
    :before-upload="beforeUpload">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      <div class="el-upload__tip"><slot name="tip" > 只能上传 xls 文件,且不超过2M</slot></div>
    </el-upload>
  </div>
</template>

<script>
// import axios from "axios";

export default {
  name: 'App',
  data() {
    const data = [];
    return {
      filterText: '',
      data: JSON.parse(JSON.stringify(data)),
      copyData: [],
      nodeForm: {},
      formShow: false,
      checkNode: {},
      xml: '',
      typeList: [
        {
          value: 'root',
          label: '根节点'
        }, {
          value: 'node',
          label: '子节点'
        }
      ]
    }
  },
  watch: {},
  created() {
  },
  methods: {
    beforeUpload(file){
      const isText = file.type == "application/vnd.ms-excel"
      const isLt2M = file.size /1024 /1024 < 2
      if(!isText){
        this.$message.error("只能上传xls文件!")
        return false;
      }
      if(!isLt2M){
        this.$message.error("文件大小超过限制!")
        return false;
      }
      return true;
    }

  }
}
</script>

<style>
.container {
  display: flex;
}
</style>

5 后端代码

java 复制代码
package org.wjg.onlinexml.service.impl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.wjg.onlinexml.po.Result;
import org.wjg.onlinexml.service.FileService;

import java.io.IOException;

@Service("xls")
public class XLSServiceImpl implements FileService {

    @Override
    public Result upload(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.builder().code(500).msg("上传失败!").build();
        }


        try (Workbook workbook = new HSSFWorkbook(file.getInputStream())) {
            //获取第一个sheet页
            Sheet sheet = workbook.getSheetAt(0);
            //遍历每行
            for (Row row : sheet) {
                //遍历每个单元格
                for (Cell cell : row) {
                    System.out.print(cell.getStringCellValue() + " ");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.builder().code(200).msg("上传成功").build();
    }
}
相关推荐
2402_8813193015 小时前
MyBatis-Plus + MySQL JSON 字段处理
spring boot·学习方法
Mcband16 小时前
【Spring Boot】Interceptor的原理、配置、顺序控制及与Filter的关键区别
java·spring boot·后端
qq_3482318516 小时前
Spring Boot 体系核心全解
java·spring boot·后端
m0_6161884916 小时前
循环多个表单进行表单校验
前端·vue.js·elementui
幸运小圣16 小时前
关于Vue 3 <script setup> defineXXX API 总结
前端·javascript·vue.js
AAA阿giao17 小时前
从零开始:用 Vue 3 + Vite 打造一个支持流式输出的 AI 聊天界面
前端·javascript·vue.js
玉宇夕落17 小时前
Vue 3 实现 LLM 流式输出:从零搭建一个简易 Chat 应用
前端·vue.js
雨中飘荡的记忆17 小时前
拼团系统设计与实现
java·spring boot
爱看书的小沐17 小时前
【小沐学WebGIS】基于Cesium.JS绘制雷达波束/几何体/传感器Sensor(Cesium / vue / react )
javascript·vue.js·react.js·雷达·cesium·传感器·波束
星空椰17 小时前
Java Excel转PDF
pdf·excel