org.dromara.hutool.poi.excel.ExcelReader 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;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.func.SerBiConsumer;
import org.dromara.hutool.poi.excel.cell.CellEditor;
import org.dromara.hutool.poi.excel.cell.CellUtil;
import org.dromara.hutool.poi.excel.reader.BeanSheetReader;
import org.dromara.hutool.poi.excel.reader.ColumnSheetReader;
import org.dromara.hutool.poi.excel.reader.ListSheetReader;
import org.dromara.hutool.poi.excel.reader.MapSheetReader;
import org.dromara.hutool.poi.excel.reader.SheetReader;
import org.apache.poi.ss.extractor.ExcelExtractor;
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 java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Excel读取器
* 读取Excel工作簿
*
* @author Looly
* @since 3.1.0
*/
public class ExcelReader extends ExcelBase {
/**
* 是否忽略空行
*/
private boolean ignoreEmptyRow = true;
/**
* 单元格值处理接口
*/
private CellEditor cellEditor;
// ------------------------------------------------------------------------------------------------------- Constructor start
/**
* 构造
*
* @param excelFilePath Excel文件路径,绝对路径或相对于ClassPath路径
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(final String excelFilePath, final int sheetIndex) {
this(FileUtil.file(excelFilePath), sheetIndex);
}
/**
* 构造
*
* @param excelFilePath Excel文件路径,绝对路径或相对于ClassPath路径
* @param sheetName sheet名,第一个默认是sheet1
* @since 5.8.0
*/
public ExcelReader(final String excelFilePath, final String sheetName) {
this(FileUtil.file(excelFilePath), sheetName);
}
/**
* 构造(读写方式读取)
*
* @param bookFile Excel文件
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(final File bookFile, final int sheetIndex) {
this(WorkbookUtil.createBook(bookFile, true), sheetIndex);
this.destFile = bookFile;
}
/**
* 构造(读写方式读取)
*
* @param bookFile Excel文件
* @param sheetName sheet名,第一个默认是sheet1
*/
public ExcelReader(final File bookFile, final String sheetName) {
this(WorkbookUtil.createBook(bookFile, true), sheetName);
this.destFile = bookFile;
}
/**
* 构造(只读方式读取)
*
* @param bookStream Excel文件的流
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(final InputStream bookStream, final int sheetIndex) {
this(WorkbookUtil.createBook(bookStream), sheetIndex);
}
/**
* 构造(只读方式读取)
*
* @param bookStream Excel文件的流
* @param sheetName sheet名,第一个默认是sheet1
*/
public ExcelReader(final InputStream bookStream, final String sheetName) {
this(WorkbookUtil.createBook(bookStream), sheetName);
}
/**
* 构造
*
* @param book {@link Workbook} 表示一个Excel文件
* @param sheetIndex sheet序号,0表示第一个sheet
*/
public ExcelReader(final Workbook book, final int sheetIndex) {
this(book.getSheetAt(sheetIndex));
}
/**
* 构造
*
* @param book {@link Workbook} 表示一个Excel文件
* @param sheetName sheet名,第一个默认是sheet1
*/
public ExcelReader(final Workbook book, final String sheetName) {
this(book.getSheet(sheetName));
}
/**
* 构造
*
* @param sheet Excel中的sheet
*/
public ExcelReader(final Sheet sheet) {
super(sheet);
}
// ------------------------------------------------------------------------------------------------------- Constructor end
// ------------------------------------------------------------------------------------------------------- Getters and Setters start
/**
* 是否忽略空行
*
* @return 是否忽略空行
*/
public boolean isIgnoreEmptyRow() {
return ignoreEmptyRow;
}
/**
* 设置是否忽略空行
*
* @param ignoreEmptyRow 是否忽略空行
* @return this
*/
public ExcelReader setIgnoreEmptyRow(final boolean ignoreEmptyRow) {
this.ignoreEmptyRow = ignoreEmptyRow;
return this;
}
/**
* 设置单元格值处理逻辑
* 当Excel中的值并不能满足我们的读取要求时,通过传入一个编辑接口,可以对单元格值自定义,例如对数字和日期类型值转换为字符串等
*
* @param cellEditor 单元格值处理接口
* @return this
*/
public ExcelReader setCellEditor(final CellEditor cellEditor) {
this.cellEditor = cellEditor;
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(final int startRowIndex) {
return read(startRowIndex, Integer.MAX_VALUE);
}
/**
* 读取工作簿中指定的Sheet,此方法会把第一行作为标题行,替换标题别名
*
* @param startRowIndex 起始行(包含,从0开始计数)
* @param endRowIndex 结束行(包含,从0开始计数)
* @return 行的集合,一行使用List表示
*/
public List> read(final int startRowIndex, final int endRowIndex) {
return read(startRowIndex, endRowIndex, false);
}
/**
* 读取工作簿中指定的Sheet
*
* @param startRowIndex 起始行(包含,从0开始计数)
* @param endRowIndex 结束行(包含,从0开始计数)
* @param aliasFirstLine 是否首行作为标题行转换别名
* @return 行的集合,一行使用List表示
* @since 5.4.4
*/
public List> read(final int startRowIndex, final int endRowIndex, final boolean aliasFirstLine) {
final ListSheetReader reader = new ListSheetReader(startRowIndex, endRowIndex, aliasFirstLine);
reader.setCellEditor(this.cellEditor);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
reader.setHeaderAlias(headerAlias);
return read(reader);
}
/**
* 读取工作簿中指定的Sheet中指定列
*
* @param columnIndex 列号,从0开始计数
* @param startRowIndex 起始行(包含,从0开始计数)
* @return 列的集合
* @since 5.7.17
*/
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy