com.ludii.excel.parse.AbstractExcelImportCellValueReader Maven / Gradle / Ivy
package com.ludii.excel.parse;
import com.ludii.excel.exceptions.ExcelException;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author 陆迪
* @date 2022/3/24
*/
public abstract class AbstractExcelImportCellValueReader implements ExcelImportCellValueReader {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected final static String EMPTY_STRING = "";
@Override
public List getHeaderValueList() {
int headerColumnIndex = getHeaderColumnIndex();
if (headerColumnIndex < 0) {
throw new ExcelException("请设置正确的标题所在行");
}
Row row = this.getRow(headerColumnIndex);
if (row == null) {
throw new ExcelException("不存在的标题行");
}
List headerValueList = new ArrayList<>();
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
Object cellValue = this.getCellValue(row, j);
headerValueList.add(Objects.toString(cellValue));
}
log.debug("标题行:{}", headerValueList);
return headerValueList;
}
@Override
public List> getCellValueListList() {
List> cellValueListList = new ArrayList<>();
int dataRowStartIndex = this.getDataRowStartIndex();
int dataRowEndIndex = this.getDataRowEndIndex();
for (int i = dataRowStartIndex; i <= dataRowEndIndex; i++) {
Row row = getRow(i);
if (row == null) {
continue;
}
List