diff --git a/CHANGELOG.md b/CHANGELOG.md index 73749004c..66deb69cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java index 301d39309..2ff3d55e6 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelReader.java @@ -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 { /** * 构造 * - * @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 { /** * 构造 * - * @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 { /** * 读取工作簿中指定的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 { return read(reader); } + /** + * 读取工作簿中指定的Sheet中指定列 + * + * @param columnIndex 列号,从0开始计数 + * @param startRowIndex 起始行(包含,从0开始计数) + * @return 列的集合 + * @since 5.7.17 + */ + public List 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 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接口
* 用户通过实现此接口,可以更加灵活的处理每个单元格的数据。 * - * @param cellHandler 单元格处理器,用于处理读到的单元格及其数据 + * @param cellHandler 单元格处理器,用于处理读到的单元格及其数据 * @since 5.3.8 */ public void read(CellHandler cellHandler) { @@ -268,7 +298,7 @@ public class ExcelReader extends ExcelBase { * * @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 { 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 { /** * 读取数据为指定类型 * - * @param 读取数据类型 + * @param 读取数据类型 * @param sheetReader {@link SheetReader}实现 * @return 数据读取结果 * @since 5.4.4 */ - public T read(SheetReader sheetReader){ + public T read(SheetReader sheetReader) { checkNotClosed(); return Assert.notNull(sheetReader).read(this.sheet); } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/reader/ColumnSheetReader.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/reader/ColumnSheetReader.java new file mode 100644 index 000000000..ef1572012 --- /dev/null +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/reader/ColumnSheetReader.java @@ -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> { + + 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 read(Sheet sheet) { + final List 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; + } +} diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelReadTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelReadTest.java index ebe04bf0f..72ac31fab 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelReadTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelReadTest.java @@ -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 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)); + } }