EasyPoi导入验证功能

摘要:
1准备好要导入的Excel,注意Excel的标题要和domain中的@Excel一样1导入验证包支持˂!

1准备好要导入的Excel,注意Excel的标题要和domain中的@Excel(name = "标题")一样

EasyPoi导入验证功能第1张

1 导入验证包支持

<!-- JSR 303 规范验证包 -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.2.4.Final</version>
</dependency>

2 domain中添加验证方法

@Entity
@Table(name = "employee")
public class Employee extendsBaseDomain {

    @Excel(name = "名字")
    @NotNull(message = "用户名不能空")
    privateString username;
    privateString password;
    @Excel(name = "年龄")
    @Max(value = 100)
    @Min(value = 18)
    privateInteger age;
    @Excel(name = "邮箱",width = 20)
    @NotNull
    privateString email;
    ...
}

3 自定义验证

  • 实现IExcelVerifyHandler
  • 把这个类交给Spring管理(千万不要忘了让Spring去扫描到它)
    /*** 自定义验证(我们会在这里做唯一性的验证)
     */@Component
    public class EmployeeExcelVerifyHandler implements IExcelVerifyHandler<Employee>{
    
        @Autowired
        privateIEmployeeService employeeService;
        /***
         * ExcelVerifyHandlerResult
         *   suceess :代表验证成功还是失败(如果用户名重复,就代表失败)
         *   msg:失败的原因
         */@Override
        publicExcelVerifyHandlerResult verifyHandler(Employee employee) {
    
            ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
            //如果根据用户名获取到用户,代表这个用户已经存在
            Employee tempEmp =employeeService.findByUsername(employee.getUsername());
            if(tempEmp!=null){
                result.setSuccess(false);
                result.setMsg("用户名重复");
            }
    
            returnresult;
        }
    }

    3 实现验证功能

  • @Controller
    @RequestMapping("/import")
    public class ImportController extends BaseController {
    
        @Autowired
        private IEmployeeService employeeService;
        @Autowired
        private IDepartmentService departmentService;
        @Autowired
        private EmployeeExcelVerifyHandler employeeExcelVerifyHandler;
    
        @RequestMapping("/index")
        public String index(){
            return "import";
        }
    
    
        @RequestMapping("/employeeXlsx")
        public String employeeXlsx(MultipartFile empFile, HttpServletResponse response) throws Exception {
            //一.使用EasyPoi获取文件数据
            ImportParams params = new ImportParams();
            params.setHeadRows(1);
            params.setNeedVerfiy(true); //设置验证支持
            params.setVerifyHandler(employeeExcelVerifyHandler); //设置一个验证处理器
    
            //二.获取excel中的数据,封装成了一个结果对象(有很多东西)
            ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore(
                    empFile.getInputStream(),
                    Employee.class, params);
            //三.获到正确的数据,并把它们保存到数据库
            List<Employee> list = result.getList();
            list.forEach(e->{
                e.setPassword("123");
                Department dept = departmentService.findByName(e.getDepartment().getName());
                e.setDepartment(dept);
                employeeService.save(e);
            });
            //四.如果有错误,把错误数据返回到前台(让前台下载一个错误的excel)
           //4.1判断是否有错误
            if(result.isVerfiyFail()){
                //4.2拿到错误的文件薄
                Workbook failWorkbook = result.getFailWorkbook();
    
                //把这个文件导出
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
                response.setHeader("Content-disposition", "attachment;filename=error.xlsx"); //告诉浏览下载的是一个附件,名字叫做error.xlsx
                response.setHeader("Pragma", "No-cache");//设置不要缓存
                OutputStream ouputStream = response.getOutputStream();
                failWorkbook.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
    
            return "import";
        }
    
    
    }
    

免责声明:文章转载自《EasyPoi导入验证功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇IntelliJ Idea 2019.1.3永久激活方式,JAR文件分享由微博图床挂掉之后想到的下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

EasyPoi导出问题

导出代码如下:Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), PriceExcelModel.class, priceExcelModelList); File file = FileUtil.createFile(PROCEED_DATA_SOURCE_PATH);...

vue springboot利用easypoi实现简单导出

vue springboot利用easypoi实现简单导出 前言 一、easypoi是什么? 二、使用步骤 1.传送门 2.前端vue 3.后端springboot 3.1编写实体类(我这里是dto,也一样) 3.2控制层 结尾 前言 今天玩了一下vue springboot利用easypoi实现excel的导出,以前没玩过导入导出,...

EasyPoi使用入门

咱们在开发的时候,总会遇到需要通过代码操作办公软件的情况,而excel与word的操作最为频繁。 当然我们Java程序员可以选择JXL或者POI来完成相应的Excel操作,但是大家用过都知道,有些地方感觉还是不够简单,不那么尽如人意。 今天给大家介绍一个EasyPoi,就算我们不会底层的POI,也可以非常轻松的完成Excel的操作。EasyPoi,主打简单...

阿里 EasyExcel 使用及避坑

github地址:https://github.com/alibaba/easyexcel 原本在项目中使用EasyPoi读取excel,后来为了统一技术方案,改用阿里的EasyExcel。EasyExcel和EasyPoi有一定的相似之处。 EasyExcel和EasyPoi效率对比: 因为数据量少,从效率上看几乎没有差别,EasyExcel略胜一筹...

easypoi入门&amp;lt;1

学习自:http://www.afterturn.cn/doc/easypoi.html 开源地址:https://gitee.com/lemur/easypoi https://gitee.com/lemur/easypoi-spring-boot-starter maven项目,加入依赖 <dependency>...

EasyPoi 导入导出Excel时使用GroupName的踩坑解决过程

一、开发功能介绍: 简单的一个excel导入功能 二、Excel导入模板(大致模板没写全): 姓名 性别 生日 客户分类 联系人姓名 联系人部门  备注 材料 综合 采购 张三 男 1994/05/25 1 1 1 张三 开发部   李四 男 1994/05/25 1 1 1 张三 开发部   王五 男 1994/05/25 1 1 1...