easypoi导出excel空模板表头加粗居中,有下拉选项,单元格是文本格式,解决输入的身份证号后三位为0问题,亲测

controller

    @SneakyThrows
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        List<Student> list = new ArrayList<>();
        ExportParams exportParams = new ExportParams(null, null, ExcelType.XSSF);
        exportParams.setStyle(SubExcelExportStylerDefaultImpl.class);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, list);
        CellStyle textStyle = workbook.createCellStyle();
        textStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("TEXT"));
        int numberOfSheets = workbook.getNumberOfSheets();
        for (int i = 0; i < numberOfSheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            short lastCellNum = sheet.getRow(0).getLastCellNum();
            for (short j = 0; j < lastCellNum; j++) {
                sheet.setDefaultColumnStyle(j, textStyle);
            }
        }
        ExcelUtil.createXssfSelected(workbook, new String[]{"男", "女"}, 2);
        ExcelUtil.downloadExcel(response, workbook, "学生表.xlsx");
    }

utils

SubExcelExportStylerDefaultImpl 类。

public class SubExcelExportStylerDefaultImpl extends AbstractExcelExportStyler
        implements IExcelExportStyler {

    private Workbook workbook;

    public SubExcelExportStylerDefaultImpl() {
    }

    public SubExcelExportStylerDefaultImpl(Workbook workbook) {
        this.workbook = workbook;
        super.createStyles(workbook);
    }

    @Override
    public CellStyle getTitleStyle(short color) {
        Font font = workbook.createFont();
        //设置粗体
        font.setBold(true);
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setFont(font);
        //下边框
        titleStyle.setBorderBottom(BorderStyle.THIN);
        //左边框
        titleStyle.setBorderLeft(BorderStyle.THIN);
        //上边框
        titleStyle.setBorderTop(BorderStyle.THIN);
        //右边框
        titleStyle.setBorderRight(BorderStyle.THIN);
        //水平居中
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        titleStyle.setWrapText(true);
        return titleStyle;
    }

    @Override
    public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setDataFormat(STRING_FORMAT);
        if (isWarp) {
            style.setWrapText(true);
        }
        return style;
    }

    @Override
    public CellStyle getHeaderStyle(short color) {
        CellStyle titleStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontHeightInPoints((short) 12);
        titleStyle.setFont(font);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return titleStyle;
    }

    @Override
    public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setDataFormat(STRING_FORMAT);
        if (isWarp) {
            style.setWrapText(true);
        }
        return style;
    }


}

ExcelUtil 类。

public class ExcelUtil {
    /**
     * 下载通用配置
     *
     * @param response  HttpServletResponse
     * @param workbook  Workbook
     * @param excelName excelName
     */
    public static void downloadExcel(HttpServletResponse response, Workbook workbook, String excelName) {
        try (OutputStream os = response.getOutputStream()) {
            response.reset();
            if (excelName == null) {
                excelName = UUID.randomUUID().toString();
            }
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition","inline;filename=" + URLEncoder.encode(excelName, "UTF-8"));
            workbook.write(os);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * XSSF
     * excel添加下拉数据校验
     *
     * @param workbook   哪个 sheet 页添加校验
     * @param dataSource 数据源数组
     * @param col        第几列校验(0开始)
     * @return
     */
    public static void createXssfSelected(Workbook workbook, String[] dataSource, int col) {
        Sheet sheet = workbook.getSheetAt(0);
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, col, col);
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createExplicitListConstraint(dataSource);
        DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
        //处理Excel兼容性问题
        if (dataValidation instanceof XSSFDataValidation) {
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        } else {
            dataValidation.setSuppressDropDownArrow(false);
        }
        dataValidation.setEmptyCellAllowed(true);
        dataValidation.setShowPromptBox(true);
        dataValidation.createPromptBox("提示", "只能选择下拉框里面的数据");
        sheet.addValidationData(dataValidation);
    }

}

VO

public class Student {

    @Excel(name = "名字(必填)")
    private String clientName;
    @Excel(name = "年龄(必填)")
    private String mobile;
    @Excel(name = "性别",replace = {"男_0","女_1"})
    private Integer gender;
    @Excel(name = "证件类型",replace = {"身份证_0","护照_1","港澳通行证_2","台胞证_3"})
    private Integer clientCertificateType;
    @Excel(name = "证件编码")
    private String clientCertificateCode;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只尘世中迷途小书童

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值