Java easyexcel 每个单元格都设置不同的背景色demo
时间: 2024-09-13 22:13:51 浏览: 98
easyExcel导出合并单元格策略
5星 · 资源好评率100%
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); // 设置边框宽度,可选
}
}
}
}
```
请注意,上述代码只是一个简单的示例,实际使用时可能需要根据具体的业务逻辑来生成颜色。
阅读全文