EasyExcel v2.1.6单元格样式设置

使用AlibabaEasyExcel v2.1.6导出excel文件,的确很方便。不过,想要设置一下单元格样式,看了官网文档,没找到。看了几篇网友的文章,综合了一下。分享出来。如果你也有类似的需求,也许可以少走一些弯路。
摘抄了一个网友的自定义注解方法,感觉很方便。在实际调试过程中发现,如果样式自定义方法与样式自定义注解发生冲突,注解会失效,输出excel文件中的格式是自定义方法中的。方法和注解结合起来设置样式,感觉很舒适。上代码。

1.实体类代码

@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_demo_data")
@ContentRowHeight(20)
@HeadRowHeight(30)
public class DownloadData extends Model<DownloadData> implements Serializable {
    private static final long serialVersionUID = 279957714570873289L;
    @ExcelProperty("字符串标题")
    @ColumnWidth(30)
    private String string;

    @ExcelProperty("日期标题")
    @ColumnWidth(46)
    private Date date;

    @ExcelProperty("数字标题")
    @ColumnWidth(20)
    @ExcelStyle(fontName = "黑体", horizontalAlignment = HorizontalAlignment.RIGHT)
    private Double doubleData;

}

2.注解类

/**
 * @ClassName ExcelStyle
 * @Description easyExcel-v2.1,自定义注解,实现不同单元格格式的自定义
 * @Author Lx
 * @Date 2020.03.26  15:53
 * @Version 1.0
 **/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelStyle {
    String fontName() default "宋体";
    short fontHeightInPoints() default 12;
    HorizontalAlignment horizontalAlignment() default HorizontalAlignment.LEFT;
    VerticalAlignment verticalAlignment() default VerticalAlignment.CENTER;
}

3.自定义注解类实现


/**
 * @ClassName ExcelStyleAnnotationCellWriteHandler
 * @Description 通过自定义注解的方式实现。因为我们可以获取到field的name,
 * 那么就会想到反射,只要给到field的name的class,
 * 就可以获取到Field。通过反射获取到field上的注解,给field设置单元格格式
 * @Author Lx
 * @Date 2020.03.26  15:59
 * @Version 1.0
 **/

@Slf4j
public class ExcelStyleAnnotationCellWriteHandler extends HorizontalCellStyleStrategy {
    private Class c;
    ExcelStyleAnnotationCellWriteHandler(Class c, WriteCellStyle headWriteCellStyle,
                                         WriteCellStyle contentWriteCellStyle) {
        super(headWriteCellStyle, contentWriteCellStyle);
        this.c = c;
    }
    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        try {
            Field declaredField = c.getDeclaredField(head.getFieldName());
            ExcelStyle annotation = declaredField.getAnnotation(ExcelStyle.class);
            if (annotation != null) {
                Workbook wb = cell.getSheet().getWorkbook();
                CellStyle cellStyle = wb.createCellStyle();
                Font font = wb.createFont();
                font.setFontName(annotation.fontName());
                font.setFontHeightInPoints(annotation.fontHeightInPoints());
                cellStyle.setFont(font);
                cellStyle.setAlignment(annotation.horizontalAlignment());
                cellStyle.setVerticalAlignment(annotation.verticalAlignment());
                cell.setCellStyle(cellStyle);
            }else {
                super.setContentCellStyle(cell,head,relativeRowIndex);
            }
        } catch (NoSuchFieldException e) {
            log.error("ExcelStyleAnnotationCellWriteHandler error{0}",e);
        }
    }
}

4.导出Excel文件实现类


/**
 * @ClassName DownloadServiceImpl
 * @Description 导出excel方法实现类。
 * @Author Lx
 * @Date 2020.03.25  11:01
 * @Version 1.0
 **/
@Service
public class DownloadServiceImpl extends ServiceImpl<DownloadMapper,DownloadData>
        implements DownloadService {
    @Resource
    DownloadService downloadService;
    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), DownloadData.class)
                    .autoCloseStream(Boolean.FALSE).sheet("模板")
                     //自定义方法输出样式设置。可以不写,就默认缺省。
                    .registerWriteHandler(myHorizontalCellStyleStrategy())
                    //自定义注解excel单元格样式,在字段上注解。
                    //自定义注解如果与自定义方法冲突,则自定义方法优先,自定义注解失效。
                    .registerWriteHandler(myAnnotHoriCellStyleStrategy())
                    .doWrite(data());
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>(8);
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }

    private HorizontalCellStyleStrategy myAnnotHoriCellStyleStrategy(){
        return  new ExcelStyleAnnotationCellWriteHandler(DownloadData.class,
                               new WriteCellStyle(), new WriteCellStyle());
    }

    private HorizontalCellStyleStrategy myHorizontalCellStyleStrategy(){
        //表头样式策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //表头前景设置淡蓝色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteFont.setFontHeightInPoints((short)12);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        //内容字体大小
        contentWriteFont.setFontName("宋体");
        contentWriteFont.setFontHeightInPoints((short)11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //设置自动换行
        contentWriteCellStyle.setWrapped(true);
        //设置垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置水平靠左
        //contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        //设置边框样式
        setBorderStyle(contentWriteCellStyle);
        //内容风格可以定义多个。
        List<WriteCellStyle>   listCntWritCellSty =  new ArrayList<>();
        listCntWritCellSty.add(contentWriteCellStyle);
        WriteCellStyle contentWriteCellStyle2 = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色。
        // 头默认了 FillPatternType所以可以不指定。
        contentWriteCellStyle2.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        contentWriteCellStyle2.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        //设置垂直居中
        contentWriteCellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置边框样式
        setBorderStyle(contentWriteCellStyle2);
        listCntWritCellSty.add(contentWriteCellStyle2);
        // 水平单元格风格综合策略(表头 + 内容)
       // return  new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        return  new HorizontalCellStyleStrategy(headWriteCellStyle, listCntWritCellSty);
    }

    private void setBorderStyle(WriteCellStyle contentWriteCellStyle){
        //设置边框样式
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        // contentWriteCellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex()); //颜色
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
    }

    private List<DownloadData> data() {
        List<DownloadData> list = downloadService.list();
        System.out.println(list);
        return list;
    }
}

数据库操作使用的是myBatisPlus 3.2 , mysql 8.0。
开发平台 idea 2018.3。
系统框架 SpringBoot 2.0。

EasyExcel是一个基于Java的简单、快速、占用内存小的Excel处理工具,它可以方便地生成和解析Excel文件。虽然EasyExcel本身并不直接提供设置每个单元格不同背景色的功能,但是可以通过编写自定义的样式实现。 以下是一个简单的示例代码,展示如何在EasyExcel中为每个单元格设置不同的背景色: ```java import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFSimpleShape; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.util.HashMap; import java.util.Map; public class CellBackgroundFillDemo { public static void main(String[] args) { // 输出文件路径 String fileName = "CellBackgroundFillDemo.xlsx"; // 写入数据,每个单元格都有自己的背景色 Map<Integer, Map<Integer, XSSFColor>> cellBackgroundMap = new HashMap<>(); for (int sheetNo = 0; sheetNo < 2; sheetNo++) { Map<Integer, XSSFColor> rowBackgroundMap = new HashMap<>(); for (int rowNo = 0; rowNo < 10; rowNo++) { for (int colNo = 0; colNo < 10; colNo++) { // 这里设置不同的颜色,实际使用时可以根据业务逻辑动态生成 XSSFColor color = new XSSFColor(new java.awt.Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255))); rowBackgroundMap.put(colNo, color); } } cellBackgroundMap.put(sheetNo, rowBackgroundMap); } // 写入Excel EasyExcel.write(fileName) .registerWriteHandler(new CellBackgroundFillHandler(cellBackgroundMap)) .sheet("模板") .doWrite(null); } public static class CellBackgroundFillHandler implements CellWriteHandler { private Map<Integer, Map<Integer, XSSFColor>> cellBackgroundMap; public CellBackgroundFillHandler(Map<Integer, Map<Integer, XSSFColor>> cellBackgroundMap) { this.cellBackgroundMap = cellBackgroundMap; } @Override public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { // 获取当前单元格的行列号 Integer currentSheetNo = writeSheetHolder.getSheet().getSheetNum(); Integer currentRowNo = cell.getRowIndex(); Integer currentColNo = cell.getColumnIndex(); // 获取单元格对应的背景色 Map<Integer, XSSFColor> rowBackgroundMap = cellBackgroundMap.get(currentSheetNo); XSSFColor color = rowBackgroundMap.get(currentColNo); if (color != null) { // 为当前单元格设置背景色 Sheet sheet = writeSheetHolder.getSheet(); Drawing<?> drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex() + 1, cell.getRowIndex() + 1); XSSFSimpleShape shape = ((XSSFWorkbook) sheet.getWorkbook()).getCreationHelper().createSimpleShape((XSSFClientAnchor) anchor); shape.setFillColor(color); shape.setLineStyleColor(-16776961); // 设置边框颜色,可选 shape.setLineStyle(1); // 设置边框宽度,可选 } } } } ``` 请注意,上述代码只是一个简单的示例,实际使用时可能需要根据具体的业务逻辑来生成颜色。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值