当前位置: 首页 > news >正文

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

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

1 环境 SpringBoot 3.2.1,Vue 2,ElementUI
2 页面
在这里插入图片描述

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

<template><div class="container"><el-uploadclass="upload-demo"dragaction="/xml/fileUpload"multipleaccept=".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 < 2if(!isText){this.$message.error("只能上传xls文件!")return false;}if(!isLt2M){this.$message.error("文件大小超过限制!")return false;}return true;}}
}
</script><style>
.container {display: flex;
}
</style>

5 后端代码

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 {@Overridepublic 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();}
}
http://www.lryc.cn/news/400428.html

相关文章:

  • 科研绘图系列:R语言金字塔图(pyramid plot)
  • Tomcat多实例
  • 前端Vue组件化实践:自定义加载组件的探索与应用
  • 半小时获得一张ESG入门证书【详细中英文笔记一】
  • 类形断言和和类型推导的区别是什么?
  • Spring-Spring、IoC、DI、注解开发
  • Facebook的未来蓝图:从元宇宙到虚拟现实的跨越
  • Redis6.2.1版本集群新加副本
  • 2.The DispatcherServlet
  • bug定位策略
  • 基于R语言的水文、水环境模型优化技术及快速率定方法与多模型案例
  • 内存函数(C语言)
  • 力扣 哈希表刷题回顾
  • Qt 统计图编程
  • SQL中的谓词与谓词下推
  • 浅聊授权-spring security和oauth2
  • 时间复杂度计算
  • React 18 + Babel 7 + Webpack 5 开发环境搭建
  • MongoDB Shard 集群 Docker 部署
  • MacOS 开发 — Packages 程序 macOS新版本 演示选项卡无法显示
  • Hive的分区表分桶表
  • PostgreSQL17索引优化之支持并行创建BRIN索引
  • 在Vue中,子组件向父组件传递数据
  • 数据结构(顺序表)
  • MySQL之基本查询(上)-表的增删查改
  • RocketMQ源码学习笔记:Producer发送消息流程
  • kotlin flow collect collectLatest 区别
  • ELK集群搭建
  • zookeeper+kafka消息队列集群部署
  • LLM_入门指南(零基础搭建大模型)