diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReadConfig.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReadConfig.java new file mode 100644 index 000000000..bb68d49a7 --- /dev/null +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReadConfig.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * 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.reader; + +import org.dromara.hutool.poi.excel.ExcelConfig; + +/** + * Excel读取配置 + * + * @author Looly + */ +public class ExcelReadConfig extends ExcelConfig { + /** + * 是否忽略空行 + */ + protected boolean ignoreEmptyRow = true; + + /** + * 是否忽略空行 + * @return 是否忽略空行 + */ + public boolean isIgnoreEmptyRow() { + return this.ignoreEmptyRow; + } + + /** + * 设置是否忽略空行 + * + * @param ignoreEmptyRow 是否忽略空行 + * @return this + */ + public ExcelReadConfig setIgnoreEmptyRow(final boolean ignoreEmptyRow) { + this.ignoreEmptyRow = ignoreEmptyRow; + return this; + } +} diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java index cb73f4b8d..694fd0f85 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java @@ -23,6 +23,7 @@ import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.cell.CellUtil; +import org.dromara.hutool.poi.excel.reader.sheet.*; import org.dromara.hutool.poi.excel.writer.ExcelWriter; import java.io.File; @@ -37,13 +38,9 @@ import java.util.Map; * @author Looly * @since 3.1.0 */ -public class ExcelReader extends ExcelBase { +public class ExcelReader extends ExcelBase { - /** - * 是否忽略空行 - */ - private boolean ignoreEmptyRow = true; - // ------------------------------------------------------------------------------------------------------- Constructor start + // region ----- Constructor /** * 构造 @@ -134,33 +131,9 @@ public class ExcelReader extends ExcelBase { * @param sheet Excel中的sheet */ public ExcelReader(final Sheet sheet) { - super(new ExcelConfig(), sheet); + super(new ExcelReadConfig(), 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; - } - - // ------------------------------------------------------------------------------------------------------- Getters and Setters end + // endregion /** * 读取工作簿中指定的Sheet的所有行列数据 @@ -205,7 +178,6 @@ public class ExcelReader extends ExcelBase { public List> read(final int startRowIndex, final int endRowIndex, final boolean aliasFirstLine) { final ListSheetReader reader = new ListSheetReader(startRowIndex, endRowIndex, aliasFirstLine); reader.setExcelConfig(this.config); - reader.setIgnoreEmptyRow(this.ignoreEmptyRow); return read(reader); } @@ -233,7 +205,6 @@ public class ExcelReader extends ExcelBase { public List readColumn(final int columnIndex, final int startRowIndex, final int endRowIndex) { final ColumnSheetReader reader = new ColumnSheetReader(columnIndex, startRowIndex, endRowIndex); reader.setExcelConfig(this.config); - reader.setIgnoreEmptyRow(this.ignoreEmptyRow); return read(reader); } @@ -262,7 +233,6 @@ public class ExcelReader extends ExcelBase { final ConsumerSheetReader reader = new ConsumerSheetReader(startRowIndex, endRowIndex, cellHandler); reader.setExcelConfig(this.config); - reader.setIgnoreEmptyRow(this.ignoreEmptyRow); reader.read(sheet); } @@ -288,7 +258,6 @@ public class ExcelReader extends ExcelBase { public List> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex) { final MapSheetReader reader = new MapSheetReader(headerRowIndex, startRowIndex, endRowIndex); reader.setExcelConfig(this.config); - reader.setIgnoreEmptyRow(this.ignoreEmptyRow); return read(reader); } @@ -330,7 +299,6 @@ public class ExcelReader extends ExcelBase { public List read(final int headerRowIndex, final int startRowIndex, final int endRowIndex, final Class beanType) { final BeanSheetReader reader = new BeanSheetReader<>(headerRowIndex, startRowIndex, endRowIndex, beanType); reader.setExcelConfig(this.config); - reader.setIgnoreEmptyRow(this.ignoreEmptyRow); return read(reader); } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/AbstractSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/AbstractSheetReader.java similarity index 75% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/AbstractSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/AbstractSheetReader.java index fe85dcebe..47831ecc6 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/AbstractSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/AbstractSheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,11 +10,11 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; -import org.dromara.hutool.poi.excel.ExcelConfig; +import org.dromara.hutool.poi.excel.reader.ExcelReadConfig; /** * 抽象{@link Sheet}数据读取实现 @@ -26,14 +26,10 @@ import org.dromara.hutool.poi.excel.ExcelConfig; public abstract class AbstractSheetReader implements SheetReader { protected final CellRangeAddress cellRangeAddress; - /** - * 是否忽略空行 - */ - protected boolean ignoreEmptyRow = true; /** * Excel配置 */ - protected ExcelConfig config; + protected ExcelReadConfig config; /** * 构造 @@ -62,16 +58,7 @@ public abstract class AbstractSheetReader implements SheetReader { * * @param config Excel配置 */ - public void setExcelConfig(final ExcelConfig config) { + public void setExcelConfig(final ExcelReadConfig config) { this.config = config; } - - /** - * 设置是否忽略空行 - * - * @param ignoreEmptyRow 是否忽略空行 - */ - public void setIgnoreEmptyRow(final boolean ignoreEmptyRow) { - this.ignoreEmptyRow = ignoreEmptyRow; - } } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/BeanSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/BeanSheetReader.java similarity index 84% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/BeanSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/BeanSheetReader.java index d01dfd276..b75f510a8 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/BeanSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/BeanSheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,12 +10,12 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.apache.poi.ss.usermodel.Sheet; import org.dromara.hutool.core.bean.BeanUtil; import org.dromara.hutool.core.bean.copier.CopyOptions; -import org.dromara.hutool.poi.excel.ExcelConfig; +import org.dromara.hutool.poi.excel.reader.ExcelReadConfig; import java.util.ArrayList; import java.util.List; @@ -67,16 +67,7 @@ public class BeanSheetReader implements SheetReader> { * * @param config Excel配置 */ - public void setExcelConfig(final ExcelConfig config) { + public void setExcelConfig(final ExcelReadConfig config) { this.mapSheetReader.setExcelConfig(config); } - - /** - * 设置是否忽略空行 - * - * @param ignoreEmptyRow 是否忽略空行 - */ - public void setIgnoreEmptyRow(final boolean ignoreEmptyRow) { - this.mapSheetReader.setIgnoreEmptyRow(ignoreEmptyRow); - } } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ColumnSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ColumnSheetReader.java similarity index 92% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ColumnSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ColumnSheetReader.java index a8ab4872f..cdad1e790 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ColumnSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ColumnSheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; @@ -48,6 +48,7 @@ public class ColumnSheetReader extends AbstractSheetReader> { final int columnIndex = this.cellRangeAddress.getFirstColumn(); final CellEditor cellEditor = this.config.getCellEditor(); + final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow(); Object value; for (int i = startRowIndex; i <= endRowIndex; i++) { value = CellUtil.getCellValue(CellUtil.getCell(sheet.getRow(i), columnIndex), cellEditor); diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ConsumerSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ConsumerSheetReader.java similarity index 97% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ConsumerSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ConsumerSheetReader.java index 1b191e850..8835bc0cf 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ConsumerSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ConsumerSheetReader.java @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ListSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ListSheetReader.java similarity index 93% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ListSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ListSheetReader.java index 7ae66e9ba..eb8630f75 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ListSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/ListSheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.convert.Convert; @@ -53,6 +53,7 @@ public class ListSheetReader extends AbstractSheetReader>> { List rowList; final CellEditor cellEditor = this.config.getCellEditor(); + final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow(); for (int i = startRowIndex; i <= endRowIndex; i++) { rowList = RowUtil.readRow(sheet.getRow(i), cellEditor); if (CollUtil.isNotEmpty(rowList) || !ignoreEmptyRow) { diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/MapSheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/MapSheetReader.java similarity index 95% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/MapSheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/MapSheetReader.java index c30a57e81..9c0a0bf6b 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/MapSheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/MapSheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.iter.IterUtil; @@ -72,6 +72,7 @@ public class MapSheetReader extends AbstractSheetReader final List headerList = this.config.aliasHeader(readRow(sheet, headerRowIndex)); final List> result = new ArrayList<>(endRowIndex - startRowIndex + 1); + final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow(); List rowList; for (int i = startRowIndex; i <= endRowIndex; i++) { // 跳过标题行 diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/SheetReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/SheetReader.java similarity index 89% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/SheetReader.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/SheetReader.java index f3de05d2c..6051364b2 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/SheetReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/SheetReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * 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: @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel.reader; +package org.dromara.hutool.poi.excel.reader.sheet; import org.apache.poi.ss.usermodel.Sheet; diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/package-info.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/package-info.java new file mode 100644 index 000000000..8c12e6319 --- /dev/null +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/sheet/package-info.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * 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. + */ + +/** + * Excel Sheet读取实现 + * + * @author Looly + */ +package org.dromara.hutool.poi.excel.reader.sheet;