easyexcel自定义样式

1、导入接口

导入工具类中
.registerWriteHandler(new OrgSheetWriteHandler()) 自定义拦截器进行设置

.registerWriteHandler(OrgSheetWriteHandler.getStyleStrategy()) 自定义内容、标题格式

.needHead(false) 不需要表头

	public static void exportOrgModelExcel(List<?> list, String sheetName, Class<?> pojoClass, String fileName,
								   HttpServletResponse response) {
		OutputStream out = null;
		try {
			if (fileName.indexOf(".") < 0) {
				throw new BizException("文件名必须带后缀");
			}
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("charset=UTF-8;application/form-data");
			response.setHeader("Content-Disposition",
					"attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1"));
			response.setHeader("Set-Cookie", "fileDownload=true; path=/");
			out = response.getOutputStream();
			EasyExcel.write(out, pojoClass).sheet(StringUtils.isEmpty(sheetName) ? "sheet0" : sheetName)
					// 宽度自适应
					.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
					// 第9列设置为文本格式
					.registerWriteHandler(new OrgSheetWriteHandler())
					// 自定义全表的样式
					.registerWriteHandler(OrgSheetWriteHandler.getStyleStrategy())
					// 不需要表头
					.needHead(false)
					.doWrite(list);
		} catch (IOException e) {
			throw new BizException(e);
		} finally {
			if (out != null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					logger.error("输出流关闭失败!", e);
				}
			}
		}
	}

2、拦截器代码

public class OrgSheetWriteHandler implements SheetWriteHandler  {

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

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        // 设置为文本格式
        SXSSFSheet sxssfSheet = (SXSSFSheet) writeSheetHolder.getSheet();
        CellStyle cellStyle = writeWorkbookHolder.getCachedWorkbook().createCellStyle();

        // 49为文本格式
        cellStyle.setDataFormat((short) 49);
        // 8为列,一整列设置为文本格式(从0开始)
        sxssfSheet.setDefaultColumnStyle(1, cellStyle);
        sxssfSheet.setDefaultColumnStyle(8, cellStyle);
        sxssfSheet.setDefaultColumnStyle(13, cellStyle);
        sxssfSheet.setDefaultColumnStyle(14, cellStyle);
    }

    public static HorizontalCellStyleStrategy getStyleStrategy() {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 设置对齐
        //headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        // 背景色, 设置为绿色,也是默认颜色
        //headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        // 字体
        //WriteFont headWriteFont = new WriteFont();
        //headWriteFont.setFontHeightInPoints((short) 12);
        //headWriteCellStyle.setWriteFont(headWriteFont);

        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        contentWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
//
//        // 字体策略
//        WriteFont contentWriteFont = new WriteFont();
//        //contentWriteFont.setFontHeightInPoints((short) 12);
//        contentWriteCellStyle.setWriteFont(contentWriteFont);
//        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
//        //设置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//        //设置 水平居中
//        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//        //设置边框样式
//        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
//        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        return horizontalCellStyleStrategy;
    }
}

3、调用工具类

List<OrgStructureExportVO> restResult = orgStructureService.downloadOrg(uuid);
EasyExcelUtils.exportOrgModelExcel(restResult, "组织机构数据", OrgStructureExportVO.class, "组织机构数据.xlsx", response);

你可能感兴趣的:(java,开发语言)