mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add read
This commit is contained in:
parent
b6a9bc6991
commit
3bca95c34a
@ -15,6 +15,7 @@
|
|||||||
* 【core 】 增加StrUtil.removeAny方法(issue#923@Github)
|
* 【core 】 增加StrUtil.removeAny方法(issue#923@Github)
|
||||||
* 【db 】 增加部分Connection参数支持(issue#924@Github)
|
* 【db 】 增加部分Connection参数支持(issue#924@Github)
|
||||||
* 【core 】 FileUtil增加别名方法(pr#926@Github)
|
* 【core 】 FileUtil增加别名方法(pr#926@Github)
|
||||||
|
* 【poi 】 EcelReader中增加read重载,提供每个单元格单独处理的方法(issue#I1JZTL@Gitee)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
||||||
|
@ -9,9 +9,11 @@ import cn.hutool.core.map.MapUtil;
|
|||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.poi.excel.cell.CellEditor;
|
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.cell.CellUtil;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
import org.apache.poi.ss.extractor.ExcelExtractor;
|
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.Row;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
@ -34,14 +36,21 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class ExcelReader extends ExcelBase<ExcelReader> {
|
public class ExcelReader extends ExcelBase<ExcelReader> {
|
||||||
|
|
||||||
/** 是否忽略空行 */
|
/**
|
||||||
|
* 是否忽略空行
|
||||||
|
*/
|
||||||
private boolean ignoreEmptyRow = true;
|
private boolean ignoreEmptyRow = true;
|
||||||
/** 单元格值处理接口 */
|
/**
|
||||||
|
* 单元格值处理接口
|
||||||
|
*/
|
||||||
private CellEditor cellEditor;
|
private CellEditor cellEditor;
|
||||||
/** 标题别名 */
|
/**
|
||||||
|
* 标题别名
|
||||||
|
*/
|
||||||
private Map<String, String> headerAlias = new HashMap<>();
|
private Map<String, String> headerAlias = new HashMap<>();
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------- Constructor start
|
// ------------------------------------------------------------------------------------------------------- Constructor start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
@ -125,6 +134,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
|||||||
// ------------------------------------------------------------------------------------------------------- Constructor end
|
// ------------------------------------------------------------------------------------------------------- Constructor end
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------- Getters and Setters start
|
// ------------------------------------------------------------------------------------------------------- Getters and Setters start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否忽略空行
|
* 是否忽略空行
|
||||||
*
|
*
|
||||||
@ -255,6 +265,45 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
|||||||
return resultList;
|
return resultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取工作簿中指定的Sheet,此方法为类流处理方式,当读到指定单元格时,会调用CellEditor接口<br>
|
||||||
|
* 用户通过实现此接口,可以更加灵活的处理每个单元格的数据。
|
||||||
|
*
|
||||||
|
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||||
|
* @since 5.3.8
|
||||||
|
*/
|
||||||
|
public void read(CellHandler cellHandler) {
|
||||||
|
read(0, Integer.MAX_VALUE, cellHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取工作簿中指定的Sheet,此方法为类流处理方式,当读到指定单元格时,会调用CellEditor接口<br>
|
||||||
|
* 用户通过实现此接口,可以更加灵活的处理每个单元格的数据。
|
||||||
|
*
|
||||||
|
* @param startRowIndex 起始行(包含,从0开始计数)
|
||||||
|
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||||
|
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||||
|
* @since 5.3.8
|
||||||
|
*/
|
||||||
|
public void read(int startRowIndex, int endRowIndex, CellHandler cellHandler) {
|
||||||
|
checkNotClosed();
|
||||||
|
|
||||||
|
startRowIndex = Math.max(startRowIndex, this.sheet.getFirstRowNum());// 读取起始行(包含)
|
||||||
|
endRowIndex = Math.min(endRowIndex, this.sheet.getLastRowNum());// 读取结束行(包含)
|
||||||
|
|
||||||
|
Row row;
|
||||||
|
short columnSize;
|
||||||
|
for (int y = startRowIndex; y <= endRowIndex; y++) {
|
||||||
|
row = this.sheet.getRow(y);
|
||||||
|
columnSize = row.getLastCellNum();
|
||||||
|
Cell cell;
|
||||||
|
for (short x = 0; x < columnSize; x++) {
|
||||||
|
cell = row.getCell(x);
|
||||||
|
cellHandler.handle(cell, CellUtil.getCellValue(cell));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取Excel为Map的列表,读取所有行,默认第一行做为标题,数据从第二行开始<br>
|
* 读取Excel为Map的列表,读取所有行,默认第一行做为标题,数据从第二行开始<br>
|
||||||
* Map表示一行,标题为key,单元格内容为value
|
* Map表示一行,标题为key,单元格内容为value
|
||||||
@ -420,6 +469,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------- Private methods start
|
// ------------------------------------------------------------------------------------------------------- Private methods start
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取一行
|
* 读取一行
|
||||||
*
|
*
|
||||||
|
@ -4,12 +4,15 @@ import org.apache.poi.ss.usermodel.Cell;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元格编辑器接口
|
* 单元格编辑器接口
|
||||||
* @author Looly
|
|
||||||
*
|
*
|
||||||
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
public interface CellEditor {
|
public interface CellEditor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编辑
|
* 编辑
|
||||||
|
*
|
||||||
* @param cell 单元格对象,可以获取单元格行、列样式等信息
|
* @param cell 单元格对象,可以获取单元格行、列样式等信息
|
||||||
* @param value 单元格值
|
* @param value 单元格值
|
||||||
* @return 编辑后的对象
|
* @return 编辑后的对象
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.hutool.poi.excel.cell;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元格处理器接口
|
||||||
|
*
|
||||||
|
* @author Looly
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface CellHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理
|
||||||
|
*
|
||||||
|
* @param cell 单元格对象,可以获取单元格行、列样式等信息
|
||||||
|
* @param value 单元格值
|
||||||
|
*/
|
||||||
|
void handle(Cell cell, Object value);
|
||||||
|
}
|
@ -208,4 +208,10 @@ public class ExcelReadTest {
|
|||||||
Assert.assertEquals(11L, read.get(2).get(2));
|
Assert.assertEquals(11L, read.get(2).get(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readCellsTest() {
|
||||||
|
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
|
||||||
|
reader.read((cell, value)-> Console.log("{}, {} {}", cell.getRowIndex(), cell.getColumnIndex(), value));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user