EasyExcel自定义表头导出模板并封装数据下拉选择

2022-08-09 09:39:59

首先查询可变数据

在这里插入图片描述

动态数据Controller

    @ApiOperation(value = " 模板导出")
    @GetMapping("/exportFarmerExcel")
    public void exportStudentExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("养殖户模板excel", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        //查询动态数据并封装成数组
        List<SysDictionary> sysDictionaryList = iSysDictDataService.selectDictByTypeAndChildren("farmers_type");
        String[] farmerTypeArray = sysDictionaryList.stream().map(SysDictionary::getDictLabel).toArray(String[]::new);
        EasyExcel.write(response.getOutputStream())
               .head(businessFarmerBasicInfoService.queryFieldTitleList())
                .registerWriteHandler(new FarmerSheetWriterHandler(farmerTypeArray))
                .sheet("养殖户模板").doWrite(null);
    }

head(businessFarmerBasicInfoService.queryFieldTitleList()) 未封装表头数据
farmerTypeArray 为动态查询的可变数据
FarmerSheetWriterHandler为定义模板数据时表格的下拉数据只允许表格固定单元格输入下拉数据

表头封装

    @Override
    public List<List<String>> queryFieldTitleList() {
        List<SysDictionary> sysDictionaryList = iSysDictDataService.selectDictByTypeAndChildren("livestock_type");
        List<List<String>> head = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        head0.add("畜主姓名");
        List<String> head1 = new ArrayList<>();
        head1.add("养殖户类型");
        List<String> head2 = new ArrayList<>();
        head2.add("手机号");
        List<String> head3 = new ArrayList<>();
        head3.add("人口数");
        head.add(head0);
        head.add(head1);
        head.add(head2);
        head.add(head3);
        List<String> head4;
        for (SysDictionary sysDictionary : sysDictionaryList) {
            String dictLabel = sysDictionary.getDictLabel();
            List<SysDictionary> children = sysDictionary.getChildren();
            if(ObjectUtils.isNotEmpty(children) && children.size() > 0){
                for (int i = 1; i <= children.size() ; i++){
                    head4 = new ArrayList<>();
                    String dictLabel1 = children.get(i-1).getDictLabel();
                    head4.add(dictLabel);
                    head4.add(dictLabel1);
                    head.add(head4);
                }
            }else {
                head4 = new ArrayList<>();
                head4.add(dictLabel);
                head.add(head4);
            }
        }
        return head;
    }

定义导出模板时的下拉数据

public class FarmerSheetWriterHandler implements SheetWriteHandler {

    private String[] farmerType;

    public FarmerSheetWriterHandler(String[] farmerType){
        this.farmerType = farmerType;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        //获取工作簿
        Sheet sheet = writeSheetHolder.getSheet();
        ///开始设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();
        //定义一个map key是需要添加下拉框的列的index value是下拉框数据
        Map<Integer, String[]> mapDropDown = new HashMap<>();
        mapDropDown.put(1,farmerType);
        //设置下拉框
        for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
            /*起始行、终止行、起始列、终止列  起始行为1即表示表头不设置**/
            CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey());

            /*设置下拉框数据**/
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);

            // 阻止输入非下拉选项的值
            dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
            dataValidation.setShowErrorBox(true);
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.createErrorBox("提示", "请选择下拉选项中的内容");
            sheet.addValidationData(dataValidation);
        }
    }
}
  • 作者:樊小樊
  • 原文链接:https://blog.csdn.net/qq_43419105/article/details/125538081
    更新时间:2022-08-09 09:39:59