EasyExcel实现excel导入(模版上传)

目录

  • 效果
  • pom.xml
  • application.yml
  • controller
  • service
  • 依赖类
  • 前台
    • vue代码

某个功能如果需要添加大量的数据,通过一条条的方式添加的方式,肯定不合理,本文通过excel导入的方式来实现该功能,100条数据导入成功85条,失败15条,肯定需要返回一个表格给前台或者返回1个错误excel给前台,本文就实现这两个功能

效果

在这里插入图片描述

  • 模版 storeTemplate.xlsx
    在这里插入图片描述

在这里插入图片描述

  • 点击确定,会直接下载excel错误文件,打开如下图
    在这里插入图片描述
  • 删除第一行 第一列后,修正后,可再次导入,方便调整错误数据

pom.xml

<!-- easypoi-spring-boot-starter --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.1.3</version></dependency>

application.yml

#####模板下载位置
downloadTemplate:storePath: storeTemplate.xlsx
  • 提供部分代码,主要是了解思路,可参考实现

controller

package com.zyee.iopace.web.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.zyee.iopace.web.config.annotation.Log;
import com.zyee.iopace.web.config.exception.BusinessException;
import com.zyee.iopace.web.dto.station.DeleteBatchPhotoDto;
import com.zyee.iopace.web.dto.station.StationStoreAddDto;
import com.zyee.iopace.web.dto.station.StationStoreDto;
import com.zyee.iopace.web.dto.store.DeleteStoreBatchPhotoDto;
import com.zyee.iopace.web.dto.store.StationStoreUpdateDto;
import com.zyee.iopace.web.dto.store.StoreBindStationDto;
import com.zyee.iopace.web.dto.store.StoreChangeTrackDto;
import com.zyee.iopace.web.entity.*;
import com.zyee.iopace.web.enums.BusinessType;
import com.zyee.iopace.web.enums.EcologyType;
import com.zyee.iopace.web.enums.StationStateEnum;
import com.zyee.iopace.web.enums.StoreStatusType;
import com.zyee.iopace.web.response.ResponseResult;
import com.zyee.iopace.web.service.*;
import com.zyee.iopace.web.utils.BeanCopyUtils;
import com.zyee.iopace.web.utils.PageUtils;
import com.zyee.iopace.web.vo.station.StationStoreDetailVo;
import com.zyee.iopace.web.vo.station.StationStoreVo;
import com.zyee.iopace.web.vo.store.StoreChangeTrackListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;@Slf4j
@Api(tags = "库存管理")
@RestController
@RequestMapping("/stationStore")
public class StationStoreController {@Autowiredprivate StoreReviseRecordService storeReviseRecordService;@ApiOperation(value = "导入库存信息")@Log(title = "导入库存信息", businessType = BusinessType.IMPORT)@PostMapping("/importStore")public ResponseResult importStore(@RequestParam MultipartFile file, HttpServletResponse response) throws Exception {return ResponseResult.SUCCESS(stationStoreService.importStore(file,response));}
}

service

package com.zyee.iopace.web.service;import com.zyee.iopace.web.dto.dept.DeptSearchDto;
import com.zyee.iopace.web.dto.station.StationStoreAddDto;
import com.zyee.iopace.web.dto.station.StationStoreDto;
import com.zyee.iopace.web.dto.store.StationStoreUpdateDto;
import com.zyee.iopace.web.dto.store.StoreBindStationDto;
import com.zyee.iopace.web.dto.store.StoreChangeTrackDto;
import com.zyee.iopace.web.entity.StationStore;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zyee.iopace.web.entity.StoreChangeTrack;
import com.zyee.iopace.web.vo.element.ElementImportResponseVo;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.util.List;public interface StationStoreService extends IService<StationStore> {/***  导入库存信息* @param file* @param response* @return  失败返回错误地址*/ElementImportResponseVo importStore(MultipartFile file, HttpServletResponse response) throws Exception;
}@Service
public class StationStoreServiceImpl extends ServiceImpl<StationStoreMapper, StationStore> implements StationStoreService {@Overridepublic ElementImportResponseVo importStore(MultipartFile file, HttpServletResponse response) throws Exception {List<StoreImportVo> stationImportVos = PoiUtils.importExcel(file, StoreImportVo.class, null);//成功List<StationStore> stationStoreList = new ArrayList<>();//失败信息的集合List<StoreImportVo> errorList = new ArrayList<>();//导入成功的数量int success = 0;//失败条数int failure = 0;//当前登录用户idString userId = userUtils.getUserId();//型号List<StoreCompanyModel> companyModelList = storeCompanyModelService.list();//序列号集合List<StationStore> stationStores = list(new LambdaQueryWrapper<StationStore>().select(StationStore::getSerialNumber));List<String> serialNumberList = new ArrayList<>();if(CollectionUtil.isNotEmpty(stationStores)){serialNumberList = stationStores.stream().map(StationStore::getSerialNumber).distinct().collect(Collectors.toList());}//站点集合List<Station> stationList = stationService.list(new LambdaQueryWrapper<Station>().orderByAsc(Station::getStationCode));if(CollectionUtil.isEmpty(stationList)){stationList = new ArrayList<>();}for (StoreImportVo storeImport : stationImportVos) {StringBuilder errMsg = new StringBuilder();StationStore stationStore = checkStore(storeImport, errMsg, userId, companyModelList, serialNumberList, stationList);if (errMsg.toString().length() > 0) {failure++;storeImport.setErrorCause(errMsg.toString());errorList.add(storeImport);}else{success++;stationStoreList.add(stationStore);}}if(CollectionUtil.isNotEmpty(stationStoreList)){//批量新增saveBatch(stationStoreList);//增加送检判断stationStoreList.stream().forEach(store->{if(Objects.nonNull(store.getRectifyTime())){//库存送检记录StationStoreAddDto dto = new StationStoreAddDto();dto.setRectifyTime(store.getRectifyTime());saveRevise(dto, store);}if(Objects.nonNull(store.getStationId())){//绑定站点stationChangeTrack(null, store);}});}//将错误数据上传String visitUrl = "";String msg = "批量站点信息完成,成功导入" + success + "条数据,失败" + failure  + "条";if (failure > 0) {try {String name = UUID.randomUUID()+".xlsx";Workbook workbook = null;StoreExcelStyler.setProperties(stationService,storeCompanyService,storeCompanyModelService);workbook = PoiUtils.exportErrorExcel(errorList, name, name,StoreImportVo.class, name, StoreExcelStyler.class, response);StoreExcelStyler.setData(workbook);FileOutputStream outputStream = new FileOutputStream(pathService.getAbsoTemplateExcelPath(name));//写入workbook.write(outputStream);visitUrl = pathService.getTemplateExcelPath(name);} catch (Exception e) {e.printStackTrace();}}ElementImportResponseVo vo = new ElementImportResponseVo();vo.setMsg(msg);vo.setVisitUrl(visitUrl);return vo;}/*** 验证格式有问题* @param storeImport* @param errMsg* @param userId* @return*/private StationStore checkStore(StoreImportVo storeImport, StringBuilder errMsg, String userId,List<StoreCompanyModel> companyModelList,List<String> serialNumberList,List<Station> stationList) {StationStore store = new StationStore();if(StringUtils.isBlank(storeImport.getCompanyName(

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/20875.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于Django快递物流管理可视化分析系统(完整系统源码+数据库+详细开发文档+万字详细论文+答辩PPT+详细部署教程等资料)

文章目录 基于Django快递物流管理可视化分析系统&#xff08;完整系统源码数据库详细开发文档万字详细论文答辩PPT详细部署教程等资料&#xff09;一、项目概述二、项目说明三、研究意义四、系统设计技术架构 五、功能实现六、完整系统源码数据库详细开发文档万字详细论文答辩P…

动态规划,复习

一.动态规划 1. 思路&#xff1a;每次递增序列结束都取最优解 class Solution { public:int findLengthOfLCIS(vector<int>& nums) {int ans -1,temp 1;for(int i 0;i<nums.size()-1;i){if(nums[i]<nums[i1]) temp;else{ans max(ans,temp);temp 1;}}ans…

【鸿蒙开发】第三十八章 ArkTS代码调试

debug启动调试attach启动调试等待调试使用断点检查变量使用调试器反向调试extension调试worker/taskpool调试Evaluate and logSmart Step Into 1 debug启动调试 如果需要设置断点调试&#xff0c;找到需要暂停的代码片断&#xff0c;点击该代码行的左侧边线&#xff0c;或将光…

个人简历html网页模板,科技感炫酷html简历模板

炫酷动效登录页 引言 在网页设计中,按钮是用户交互的重要元素之一。这样一款黑色个人简历html网页模板,科技感炫酷html简历模板,设计效果类似科技看板图,可帮您展示技能、任职经历、作品等,喜欢这种风格的小伙伴不要犹豫哦。该素材呈现了数据符号排版显示出人形的动画效…

Python深度学习环境配置(Pytorch、CUDA、cuDNN),包括Anaconda搭配Pycharm的环境搭建以及基础使用教程(保姆级教程,适合小白、深度学习零基础入门)

全流程导览 一、前言二、基本介绍2.1全过程软件基本介绍2.1.1 Pytorch2.1.2 Anaconda2.1.3 Pycharm2.1.4 显卡GPU及其相关概念2.1.5 CUDA和cuDNN 2.2 各部分相互间的联系和安装逻辑关系 三、Anaconda安装3.1安装Anaconda3.2配置环境变量3.3检验是否安装成功 四、Pycharm安装五、…

案例-18.文件上传-阿里云OSS-集成

一.分析 要想将阿里云oss集成到新增员工的功能中&#xff0c;必须要设计文件上传的接口UploadController。首先前端给接口上传接口需要接收请求的图片&#xff0c;然后接口再将图片上传到阿里云oss中存储起来&#xff0c;接着接口从阿里云oss中获取图片的url并返回给前端&#…

在Unity中用简单工厂模式模拟原神中的元素反应

1. 第一步创建3个脚本Factory&#xff08;反应工厂&#xff09;&#xff0c;Reactions&#xff08;具体反应&#xff09;&#xff0c;FactoryText&#xff08;测试反应的脚本&#xff09; 2.编写工厂脚本 using UnityEngine;// 定义一个元素反应的接口&#xff0c;所有具体的元…

Markdown 常用语法及示例

介绍 Markdown 是一种轻量级标记语言&#xff0c;广泛用于编写文档、README 文件、博客文章等。本文将介绍 Markdown 的一些常用语法&#xff0c;帮助你快速上手。官网地址1 1. 标题 Markdown 使用井号 # 来表示标题&#xff0c;井号的数量决定了标题的级别&#xff0c;最多…

Ubuntu USB耳机找不到设备解决

​ 一. 确定硬件连接 lsusb -t 插拔USB耳机&#xff0c;确定是否有USB识别到 二. 查看输出设备 sudo apt-get install pavucontrol pavucontrol 点击想要使用的输出设备后面的绿色选项 三. 输出设备没有USB耳机时调试 3.1 确认ALSA是否识别设备 列出ALSA播放设备&#…

企业软件合规性管理:构建高效、安全的软件资产生态

引言 在数字化转型的浪潮下&#xff0c;企业的软件使用方式日益多元化&#xff0c;涉及云端、订阅制、永久授权及浮动许可等多种模式。然而&#xff0c;随着软件资产的增多&#xff0c;企业面临着合规性管理的严峻挑战&#xff1a;非法软件使用、许可证管理不当、软件资产闲置…

el-table树状表格,默认展开第一个节点的每一层

效果如图 <template><el-table:data"tableData"style"width: 100%":tree-props"{ children: children, hasChildren: hasChildren }":expand-row-keys"expandRowKeys"row-key"id"expand-change"handleExpan…

C ASCII字符表示

1-1 ASCII字符表示 sprintf(buf, "A5%c%02d", SOFTWARE_VERSRION_1 A - 1, (U16)SOFTWARE_VERSRION_4); 在程序中通常会出现这样的写法&#xff0c;A对应的ASCII字符表示的含义是65&#xff0c;一直往后到95获得的支付是B是66&#xff0c;然后Z对应的是90&#xff…

【iOS】包大小和性能稳定性优化

包大小优化 图片 LSUnusedResources 扫描重复的图片 ImageOptim,压缩图片 压缩文件 优化音视频资源 &#xff0c;使用MP3 代替 WAV ffmpeg -i input.mp3 -b:a 128k output.mp3 视频 H.265&#xff08;HEVC&#xff09; 代替 H.264 ffmpeg ffmpeg -i input.mp4 -vcodec lib…

智能选路+NAT实验

智能选路NAT实验 1.拓扑 2.配置 1.配IP&#xff08;按图配&#xff0c;略&#xff09; 2.配真实DNS服务器 3.虚拟服务器 4.配置DNS透明代理功能 [USG6000V1]dns-transparent-policy [USG6000V1-policy-dns]dns t [USG6000V1-policy-dns]dns transparent-proxy en [USG6000…

【Linux网络-网络基础】TCP/IP五层(或四层)模型+网络传输的基本流程

一、TCP/IP五层&#xff08;或四层&#xff09;模型 TCP/IP 是一组协议的代名词&#xff0c;它还包括许多协议&#xff0c;组成了 TCP/IP 协议簇。 TCP/IP 通讯协议采用了 5 层的层级结构&#xff0c;每一层都呼叫它的下一层所提供的网络来完成自己的需求。 ❍ 物理层&#…

POI优化Excel录入

57000单词原始录入时间258S 核心代码: List<Word> wordBookList ExcelUtil.getReader(file.getInputStream()).readAll(Word.class);if (!CollectionUtil.isEmpty(wordBookList)) {for (Word word : wordBookList) {//逐条向数据库中插入单词wordMapper.insert(word);}…

Solon —— 容器

说明 Solon 的核心概念有 IoC、AOP 和本地事件总线。有人常常有误解以为 IoC 和 AOP 是 Spring 提出的&#xff0c;其实这两种思想在Spring 之前就已经有了&#xff0c;但 Spring 把这两个思想在技术上落地和推广做得很好&#xff0c;让 Ioc 和 AOP 广为人知。 核心概念 IoC…

java每日精进 2.13 MySql迁移人大金仓

1.迁移数据库 1. 数据库创建语句 MySQL&#xff1a; CREATE DATABASE dbname; 人大金仓&#xff08;Kingbase&#xff09;&#xff1a; 在人大金仓中&#xff0c;CREATE DATABASE 的语法通常相同&#xff0c;但可能需要特别注意字符集的指定&#xff08;如果涉及到多语言支持…

《DeepSeek 一站式工作生活 AI 助手》

最近国产AI工具DeepSeek在全球火出圈&#xff0c;登顶多个国家应用商店&#xff0c;下载量一路飙升。这匹AI “黑马” 到底凭什么征服全球用户&#xff1f;让我们全方位解锁DeepSeek——从基础入门到高阶玩法&#xff0c;从实用技巧到隐藏功能。 DeepSeek是一款功能强大的国产A…

php-fpm

摘要 php-fpm(fastcgi process manager)是PHP 的FastCGI管理器&#xff0c;管理PHP的FastCGI进程&#xff0c;提升PHP应用的性能和稳定性 php-fpm是一个高性能的php FastCGI管理器&#xff0c;提供了更好的php进程管理方式&#xff0c;可以有效的控制内存和进程&#xff0c;支…