diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowAddBackgroundColorHandler.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowAddBackgroundColorHandler.java new file mode 100644 index 000000000..298849379 --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowAddBackgroundColorHandler.java @@ -0,0 +1,53 @@ +package com.alibaba.easyexcel.test.demo.write; + +import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.metadata.style.WriteCellStyle; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.FillPatternType; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * 为指定行添加背景颜色 + * @author raxcl + */ +public class CustomRowAddBackgroundColorHandler implements CellWriteHandler { + private final int colorIndex; + private final List rowList; + + + public CustomRowAddBackgroundColorHandler(int colorIndex, Integer ...row) { + this.colorIndex = colorIndex; + this.rowList = new ArrayList<>(); + rowList.addAll(Stream.of(row).collect(Collectors.toList())); + } + + @Override + public void afterCellDispose(CellWriteHandlerContext context) { + Cell cell = context.getCell(); + int rowIndex = cell.getRowIndex(); + + // 自定义样式处理 + // 当前事件会在 数据设置到poi的cell里面才会回调 + // 指定行设置背景颜色 + if (rowList.contains(rowIndex)) { + // 第一个单元格 + // 只要不是头 一定会有数据 当然fill的情况 可能要context.getCellDataList() ,这个需要看模板,因为一个单元格会有多个 WriteCellData + WriteCellData cellData = context.getFirstCellData(); + // 这里需要去cellData 获取样式 + // 很重要的一个原因是 WriteCellStyle 和 dataFormatData绑定的 简单的说 比如你加了 DateTimeFormat + // ,已经将writeCellStyle里面的dataFormatData 改了 如果你自己new了一个WriteCellStyle,可能注解的样式就失效了 + // 然后 getOrCreateStyle 用于返回一个样式,如果为空,则创建一个后返回 + WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); + //writeCellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); + writeCellStyle.setFillForegroundColor((short) this.colorIndex); + // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND + writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); + } + } +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowSetPercentageFormatHandler.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowSetPercentageFormatHandler.java new file mode 100644 index 000000000..267594a21 --- /dev/null +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowSetPercentageFormatHandler.java @@ -0,0 +1,42 @@ +package com.alibaba.easyexcel.test.demo.write; + +import com.alibaba.excel.metadata.data.DataFormatData; +import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.metadata.style.WriteCellStyle; +import org.apache.poi.ss.usermodel.Cell; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 为指定行设置百分比格式 + * @author raxcl + */ +public class CustomRowSetPercentageFormatHandler implements CellWriteHandler { + private final List rowList; + + public CustomRowSetPercentageFormatHandler(Integer... rows) { + this.rowList = new ArrayList<>(); + Collections.addAll(this.rowList, rows); + } + + @Override + public void afterCellDispose(CellWriteHandlerContext context) { + // 获取当前单元格 + Cell cell = context.getCell(); + int rowIndex = cell.getRowIndex(); + + // 如果是我们要设置的行 + if (rowList.contains(rowIndex)) { + WriteCellData cellData = context.getFirstCellData(); + WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); + DataFormatData dataFormatData = new DataFormatData(); + dataFormatData.setIndex((short)10); + // 设置百分比 + writeCellStyle.setDataFormatData(dataFormatData); + } + } +} diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 0e7cf2db1..cff3a6e7a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -33,6 +33,7 @@ import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.metadata.WriteSheet; @@ -769,4 +770,34 @@ private List data() { return list; } + /** + * 为指定行标记背景颜色、为指定行转化数字格式为百分比格式 + */ + @Test + public void testLineWrite() { + String fileName = TestFileUtil.getPath() + "handlerStyleWrite" + System.currentTimeMillis() + ".xlsx"; + ExcelWriter excelWriter = EasyExcel.write(fileName).build(); + + //写入 + WriteSheet writeSheet1 = EasyExcel.writerSheet(0).build(); + List cellWriteHandlerList = new ArrayList<>(); + //指定行设置背景颜色 + cellWriteHandlerList.add(new CustomRowAddBackgroundColorHandler(IndexedColors.GREY_25_PERCENT.getIndex(),1, 9, 13, 17, 22)); + // 指定行设置百分比格式 + cellWriteHandlerList.add(new CustomRowSetPercentageFormatHandler(2, 10, 14, 18, 23)); + writeSheet1.setCustomWriteHandlerList(cellWriteHandlerList); + // 分页去数据库查询数据 这里可以去数据库查询每一页的数据 + List data = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + ConverterData converterData = new ConverterData(); + converterData.setString("字符串" + i); + converterData.setDate(new Date()); + converterData.setDoubleData(0.56); + data.add(converterData); + } + excelWriter.fill(data, writeSheet1); + } + + + }