org.dromara.hutool.poi.excel.reader.AbstractSheetReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-poi Show documentation
Show all versions of hutool-poi Show documentation
Hutool POI工具类(对Office文档、OFD等操作)
/*
* Copyright (c) 2023 looly([email protected])
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.poi.excel.reader;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.RowUtil;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.apache.poi.ss.usermodel.Sheet;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 抽象{@link Sheet}数据读取实现
*
* @param 读取类型
* @author looly
* @since 5.4.4
*/
public abstract class AbstractSheetReader implements SheetReader {
/**
* 读取起始行(包含,从0开始计数)
*/
protected final int startRowIndex;
/**
* 读取结束行(包含,从0开始计数)
*/
protected final int endRowIndex;
/**
* 是否忽略空行
*/
protected boolean ignoreEmptyRow = true;
/**
* 单元格值处理接口
*/
protected CellEditor cellEditor;
/**
* 标题别名
*/
private Map headerAlias;
/**
* 构造
*
* @param startRowIndex 起始行(包含,从0开始计数)
* @param endRowIndex 结束行(包含,从0开始计数)
*/
public AbstractSheetReader(final int startRowIndex, final int endRowIndex) {
this.startRowIndex = startRowIndex;
this.endRowIndex = endRowIndex;
}
/**
* 设置单元格值处理逻辑
* 当Excel中的值并不能满足我们的读取要求时,通过传入一个编辑接口,可以对单元格值自定义,例如对数字和日期类型值转换为字符串等
*
* @param cellEditor 单元格值处理接口
*/
public void setCellEditor(final CellEditor cellEditor) {
this.cellEditor = cellEditor;
}
/**
* 设置是否忽略空行
*
* @param ignoreEmptyRow 是否忽略空行
*/
public void setIgnoreEmptyRow(final boolean ignoreEmptyRow) {
this.ignoreEmptyRow = ignoreEmptyRow;
}
/**
* 设置标题行的别名Map
*
* @param headerAlias 别名Map
*/
public void setHeaderAlias(final Map headerAlias) {
this.headerAlias = headerAlias;
}
/**
* 增加标题别名
*
* @param header 标题
* @param alias 别名
*/
public void addHeaderAlias(final String header, final String alias) {
Map headerAlias = this.headerAlias;
if (null == headerAlias) {
headerAlias = new LinkedHashMap<>();
}
this.headerAlias = headerAlias;
this.headerAlias.put(header, alias);
}
/**
* 转换标题别名,如果没有别名则使用原标题,当标题为空时,列号对应的字母便是header
*
* @param headerList 原标题列表
* @return 转换别名列表
*/
protected List aliasHeader(final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy