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();
    }
}
相关推荐
阿猫的故乡1 分钟前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
裕波15 分钟前
Vue&ViteConf 2026 将于 7 月 18 日在上海举办,尤雨溪将现场发表主题演讲
vue.js·vite
Sam_Deep_Thinking2 小时前
Spring Boot 的启动原理是什么?
java·spring boot·后端
如果超人不会飞2 小时前
TinyRobot SuggestionPills紧凑的建议按钮组组件
前端·vue.js
如果超人不会飞2 小时前
TinyRobot Container构建优雅的AI对话容器
前端·vue.js
屋外雨大,惊蛰出没2 小时前
深入浅出Spring Boot
java·spring boot·ioc·aop
如果超人不会飞2 小时前
TinyRobot SuggestionPopover智能建议弹出框组件
前端·vue.js
协享科技3 小时前
Spring Boot 与 Go 双服务架构实践:从单体拆分到通信设计
java·人工智能·spring boot·后端·架构·golang·ai编程
zhedream4 小时前
从模糊到清晰:一次组件重构里的开发哲学
vue.js
如果超人不会飞4 小时前
TinyRobot AI 对话组件库全组件使用指南
前端·vue.js