add ExcelReadConfig

This commit is contained in:
Looly 2024-08-10 23:45:55 +08:00
parent 6440c302a5
commit 98c4722f54
10 changed files with 90 additions and 77 deletions

View File

@ -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;
}
}

View File

@ -23,6 +23,7 @@ import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.*;
import org.dromara.hutool.poi.excel.cell.CellUtil; 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 org.dromara.hutool.poi.excel.writer.ExcelWriter;
import java.io.File; import java.io.File;
@ -37,13 +38,9 @@ import java.util.Map;
* @author Looly * @author Looly
* @since 3.1.0 * @since 3.1.0
*/ */
public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> { public class ExcelReader extends ExcelBase<ExcelReader, ExcelReadConfig> {
/** // region ----- Constructor
* 是否忽略空行
*/
private boolean ignoreEmptyRow = true;
// ------------------------------------------------------------------------------------------------------- Constructor start
/** /**
* 构造 * 构造
@ -134,33 +131,9 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
* @param sheet Excel中的sheet * @param sheet Excel中的sheet
*/ */
public ExcelReader(final Sheet sheet) { public ExcelReader(final Sheet sheet) {
super(new ExcelConfig(), sheet); super(new ExcelReadConfig(), sheet);
} }
// ------------------------------------------------------------------------------------------------------- Constructor end // endregion
// ------------------------------------------------------------------------------------------------------- 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
/** /**
* 读取工作簿中指定的Sheet的所有行列数据 * 读取工作簿中指定的Sheet的所有行列数据
@ -205,7 +178,6 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
public List<List<Object>> read(final int startRowIndex, final int endRowIndex, final boolean aliasFirstLine) { public List<List<Object>> read(final int startRowIndex, final int endRowIndex, final boolean aliasFirstLine) {
final ListSheetReader reader = new ListSheetReader(startRowIndex, endRowIndex, aliasFirstLine); final ListSheetReader reader = new ListSheetReader(startRowIndex, endRowIndex, aliasFirstLine);
reader.setExcelConfig(this.config); reader.setExcelConfig(this.config);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
return read(reader); return read(reader);
} }
@ -233,7 +205,6 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
public List<Object> readColumn(final int columnIndex, final int startRowIndex, final int endRowIndex) { public List<Object> readColumn(final int columnIndex, final int startRowIndex, final int endRowIndex) {
final ColumnSheetReader reader = new ColumnSheetReader(columnIndex, startRowIndex, endRowIndex); final ColumnSheetReader reader = new ColumnSheetReader(columnIndex, startRowIndex, endRowIndex);
reader.setExcelConfig(this.config); reader.setExcelConfig(this.config);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
return read(reader); return read(reader);
} }
@ -262,7 +233,6 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
final ConsumerSheetReader reader = new ConsumerSheetReader(startRowIndex, endRowIndex, cellHandler); final ConsumerSheetReader reader = new ConsumerSheetReader(startRowIndex, endRowIndex, cellHandler);
reader.setExcelConfig(this.config); reader.setExcelConfig(this.config);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
reader.read(sheet); reader.read(sheet);
} }
@ -288,7 +258,6 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
public List<Map<String, Object>> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex) { public List<Map<String, Object>> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex) {
final MapSheetReader reader = new MapSheetReader(headerRowIndex, startRowIndex, endRowIndex); final MapSheetReader reader = new MapSheetReader(headerRowIndex, startRowIndex, endRowIndex);
reader.setExcelConfig(this.config); reader.setExcelConfig(this.config);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
return read(reader); return read(reader);
} }
@ -330,7 +299,6 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelConfig> {
public <T> List<T> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex, final Class<T> beanType) { public <T> List<T> read(final int headerRowIndex, final int startRowIndex, final int endRowIndex, final Class<T> beanType) {
final BeanSheetReader<T> reader = new BeanSheetReader<>(headerRowIndex, startRowIndex, endRowIndex, beanType); final BeanSheetReader<T> reader = new BeanSheetReader<>(headerRowIndex, startRowIndex, endRowIndex, beanType);
reader.setExcelConfig(this.config); reader.setExcelConfig(this.config);
reader.setIgnoreEmptyRow(this.ignoreEmptyRow);
return read(reader); return read(reader);
} }

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,11 +10,11 @@
* See the Mulan PSL v2 for more details. * 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.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.dromara.hutool.poi.excel.ExcelConfig; import org.dromara.hutool.poi.excel.reader.ExcelReadConfig;
/** /**
* 抽象{@link Sheet}数据读取实现 * 抽象{@link Sheet}数据读取实现
@ -26,14 +26,10 @@ import org.dromara.hutool.poi.excel.ExcelConfig;
public abstract class AbstractSheetReader<T> implements SheetReader<T> { public abstract class AbstractSheetReader<T> implements SheetReader<T> {
protected final CellRangeAddress cellRangeAddress; protected final CellRangeAddress cellRangeAddress;
/**
* 是否忽略空行
*/
protected boolean ignoreEmptyRow = true;
/** /**
* Excel配置 * Excel配置
*/ */
protected ExcelConfig config; protected ExcelReadConfig config;
/** /**
* 构造 * 构造
@ -62,16 +58,7 @@ public abstract class AbstractSheetReader<T> implements SheetReader<T> {
* *
* @param config Excel配置 * @param config Excel配置
*/ */
public void setExcelConfig(final ExcelConfig config) { public void setExcelConfig(final ExcelReadConfig config) {
this.config = config; this.config = config;
} }
/**
* 设置是否忽略空行
*
* @param ignoreEmptyRow 是否忽略空行
*/
public void setIgnoreEmptyRow(final boolean ignoreEmptyRow) {
this.ignoreEmptyRow = ignoreEmptyRow;
}
} }

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,12 +10,12 @@
* See the Mulan PSL v2 for more details. * 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.usermodel.Sheet;
import org.dromara.hutool.core.bean.BeanUtil; import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.bean.copier.CopyOptions; 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.ArrayList;
import java.util.List; import java.util.List;
@ -67,16 +67,7 @@ public class BeanSheetReader<T> implements SheetReader<List<T>> {
* *
* @param config Excel配置 * @param config Excel配置
*/ */
public void setExcelConfig(final ExcelConfig config) { public void setExcelConfig(final ExcelReadConfig config) {
this.mapSheetReader.setExcelConfig(config); this.mapSheetReader.setExcelConfig(config);
} }
/**
* 设置是否忽略空行
*
* @param ignoreEmptyRow 是否忽略空行
*/
public void setIgnoreEmptyRow(final boolean ignoreEmptyRow) {
this.mapSheetReader.setIgnoreEmptyRow(ignoreEmptyRow);
}
} }

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details. * 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.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@ -48,6 +48,7 @@ public class ColumnSheetReader extends AbstractSheetReader<List<Object>> {
final int columnIndex = this.cellRangeAddress.getFirstColumn(); final int columnIndex = this.cellRangeAddress.getFirstColumn();
final CellEditor cellEditor = this.config.getCellEditor(); final CellEditor cellEditor = this.config.getCellEditor();
final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow();
Object value; Object value;
for (int i = startRowIndex; i <= endRowIndex; i++) { for (int i = startRowIndex; i <= endRowIndex; i++) {
value = CellUtil.getCellValue(CellUtil.getCell(sheet.getRow(i), columnIndex), cellEditor); value = CellUtil.getCellValue(CellUtil.getCell(sheet.getRow(i), columnIndex), cellEditor);

View File

@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details. * 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.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details. * 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.CollUtil;
import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.convert.Convert;
@ -53,6 +53,7 @@ public class ListSheetReader extends AbstractSheetReader<List<List<Object>>> {
List<Object> rowList; List<Object> rowList;
final CellEditor cellEditor = this.config.getCellEditor(); final CellEditor cellEditor = this.config.getCellEditor();
final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow();
for (int i = startRowIndex; i <= endRowIndex; i++) { for (int i = startRowIndex; i <= endRowIndex; i++) {
rowList = RowUtil.readRow(sheet.getRow(i), cellEditor); rowList = RowUtil.readRow(sheet.getRow(i), cellEditor);
if (CollUtil.isNotEmpty(rowList) || !ignoreEmptyRow) { if (CollUtil.isNotEmpty(rowList) || !ignoreEmptyRow) {

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details. * 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.CollUtil;
import org.dromara.hutool.core.collection.iter.IterUtil; import org.dromara.hutool.core.collection.iter.IterUtil;
@ -72,6 +72,7 @@ public class MapSheetReader extends AbstractSheetReader<List<Map<String, Object>
final List<String> headerList = this.config.aliasHeader(readRow(sheet, headerRowIndex)); final List<String> headerList = this.config.aliasHeader(readRow(sheet, headerRowIndex));
final List<Map<String, Object>> result = new ArrayList<>(endRowIndex - startRowIndex + 1); final List<Map<String, Object>> result = new ArrayList<>(endRowIndex - startRowIndex + 1);
final boolean ignoreEmptyRow = this.config.isIgnoreEmptyRow();
List<Object> rowList; List<Object> rowList;
for (int i = startRowIndex; i <= endRowIndex; i++) { for (int i = startRowIndex; i <= endRowIndex; i++) {
// 跳过标题行 // 跳过标题行

View File

@ -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. * Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the 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: * You may obtain a copy of Mulan PSL v2 at:
@ -10,7 +10,7 @@
* See the Mulan PSL v2 for more details. * 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.usermodel.Sheet;

View File

@ -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;