top.coos.poi.excel.ExcelReader Maven / Gradle / Ivy
package top.coos.poi.excel;
import java.io.Closeable;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import top.coos.core.bean.BeanUtil;
import top.coos.core.collection.CollUtil;
import top.coos.core.collection.IterUtil;
import top.coos.core.io.FileUtil;
import top.coos.core.io.IoUtil;
import top.coos.core.lang.Assert;
import top.coos.core.map.MapUtil;
import top.coos.poi.excel.editors.TrimEditor;
import top.coos.util.StringUtil;
/**
* Excel读取器
* 读取Excel工作簿
*
* @since 3.1.0
*/
public class ExcelReader implements Closeable {
/** 是否被关闭 */
private boolean isClosed;
/** 工作簿 */
private Workbook workbook;
/** Excel中对应的Sheet */
private Sheet sheet;
/** 是否忽略空行 */
private boolean ignoreEmptyRow = true;
/** 单元格值处理接口 */
private CellEditor cellEditor;
/** 标题别名 */
private Map headerAlias = new HashMap<>();
// ------------------------------------------------------------------------------------------------------- Constructor start
/**
* 构造
*
* @param excelFilePath Excel文件路径,绝对路径或相对于ClassPath路径
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(String excelFilePath, int sheetIndex) {
this(FileUtil.file(excelFilePath), sheetIndex);
}
/**
* 构造
*
* @param bookFile Excel文件
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(File bookFile, int sheetIndex) {
this(WorkbookUtil.loadBook(bookFile), sheetIndex);
}
/**
* 构造
*
* @param bookFile Excel文件
* @param sheetName sheet名,第一个默认是sheet1
*/
public ExcelReader(File bookFile, String sheetName) {
this(WorkbookUtil.loadBook(bookFile), sheetName);
}
/**
* 构造
*
* @param bookStream Excel文件的流
* @param sheetIndex sheet序号,0表示第一个sheet
* @param closeAfterRead 读取结束是否关闭流
*/
public ExcelReader(InputStream bookStream, int sheetIndex, boolean closeAfterRead) {
this(WorkbookUtil.loadBook(bookStream, closeAfterRead), sheetIndex);
}
/**
* 构造
*
* @param bookStream Excel文件的流
* @param sheetName sheet名,第一个默认是sheet1
* @param closeAfterRead 读取结束是否关闭流
*/
public ExcelReader(InputStream bookStream, String sheetName, boolean closeAfterRead) {
this(WorkbookUtil.loadBook(bookStream, closeAfterRead), sheetName);
}
/**
* 构造
*
* @param book {@link Workbook} 表示一个Excel文件
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(Workbook book, int sheetIndex) {
this(book.getSheetAt(sheetIndex));
this.workbook = book;
}
/**
* 构造
*
* @param book {@link Workbook} 表示一个Excel文件
* @param sheetName sheet名,第一个默认是sheet1
*/
public ExcelReader(Workbook book, String sheetName) {
this(book.getSheet(sheetName));
this.workbook = book;
}
/**
* 构造
*
* @param sheet Excel中的sheet
*/
public ExcelReader(Sheet sheet) {
Assert.notNull(sheet, "No Sheet provided.");
this.sheet = sheet;
}
// ------------------------------------------------------------------------------------------------------- Constructor end
// ------------------------------------------------------------------------------------------------------- Getters and Setters start
/**
* 获取读取的Workbook
*
* @return Workbook
* @since 4.0.0
*/
public Workbook getWorkbook() {
return this.workbook;
}
/**
* 获取此工作簿所有Sheet表
*
* @return sheet表列表
* @since 4.0.3
*/
public List getSheets() {
final int totalSheet = workbook.getNumberOfSheets();
List result = new ArrayList<>(totalSheet);
for (int i = 0; i < totalSheet; i++) {
result.add(this.workbook.getSheetAt(i));
}
return result;
}
/**
* 获取表名列表
*
* @return 表名列表
* @since 4.0.3
*/
public List getSheetNames() {
final int totalSheet = workbook.getNumberOfSheets();
List result = new ArrayList<>(totalSheet);
for (int i = 0; i < totalSheet; i++) {
result.add(this.workbook.getSheetAt(i).getSheetName());
}
return result;
}
/**
* 获取当前编辑的sheet
*
* @return sheet
* @since 4.0.0
*/
public Sheet getSheet() {
return this.sheet;
}
/**
* 是否忽略空行
*
* @return 是否忽略空行
*/
public boolean isIgnoreEmptyRow() {
return ignoreEmptyRow;
}
/**
* 设置是否忽略空行
*
* @param ignoreEmptyRow 是否忽略空行
* @return this
*/
public ExcelReader setIgnoreEmptyRow(boolean ignoreEmptyRow) {
this.ignoreEmptyRow = ignoreEmptyRow;
return this;
}
/**
* 设置单元格值处理逻辑
* 当Excel中的值并不能满足我们的读取要求时,通过传入一个编辑接口,可以对单元格值自定义,例如对数字和日期类型值转换为字符串等
*
* @param cellEditor 单元格值处理接口
* @return this
* @see TrimEditor
*/
public ExcelReader setCellEditor(CellEditor cellEditor) {
this.cellEditor = cellEditor;
return this;
}
/**
* 获得标题行的别名Map
*
* @return 别名Map
*/
public Map getHeaderAlias() {
return headerAlias;
}
/**
* 设置标题行的别名Map
*
* @param headerAlias 别名Map
* @return this
*/
public ExcelReader setHeaderAlias(Map headerAlias) {
this.headerAlias = headerAlias;
return this;
}
/**
* 增加标题别名
*
* @param header 标题
* @param alias 别名
* @return this
*/
public ExcelReader addHeaderAlias(String header, String alias) {
this.headerAlias.put(header, alias);
return this;
}
/**
* 去除标题别名
*
* @param header 标题
* @return this
*/
public ExcelReader removeHeaderAlias(String header) {
this.headerAlias.remove(header);
return this;
}
// ------------------------------------------------------------------------------------------------------- Getters and Setters end
/**
* 读取工作簿中指定的Sheet的所有行列数据
*
* @return 行的集合,一行使用List表示
*/
public List> read() {
return read(0);
}
/**
* 读取工作簿中指定的Sheet
*
* @param startRowIndex 起始行(包含,从0开始计数)
* @return 行的集合,一行使用List表示
* @since 4.0.0
*/
public List> read(int startRowIndex) {
return read(startRowIndex, Integer.MAX_VALUE);
}
/**
* 读取工作簿中指定的Sheet
*
* @param startRowIndex 起始行(包含,从0开始计数)
* @param endRowIndex 结束行(包含,从0开始计数)
* @return 行的集合,一行使用List表示
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public List> read(int startRowIndex, int endRowIndex) {
checkNotClosed();
List> resultList = new ArrayList<>();
startRowIndex = Math.max(startRowIndex, sheet.getFirstRowNum());// 读取起始行(包含)
endRowIndex = Math.min(endRowIndex, sheet.getLastRowNum());// 读取结束行(包含)
boolean isFirstLine = true;
List rowList;
for (int i = startRowIndex; i <= endRowIndex; i++) {
rowList = readRow(i);
if (CollUtil.isNotEmpty(rowList) || false == ignoreEmptyRow) {
if (null == rowList) {
rowList = new ArrayList<>(0);
}
if (isFirstLine) {
isFirstLine = false;
if (MapUtil.isNotEmpty(headerAlias)) {
rowList = aliasHeader(rowList);
}
}
resultList.add(rowList);
}
}
return resultList;
}
/**
* 读取Excel为Map的列表,读取所有行,默认第一行做为标题,数据从第二行开始
* Map表示一行,标题为key,单元格内容为value
*
* @return Map的列表
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy