easyexcel 动态表头+对象 导出数据

elcex8rz  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(485)

动态表头+对象注解 导出后数据没有对应

是否可以将动态表头和对象注解 @ExcelProperty("行政区域代码") 一一对应然后再写入数据

yfjy0ee7

yfjy0ee71#

自己按照你的表头的顺序把数据拼好就行了

fxnxkyjh

fxnxkyjh2#

我的是这样写的,动态表头(非注解)+相同合并内容。

 package com.ess.framework.commons.easyexcel;

import com.alibaba.excel.metadata.CellData;
 import com.alibaba.excel.metadata.Head;
 import com.alibaba.excel.write.handler.CellWriteHandler;
 import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
 import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**

* excel导出数据内容单元格合并规则
 */
 public class ExcelFileCellMergeStrategy implements CellWriteHandler {

/**

* 合并列的范围索引
*/
 private int[] mergeColumnIndex;
 /**
* 合并起始行索引
 */
 private int mergeRowIndex;

public ExcelFileCellMergeStrategy(int[] mergeColumnIndex, int mergeRowIndex) {
 this.mergeColumnIndex = mergeColumnIndex;
 this.mergeRowIndex = mergeRowIndex;
 }

@OverRide
 public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

}

@OverRide
 public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

}

@OverRide
 public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

}

@OverRide
 public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
 //当前行
 int curRowIndex = cell.getRowIndex();
 //当前列
 int curColIndex = cell.getColumnIndex();
 if (curRowIndex > mergeRowIndex) {
 for (int columnIndex : mergeColumnIndex) {
 if (curColIndex == columnIndex) {
 mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
 break;
 }
 }
 }
 }

/**

* 当前单元格向上合并
* @param writeSheetHolder
* @param cell
* @param curRowIndex
* @param curColIndex
 */
 private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
 //获取当前行的当前列的数据和上一行的当前列数据,通过上一行数据是否相同进行合并
 Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
 Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
 Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();

//比较当前行的第一列的单元格与上一行是否相同,相同合并当前单元格与上一行
 if (curData.equals(preData)) {
 Sheet sheet = writeSheetHolder.getSheet();
 List mergedRegions = sheet.getMergedRegions();
 boolean isMerged = false;
 for (int i = 0; i < mergedRegions.size() && !isMerged; i++) {
 CellRangeAddress cellAddresses = mergedRegions.get(i);
 //若上 一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
 if (cellAddresses.isInRange(curRowIndex - 1, curColIndex)) {
 sheet.removeMergedRegion(i);
 cellAddresses.setLastRow(curRowIndex);
 sheet.addMergedRegion(cellAddresses);
 isMerged = true;
 }
 }
 //若上一个单元格未被合并,则新增合并单元
 if (!isMerged) {
 CellRangeAddress cellAddresses = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
 sheet.addMergedRegion(cellAddresses);
 }
 }
 }
 }

相关问题