mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add ColumnSheetReader
This commit is contained in:
parent
ab1d8e84e4
commit
f4ca74f376
@ -11,6 +11,7 @@
|
||||
* 【http 】 增加BytesBody、FormUrlEncodedBody
|
||||
* 【cron 】 TaskTable.remove增加返回值(issue#I4HX3B@Gitee)
|
||||
* 【core 】 Tree增加filter、filterNew、cloneTree、hasChild方法(issue#I4HFC6@Gitee)
|
||||
* 【poi 】 增加ColumnSheetReader及ExcelReader.readColumn,支持读取某一列
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github)
|
||||
|
||||
|
@ -6,6 +6,7 @@ import cn.hutool.poi.excel.cell.CellEditor;
|
||||
import cn.hutool.poi.excel.cell.CellHandler;
|
||||
import cn.hutool.poi.excel.cell.CellUtil;
|
||||
import cn.hutool.poi.excel.reader.BeanSheetReader;
|
||||
import cn.hutool.poi.excel.reader.ColumnSheetReader;
|
||||
import cn.hutool.poi.excel.reader.ListSheetReader;
|
||||
import cn.hutool.poi.excel.reader.MapSheetReader;
|
||||
import cn.hutool.poi.excel.reader.SheetReader;
|
||||
@ -78,8 +79,8 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param bookStream Excel文件的流
|
||||
* @param sheetIndex sheet序号,0表示第一个sheet
|
||||
* @param bookStream Excel文件的流
|
||||
* @param sheetIndex sheet序号,0表示第一个sheet
|
||||
*/
|
||||
public ExcelReader(InputStream bookStream, int sheetIndex) {
|
||||
this(WorkbookUtil.createBook(bookStream), sheetIndex);
|
||||
@ -88,8 +89,8 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param bookStream Excel文件的流
|
||||
* @param sheetName sheet名,第一个默认是sheet1
|
||||
* @param bookStream Excel文件的流
|
||||
* @param sheetName sheet名,第一个默认是sheet1
|
||||
*/
|
||||
public ExcelReader(InputStream bookStream, String sheetName) {
|
||||
this(WorkbookUtil.createBook(bookStream), sheetName);
|
||||
@ -237,8 +238,8 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
/**
|
||||
* 读取工作簿中指定的Sheet
|
||||
*
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
* @param aliasFirstLine 是否首行作为标题行转换别名
|
||||
* @return 行的集合,一行使用List表示
|
||||
* @since 5.4.4
|
||||
@ -251,11 +252,40 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
return read(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取工作簿中指定的Sheet中指定列
|
||||
*
|
||||
* @param columnIndex 列号,从0开始计数
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @return 列的集合
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public List<Object> readColumn(int columnIndex, int startRowIndex) {
|
||||
return readColumn(columnIndex, startRowIndex, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取工作簿中指定的Sheet中指定列
|
||||
*
|
||||
* @param columnIndex 列号,从0开始计数
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
* @return 列的集合
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public List<Object> readColumn(int columnIndex, int startRowIndex, int endRowIndex) {
|
||||
final ColumnSheetReader reader = new ColumnSheetReader(columnIndex, startRowIndex, endRowIndex);
|
||||
reader.setCellEditor(this.cellEditor);
|
||||
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
|
||||
reader.setHeaderAlias(headerAlias);
|
||||
return read(reader);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取工作簿中指定的Sheet,此方法为类流处理方式,当读到指定单元格时,会调用CellEditor接口<br>
|
||||
* 用户通过实现此接口,可以更加灵活的处理每个单元格的数据。
|
||||
*
|
||||
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public void read(CellHandler cellHandler) {
|
||||
@ -268,7 +298,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
*
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public void read(int startRowIndex, int endRowIndex, CellHandler cellHandler) {
|
||||
@ -281,7 +311,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
short columnSize;
|
||||
for (int y = startRowIndex; y <= endRowIndex; y++) {
|
||||
row = this.sheet.getRow(y);
|
||||
if(null != row){
|
||||
if (null != row) {
|
||||
columnSize = row.getLastCellNum();
|
||||
Cell cell;
|
||||
for (short x = 0; x < columnSize; x++) {
|
||||
@ -365,12 +395,12 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||
/**
|
||||
* 读取数据为指定类型
|
||||
*
|
||||
* @param <T> 读取数据类型
|
||||
* @param <T> 读取数据类型
|
||||
* @param sheetReader {@link SheetReader}实现
|
||||
* @return 数据读取结果
|
||||
* @since 5.4.4
|
||||
*/
|
||||
public <T> T read(SheetReader<T> sheetReader){
|
||||
public <T> T read(SheetReader<T> sheetReader) {
|
||||
checkNotClosed();
|
||||
return Assert.notNull(sheetReader).read(this.sheet);
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
package cn.hutool.poi.excel.reader;
|
||||
|
||||
import cn.hutool.poi.excel.cell.CellUtil;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 读取单独一列
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public class ColumnSheetReader extends AbstractSheetReader<List<Object>> {
|
||||
|
||||
private final int columnIndex;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param columnIndex 列号,从0开始计数
|
||||
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
*/
|
||||
public ColumnSheetReader(int columnIndex, int startRowIndex, int endRowIndex) {
|
||||
super(startRowIndex, endRowIndex);
|
||||
this.columnIndex = columnIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> read(Sheet sheet) {
|
||||
final List<Object> resultList = new ArrayList<>();
|
||||
|
||||
int startRowIndex = Math.max(this.startRowIndex, sheet.getFirstRowNum());// 读取起始行(包含)
|
||||
int endRowIndex = Math.min(this.endRowIndex, sheet.getLastRowNum());// 读取结束行(包含)
|
||||
|
||||
Object value;
|
||||
for (int i = startRowIndex; i <= endRowIndex; i++) {
|
||||
value = CellUtil.getCellValue(CellUtil.getCell(sheet.getRow(i), columnIndex), cellEditor);
|
||||
if(null != value || false == ignoreEmptyRow){
|
||||
resultList.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
}
|
@ -233,4 +233,15 @@ public class ExcelReadTest {
|
||||
final ExcelReader reader = ExcelUtil.getReader("d:/test/1.-.xls");
|
||||
reader.read((CellHandler) Console::log);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readColumnTest(){
|
||||
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
|
||||
final List<Object> objects = reader.readColumn(0, 1);
|
||||
|
||||
Assert.assertEquals(3, objects.size());
|
||||
Assert.assertEquals("张三", objects.get(0));
|
||||
Assert.assertEquals("李四", objects.get(1));
|
||||
Assert.assertEquals("", objects.get(2));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user