This commit is contained in:
Looly
2020-08-16 11:55:30 +08:00
parent ef235aab5d
commit a444d00e98
6 changed files with 56 additions and 25 deletions

View File

@@ -511,12 +511,12 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @return 转换别名列表
*/
private List<String> aliasHeader(List<Object> headerList) {
final int size = headerList.size();
final ArrayList<String> result = new ArrayList<>(size);
if (CollUtil.isEmpty(headerList)) {
return result;
if(CollUtil.isEmpty(headerList)){
return new ArrayList<>(0);
}
final int size = headerList.size();
final ArrayList<String> result = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
result.add(aliasHeader(headerList.get(i), i));
}

View File

@@ -1,27 +1,27 @@
package cn.hutool.poi.excel;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.CellEditor;
import cn.hutool.poi.excel.cell.CellUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.CellEditor;
import cn.hutool.poi.excel.cell.CellUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Excel中的行{@link Row}封装工具类
*
*
* @author looly
* @since 4.0.7
*/
public class RowUtil {
/**
* 获取已有行或创建新行
*
* @param sheet Excel表
*
* @param sheet Excel表
* @param rowIndex 行号
* @return {@link Row}
* @since 4.0.2
@@ -36,23 +36,38 @@ public class RowUtil {
/**
* 读取一行
*
* @param row 行
*
* @param row
* @param cellEditor 单元格编辑器
* @return 单元格值列表
*/
public static List<Object> readRow(Row row, CellEditor cellEditor) {
return readRow(row, 0, Short.MAX_VALUE, cellEditor);
}
/**
* 读取一行
*
* @param row 行
* @param startCellNumInclude 起始单元格号0开始包含
* @param endCellNumInclude 结束单元格号0开始包含
* @param cellEditor 单元格编辑器
* @return 单元格值列表
*/
public static List<Object> readRow(Row row, int startCellNumInclude, int endCellNumInclude, CellEditor cellEditor) {
if (null == row) {
return new ArrayList<>(0);
}
final short length = row.getLastCellNum();
if (length < 0) {
return new ArrayList<>(0);
final short rowLength = row.getLastCellNum();
if (rowLength < 0) {
return ListUtil.empty();
}
final List<Object> cellValues = new ArrayList<>(length);
final int size = Math.min(endCellNumInclude + 1, rowLength);
final List<Object> cellValues = new ArrayList<>(size);
Object cellValue;
boolean isAllNull = true;
for (short i = 0; i < length; i++) {
for (int i = startCellNumInclude; i < size; i++) {
cellValue = CellUtil.getCellValue(row.getCell(i), cellEditor);
isAllNull &= StrUtil.isEmptyIfStr(cellValue);
cellValues.add(cellValue);
@@ -60,16 +75,16 @@ public class RowUtil {
if (isAllNull) {
// 如果每个元素都为空,则定义为空行
return new ArrayList<>(0);
return ListUtil.empty();
}
return cellValues;
}
/**
* 写一行数据
*
* @param row 行
* @param rowData 一行的数据
*
* @param row
* @param rowData 一行的数据
* @param styleSet 单元格样式集,包括日期等样式
* @param isHeader 是否为标题行
*/