Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增写入demo:为指定行标记背景颜色、为指定行转化数字格式为百分比格式 #3779

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Integer> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -769,4 +770,34 @@ private List<DemoData> 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<WriteHandler> 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<ConverterData> 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);
}



}