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

使用EasyPoi导入数据并返回失败xls

添加依赖

<!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-base -->
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.4.0</version>
</dependency>

工具类

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;/*** @author PIGS* @version 1.0* @date 2020/4/25 14:16* @effect :* 表格数据工具类*/
public final class EasyPoiUtils {private EasyPoiUtils() {}private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {try {response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));workbook.write(response.getOutputStream());} catch (IOException e) {throw new RuntimeException(e);}}private static <T> void defaultExport(List<T> dataList, Class<?> clz, String fileName, HttpServletResponse response, ExportParams exportParams) {Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clz, dataList);if (workbook != null) {downLoadExcel(fileName, response, workbook);}}public static <T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, boolean isCreateHeader, HttpServletResponse response) {ExportParams exportParams = new ExportParams(title, sheetName);exportParams.setCreateHeadRows(isCreateHeader);defaultExport(dataList, clz, fileName, response, exportParams);}public static <T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, HttpServletResponse response) {defaultExport(dataList, clz, fileName, response, new ExportParams(title, sheetName));}private static void defaultExport(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {Workbook workbook = ExcelExportUtil.exportExcel(dataList, ExcelType.HSSF);if (workbook != null) {downLoadExcel(fileName, response, workbook);}}public static void exportExcel(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {defaultExport(dataList, fileName, response);}public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> clz) {if (StringUtils.isBlank(filePath)) {return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);try {return ExcelImportUtil.importExcel(new File(filePath), clz, params);} catch (Exception e) {throw new RuntimeException(e.getMessage());}}public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> clz) {if (file == null) {return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);try {return ExcelImportUtil.importExcel(file.getInputStream(), clz, params);} catch (Exception e) {throw new RuntimeException(e);}}/*** 导入数据* userEnity 你自己新建的实体类 实体类代码在下面* @param file* @param clz* @return*/public static <T> List<T> importExcel(MultipartFile file, Class<T> clazz) {if (file == null) {return null;}try(InputStream inputStream = file.getInputStream()){return ExcelImportUtil.importExcel(inputStream, clazz, new ImportParams());}catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}
}

代码


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;/*** <p>*  前端控制器* </p>**/
@RestController
@RequestMapping("/api/base")
@Slf4j
@Api(tags = "基础接口")
public class BaseController {@Resourceprivate IProductService service;@PostMapping("/importData")@ApiOperation("导入数据返回失败xls")public void importData(@RequestParam("file") MultipartFile file, HttpServletResponse response) {long l = System.currentTimeMillis();List<SalesDataEntity> checkingIns = EasyPoiUtils.importExcel(file, SalesDataEntity.class);List<SalesDataEntity> failedList = new ArrayList<>();for (SalesDataEntity salesDataEntity : checkingIns) {if (ObjUtil.isEmpty(salesDataEntity.getProducer())) {log.error("信息不可为空");failedList.add(salesDataEntity);continue;}try {// xxxx  业务逻辑处理service.insert(xxx);}catch (Exception e){e.printStackTrace();failedList.add(salesDataEntity);}}log.info(String.valueOf(System.currentTimeMillis() - l));EasyPoiUtils.exportExcel(failedList,null,"导入失败数据",SalesDataEntity.class,"失败数据",response);}}
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author PIGS* @version 1.0* @date 2020/4/25 14:16* @effect :* 用户实体类*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SalesDataEntity {@Excel(name = "序号", width = 5)private String rowNo;@Excel(name = "生产厂家", width = 36)private String producer;@Excel(name = "联系电话", width = 19)private String contactTel;@Excel(name = "产品型号", width = 38)private String model;@Excel(name = "流向地区", width = 25)private String targetArea;@Excel(name = "用户单位", width = 123)private String userCompany;@Excel(name = "用户单位地址", width = 91)private String userCompanyAddr;@Excel(name = "销售时间", width = 11)private String saleDate;@Excel(name = "销售数量", width = 9)private String saleCount;}

http://www.lryc.cn/news/274232.html

相关文章:

  • 机械配件移动商城课程概述
  • prometheus-docker 快速安装
  • RabbitMQ 核心概念(交换机、队列、路由键),队列类型等介绍
  • 1001 害死人不偿命的(3n+1)猜想
  • 七、HTML 文本格式化
  • OSI 模型和 TCP/IP 模型的异同
  • 创新性文生视频模型,南洋理工开源FreeInit
  • linux的页缓存page cache
  • 数字IC后端实现之Innovus TA-152错误解析(分频generated clock定义错误)
  • 虹科方案丨从困境到突破:TigoLeap方案引领数据采集与优化变革
  • 自检服务器,无需服务器、不用编程。
  • Java并行流parallelStream()下InheritableThreadLocal引起的问题
  • 【C++期末编程题题库】代码+详解18道
  • 一种DevOpts的实现方式:基于gitlab的CICD(一)
  • nodejs和vuejs的区别
  • 16、Kubernetes核心技术 - 节点选择器、亲和和反亲和
  • 面试算法96:字符串交织
  • 什么是Vue.js的响应式系统(reactivity system)?如何实现数据的双向绑定?
  • 力扣labuladong一刷day52天LRU算法
  • CCNP课程实验-06-EIGRP-Trouble-Shooting
  • 判断完全数-第11届蓝桥杯省赛Python真题精选
  • 【Bootstrap5学习 day12】
  • 算法训练第五十九天|503. 下一个更大元素 II、42. 接雨水
  • mysql之数据类型、建表以及约束
  • 复试 || 就业day04(2024.01.05)项目一
  • 华为机试真题实战应用【赛题代码篇】-最小传输时延(附python、C++和JAVA代码实现)
  • C++ 运算符重载
  • vue3学习 【2】vite起步和开发工具基本配置
  • 计算机创新协会冬令营——暴力枚举题目06
  • 单片机快速入门