EasyExcel合并列 EasyExcel合并 easyexcel合并 列 easyexcel合并
效果图
调用
public void test1(){
LinkedList<WorkstationMaintenancePerformanceDetailsVO> linkedList = getData();
Set<String> includeColumnFiledNames = new HashSet<String>();
includeColumnFiledNames.add("maintenanceDate");
includeColumnFiledNames.add("maintenanceName");
includeColumnFiledNames.add("workerLevel");
includeColumnFiledNames.add("modelId");
includeColumnFiledNames.add("model");
includeColumnFiledNames.add("difficulty");
includeColumnFiledNames.add("skillLevel");
includeColumnFiledNames.add("levelTargetNumber");
includeColumnFiledNames.add("maintenanceNumber");
includeColumnFiledNames.add("excessUnitPrice");
includeColumnFiledNames.add("taskProportion");
includeColumnFiledNames.add("dayCumulativeProportion");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(("维修绩效明细").getBytes(), StandardCharsets.ISO_8859_1) + ".xlsx");
try (OutputStream outputStream = response.getOutputStream()) {
EasyExcel.write(outputStream, WorkstationMaintenancePerformanceDetailsVO.class)
.registerWriteHandler(new WorkstationMaintenancePerformanceDetailsMergeStrategy(linkedList.size(), 0, 1))
.registerWriteHandler(horizontalCellStyleStrategyBuilder.horizontalCellStyleStrategyBuilder())
.includeColumnFiledNames(includeColumnFiledNames)
.sheet("维修绩效明细")
.doWrite(linkedList);
outputStream.flush();
} catch (IOException ignored) {
}
}
合并实现类
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class WorkstationMaintenancePerformanceDetailsMergeStrategy extends AbstractMergeStrategy {
private final Set<Integer> mergeCellIndex = new HashSet<>();
private final Map<Integer, MergeRange> lastRow = new HashedMap();
private Integer maxRow = 0;
private WorkstationMaintenancePerformanceDetailsMergeStrategy() {
}
public WorkstationMaintenancePerformanceDetailsMergeStrategy(Integer maxRow, int... mergeCellIndex) {
Arrays.stream(mergeCellIndex).forEach(this.mergeCellIndex::add);
this.maxRow = maxRow;
}
@Override
protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
int currentCellIndex = cell.getColumnIndex();
if (mergeCellIndex.contains(currentCellIndex)) {
String currentCellValue = cell.getStringCellValue();
int currentRowIndex = cell.getRowIndex();
if (!lastRow.containsKey(currentCellIndex)) {
lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
return;
}
MergeRange mergeRange = lastRow.get(currentCellIndex);
if (!(mergeRange.lastValue != null && mergeRange.lastValue.equals(currentCellValue))) {
if (mergeRange.startRow != mergeRange.endRow || mergeRange.startCell != mergeRange.endCell) {
sheet.addMergedRegionUnsafe(new CellRangeAddress(mergeRange.startRow, mergeRange.endRow, mergeRange.startCell, mergeRange.endCell));
}
lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
}
mergeRange.endRow += 1;
if (relativeRowIndex.equals(maxRow - 1)) {
MergeRange lastMergeRange = lastRow.get(currentCellIndex);
if (lastMergeRange.startRow != lastMergeRange.endRow || lastMergeRange.startCell != lastMergeRange.endCell) {
sheet.addMergedRegionUnsafe(new CellRangeAddress(lastMergeRange.startRow, lastMergeRange.endRow, lastMergeRange.startCell, lastMergeRange.endCell));
}
}
}
}
}
class MergeRange {
public int startRow;
public int endRow;
public int startCell;
public int endCell;
public String lastValue;
public MergeRange(String lastValue, int startRow, int endRow, int startCell, int endCell) {
this.startRow = startRow;
this.endRow = endRow;
this.startCell = startCell;
this.endCell = endCell;
this.lastValue = lastValue;
}
}
表格样式实现类
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
public class horizontalCellStyleStrategyBuilder {
private static HorizontalCellStyleStrategy cellStyleStrategy;
public static HorizontalCellStyleStrategy horizontalCellStyleStrategyBuilder() {
if (cellStyleStrategy == null) {
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 25);
headWriteFont.setColor(IndexedColors.WHITE.getIndex());
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 20);
contentWriteCellStyle.setWriteFont(contentWriteFont);
cellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
return cellStyleStrategy;
}
}