EasyPoi 导入校验使用

EasyPoi 导入校验使用

因工作需要,使用easypoi导入表格,并进行校验,将表格中有问题的地方,给出提示信息,以表格形式返回.

本篇,直接讲述Excel导入校验,基本介绍后续补上.

1 基于Springboot的easypoi导入表格校验

1 pom.xml

        <!--easypoi与SpringBoot的整合包-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.2</version>
        </dependency>

2 实体类DTO

/**
 * @author Chengfei
 * @description 手机类
 * @date 2021/1/30
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PhoneDTO {

    @Max(value = 15,message = "最大不能超过15")
    @Min(value = 3,message = "最小不能小于3")
    @Excel(name = "商品id", width = 15,orderNum = "10")
    @NotNull(message = "商品id不能为空哦!!!")
    private int id;

    @Length(max = 2)
    @Excel(name = "手机名", width = 15,orderNum = "20")
    @NotNull(message = "用户名不能为空哦!!")
    private String phoneName;

    @Excel(name = "价格", width = 15, orderNum = "30")
    @NotNull(message = "价格不能为空!!")
    @Digits(integer = 3,fraction = 2,message = "整数位最多3位,小数位最多2位")
    @DecimalMin(value = "0",message = "成本不能为负数")
    private BigDecimal cost;

    @Excel(name = "时间", exportFormat = "yyyy-MM-dd HH:mm:ss", importFormat = "yyyy-MM-dd HH:mm:ss", width = 25, orderNum = "40")
    private LocalDateTime createTime;

/*
	String类型可以使用正则校验
    @Pattern(regexp = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$" ,message = "必须为金额数值")
      private String money;
*/

}

3 工具类

public class ExcelUtils {
    /**
     * excel 导出
     *
     * @param list      数据
     * @param title     标题
     * @param sheetName sheet名称
     * @param pojoClass pojo类型
     * @param fileName  文件名称
     * @param response
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException {
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
    }
    
        /**
     * 默认的 excel 导出
     *
     * @param list         数据
     * @param pojoClass    pojo类型
     * @param fileName     文件名称
     * @param response
     * @param exportParams 导出参数
     */
    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downLoadExcel(fileName, response, workbook);
    }
    
        /**
     * 下载
     *
     * @param fileName 文件名称
     * @param response
     * @param workbook excel数据
     */
    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
    
    /**
     * Excel 类型枚举
     */
    enum ExcelTypeEnum {
        XLS("xls"), XLSX("xlsx");
        private String value;

        ExcelTypeEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

}

4 自定义校验类

/**
 * @author Chengfei
 * @description
 * @date 2021/1/30
 */
@Component
public class PhoneExcelverifiyName implements IExcelVerifyHandler<PhoneDTO> {

    //保存表格中的手机名称
    private static  Set<String> stringSet = new HashSet<>();

    @Override
    public ExcelVerifyHandlerResult verifyHandler(PhoneDTO phoneDTO) {

        //设置默认验证为true
        ExcelVerifyHandlerResult excelVerifyHandlerResult = new ExcelVerifyHandlerResult(true);
        if (StringUtils.isNotBlank(phoneDTO.getPhoneName())) {
            stringSet.add(phoneDTO.getPhoneName());
            StringBuilder str = new StringBuilder();

            //校验表格中手机名称是否一致, 不一致,给出错误提示
            if (stringSet.size()>1){
                excelVerifyHandlerResult.setSuccess(false);
                str.append("手机名称不一致 ");
            }

            // 手机名为小米 则为重复录入
            if ("小米".equals(phoneDTO.getPhoneName())) {

                if (excelVerifyHandlerResult.isSuccess()){
                    excelVerifyHandlerResult.setSuccess(false);
                    str.append("手机名称重复");
                }else {
                    str.append(",手机名称重复");
                }
            }

            excelVerifyHandlerResult.setMsg(str.toString());

        }
        return excelVerifyHandlerResult;
    }
}

5 controller控制层

/**
 * @author Chengfei
 * @description
 * @date 2021/1/30
 */
@Controller
@RequestMapping("/phone")
public class PhoneController {

    /**
     * 导出1  使用网上excel导出导入工具类  无需校验的导入导出
     *
     * @param response
     */
    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response) throws IOException {

        List<PhoneDTO> phoneDTOList = new ArrayList<>();
        phoneDTOList.add(new PhoneDTO(6,"小米",new BigDecimal(33.00),LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(2,"小米",new BigDecimal(33.00),LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(18,"小米米米米米米",new BigDecimal(33.00),LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6,null,new BigDecimal(-1.02),LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6,"小米",new BigDecimal(3322.00),LocalDateTime.now()));
        phoneDTOList.add(new PhoneDTO(6,"小米",new BigDecimal(33.0067),LocalDateTime.now()));

        ExcelUtils.exportExcel(phoneDTOList, "手机信息表", "手机信息", PhoneDTO.class, "手机信息", response);
    }


    @Autowired
    private PhoneExcelverifiyName phoneExcelverifiyName;
    /**
     * 文件校验导入
     *
     * @return
     */
    @RequestMapping("/import")
    public Object upload(MultipartFile file,HttpServletResponse resp) throws Exception {
        //导入的基本配置
        ImportParams params = new ImportParams();
        //表头一行
        params.setHeadRows(1);
        //标题一行
        params.setTitleRows(1);
        //代表导入这里是需要验证的(根据字段上的注解校验)
        params.setNeedVerify(true);
        //设及一个自定义校验 (自定义校验名字不可重复)
        params.setVerifyHandler(phoneExcelverifiyName);
        //使用框架自身导入工具
        ExcelImportResult<PhoneDTO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), PhoneDTO.class, params);
        //导入成功的数据
        List<PhoneDTO> list = result.getList();
        //失败结果集
        List<PhoneDTO> failList = result.getFailList();
        //拿到导出失败的工作簿
        Workbook failWorkbook = result.getFailWorkbook();



        //验证是否有失败的数据
        if (result.isVerifyFail()) {

            ServletOutputStream fos = resp.getOutputStream();
            //mime类型
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            resp.setHeader("Content-disposition", "attachment;filename=error.xlsx");
            result.getFailWorkbook().write(fos);
            fos.close();
        }
        return failList;
    }
    
}

6 结果展示

http://localhost:8080/phone/export

image-20210131215603988

http://localhost:8080/phone/import把下载文件上传

image-20210131215649546

7 说明

要求对导入的表格字段进行校验,并将错误的列,给出提示返回,如没有错误,返回表格内容.

要求:

  • 手机名为小米,则提示重复导入
  • 手机名不一致,提示手机名称不一致
  • 手机名不能为空
  • 商品id,大于等于3,小于等于15,且不为空
  • 价格中整数最多3位,小数最多2位,且必须为数字类型
EasyPOI是一个基于Java的Excel导入导出库,支持Word和Excel的读写,提供了基于注解和API两种使用方式。在4.4.0版本中,引入了使用字典(Map)进行导入导出的功能,这为数据处理提供了更多灵活性。 使用字典进行Excel导入导出,通常意味着你可以将数据以键值对的形式组织,其中键(Key)代表Excel中的列名或标题,值(Value)代表单元格的数据。这种方式简化了数据的处理流程,特别是当数据结构不是很固定时,可以动态地映射到对应的Excel列。 下面是一个简单的例子,展示了如何使用字典进行Excel的导出: ```java import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl; import cn.afterturn.easypoi.excel.handler.impl.DefaultExcelExportHandler; import java.util.*; public class EasyPoiDemo { public static void main(String[] args) { // 创建字典对象,模拟数据库中的数据 Map<String, Object> data = new HashMap<>(); data.put("name", "张三"); data.put("age", "20"); data.put("address", "北京市"); // 创建Excel导出参数 List<ExcelExportEntity> params = new ArrayList<>(); params.add(new ExcelExportEntity("姓名", "name")); params.add(new ExcelExportEntity("年龄", "age")); params.add(new ExcelExportEntity("地址", "address")); // 创建导出工具对象 DefaultExcelExportHandler handler = new DefaultExcelExportHandler(); // 进行导出操作 handler.export(params, data, ExcelExportStylerDefaultImpl.class, System.out, "导出文件名.xlsx"); } } ``` 以上代码创建了一个简单的字典对象,并设置了三个键值对,模拟从数据库或其他数据源获取的数据。然后定义了导出参数,指定了Excel文件中各列的标题和对应的数据字段。最后,使用`DefaultExcelExportHandler`类的`export`方法将数据导出到Excel文件中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值