From aa9e9c88a8440447260fb6c3430f89595f253800 Mon Sep 17 00:00:00 2001 From: chenlong_DJ033979 Date: Mon, 22 Apr 2024 18:18:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E6=8C=87=E5=AE=9A=E8=A1=8C=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E8=83=8C=E6=99=AF=E9=A2=9C=E8=89=B2=E3=80=81=E4=B8=BA?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E8=A1=8C=E8=BD=AC=E5=8C=96=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=B8=BA=E7=99=BE=E5=88=86=E6=AF=94=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomRowAddBackgroundColorHandler.java | 53 +++++++++++++++++++ .../CustomRowSetPercentageFormatHandler.java | 42 +++++++++++++++ .../easyexcel/test/demo/write/WriteTest.java | 31 +++++++++++ 3 files changed, 126 insertions(+) create mode 100644 easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowAddBackgroundColorHandler.java create mode 100644 easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/CustomRowSetPercentageFormatHandler.java 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 00000000..29884937 --- /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 00000000..267594a2 --- /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 0e7cf2db..cff3a6e7 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.BooleanUtils; 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 @@ public class WriteTest { 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); + } + + + }