EasyExcel 单元格合并
easyexcel
快速、简洁、解决大文件内存溢出的java处理Excel工具
项目地址:https://gitcode.com/gh_mirrors/ea/easyexcel
·
1. 继承AbstractMergeStrategy,重写merge方法
参数介绍:
countryCount 合并行数集合。 如下图 list则为6,5。同理productTypeCount

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.List;
public class MyMergeStrategy extends AbstractMergeStrategy {
private Sheet sheet;
private List<Integer> countryCount;
private List<Integer> productTypeCount;
public MyMergeStrategy(List<Integer> countryCount,List<Integer> productTypeCount) {
this.countryCount = countryCount;
this.productTypeCount = productTypeCount;
}
// 按照分组将各种类别分别合并成一个单元格
private void mergeCountryColumn(Integer index) {
Integer rowCnt = 1;
for (Integer count : countryCount) {
if (count == 1) {
rowCnt += count;
continue;
}
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, rowCnt + count - 1, index, index);
sheet.addMergedRegionUnsafe(cellRangeAddress);
rowCnt += count;
}
}
// 按照分组将各种类别分别合并成一个单元格
private void mergeProductTypeColumn(Integer index) {
Integer rowCnt = 1;
for (Integer count : productTypeCount) {
if (count == 1) {
rowCnt += count;
continue;
}
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt, rowCnt + count - 1, index, index);
sheet.addMergedRegionUnsafe(cellRangeAddress);
rowCnt += count;
}
}
@Override
protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
this.sheet = sheet;
if (cell.getRowIndex() == 1) {
switch (cell.getColumnIndex()) {
case 0:
this.mergeCountryColumn(0);
break;
case 1:
this.mergeProductTypeColumn(1);
break;
default:
break;
}
}
}
}
2. Excel导出
EasyExcel.write(response.getOutputStream(), clazz).registerWriteHandler(abstractMergeStrategy).excelType(ExcelTypeEnum.XLSX).sheet(sheetName);
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:4 个月前 )
c42183df
Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io`
1 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)