mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
4a9b4d4253
commit
3dc61bd27c
@ -189,7 +189,7 @@ public class ExcelBase<T extends ExcelBase<T, C>, C extends ExcelConfig> impleme
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public T setSheet(final String sheetName) {
|
||||
return setSheet(WorkbookUtil.getOrCreateSheet(this.workbook, sheetName));
|
||||
return setSheet(SheetUtil.getOrCreateSheet(this.workbook, sheetName));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -201,13 +201,13 @@ public class ExcelBase<T extends ExcelBase<T, C>, C extends ExcelConfig> impleme
|
||||
* @since 4.0.10
|
||||
*/
|
||||
public T setSheet(final int sheetIndex) {
|
||||
return setSheet(WorkbookUtil.getOrCreateSheet(this.workbook, sheetIndex));
|
||||
return setSheet(SheetUtil.getOrCreateSheet(this.workbook, sheetIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置自定义Sheet
|
||||
*
|
||||
* @param sheet 自定义sheet,可以通过{@link WorkbookUtil#getOrCreateSheet(Workbook, String)} 创建
|
||||
* @param sheet 自定义sheet,可以通过{@link SheetUtil#getOrCreateSheet(Workbook, String)} 创建
|
||||
* @return this
|
||||
* @since 5.2.1
|
||||
*/
|
||||
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.ss.util.cellwalk.CellHandler;
|
||||
import org.apache.poi.ss.util.cellwalk.CellWalk;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
/**
|
||||
* {@link Sheet}相关工具类
|
||||
*
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class SheetUtil {
|
||||
|
||||
/**
|
||||
* 获取或者创建sheet表<br>
|
||||
* 如果sheet表在Workbook中已经存在,则获取之,否则创建之
|
||||
*
|
||||
* @param book 工作簿{@link Workbook}
|
||||
* @param sheetName 工作表名
|
||||
* @return 工作表{@link Sheet}
|
||||
* @since 4.0.2
|
||||
*/
|
||||
public static Sheet getOrCreateSheet(final Workbook book, String sheetName) {
|
||||
if (null == book) {
|
||||
return null;
|
||||
}
|
||||
sheetName = StrUtil.isBlank(sheetName) ? "sheet1" : sheetName;
|
||||
Sheet sheet = book.getSheet(sheetName);
|
||||
if (null == sheet) {
|
||||
sheet = book.createSheet(sheetName);
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或者创建sheet表<br>
|
||||
* 自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认)<br>
|
||||
* 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet
|
||||
*
|
||||
* @param book 工作簿{@link Workbook}
|
||||
* @param sheetIndex 工作表序号
|
||||
* @return 工作表{@link Sheet}
|
||||
* @since 5.2.1
|
||||
*/
|
||||
public static Sheet getOrCreateSheet(final Workbook book, final int sheetIndex) {
|
||||
Sheet sheet = null;
|
||||
try {
|
||||
sheet = book.getSheetAt(sheetIndex);
|
||||
} catch (final IllegalArgumentException ignore) {
|
||||
//ignore
|
||||
}
|
||||
if (null == sheet) {
|
||||
sheet = book.createSheet();
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* sheet是否为空
|
||||
*
|
||||
* @param sheet {@link Sheet}
|
||||
* @return sheet是否为空
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static boolean isEmpty(final Sheet sheet) {
|
||||
return null == sheet || (sheet.getLastRowNum() == 0 && sheet.getPhysicalNumberOfRows() == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历Sheet中的指定区域单元格
|
||||
*
|
||||
* @param sheet {@link Sheet}
|
||||
* @param range 区域
|
||||
* @param cellHandler 单元格处理器
|
||||
*/
|
||||
public static void walk(final Sheet sheet, final CellRangeAddress range, final CellHandler cellHandler) {
|
||||
final CellWalk cellWalk = new CellWalk(sheet, range);
|
||||
cellWalk.traverse(cellHandler);
|
||||
}
|
||||
}
|
@ -13,16 +13,15 @@
|
||||
|
||||
package org.dromara.hutool.poi.excel;
|
||||
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.poi.exceptions.POIException;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.IoUtil;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.poi.exceptions.POIException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -324,61 +323,6 @@ public class WorkbookUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或者创建sheet表<br>
|
||||
* 如果sheet表在Workbook中已经存在,则获取之,否则创建之
|
||||
*
|
||||
* @param book 工作簿{@link Workbook}
|
||||
* @param sheetName 工作表名
|
||||
* @return 工作表{@link Sheet}
|
||||
* @since 4.0.2
|
||||
*/
|
||||
public static Sheet getOrCreateSheet(final Workbook book, String sheetName) {
|
||||
if (null == book) {
|
||||
return null;
|
||||
}
|
||||
sheetName = StrUtil.isBlank(sheetName) ? "sheet1" : sheetName;
|
||||
Sheet sheet = book.getSheet(sheetName);
|
||||
if (null == sheet) {
|
||||
sheet = book.createSheet(sheetName);
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或者创建sheet表<br>
|
||||
* 自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认)<br>
|
||||
* 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet
|
||||
*
|
||||
* @param book 工作簿{@link Workbook}
|
||||
* @param sheetIndex 工作表序号
|
||||
* @return 工作表{@link Sheet}
|
||||
* @since 5.2.1
|
||||
*/
|
||||
public static Sheet getOrCreateSheet(final Workbook book, final int sheetIndex) {
|
||||
Sheet sheet = null;
|
||||
try {
|
||||
sheet = book.getSheetAt(sheetIndex);
|
||||
} catch (final IllegalArgumentException ignore) {
|
||||
//ignore
|
||||
}
|
||||
if (null == sheet) {
|
||||
sheet = book.createSheet();
|
||||
}
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* sheet是否为空
|
||||
*
|
||||
* @param sheet {@link Sheet}
|
||||
* @return sheet是否为空
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static boolean isEmpty(final Sheet sheet) {
|
||||
return null == sheet || (sheet.getLastRowNum() == 0 && sheet.getPhysicalNumberOfRows() == 0);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
|
@ -231,7 +231,7 @@ public class ExcelReader extends ExcelBase<ExcelReader, ExcelReadConfig> {
|
||||
public void read(final int startRowIndex, final int endRowIndex, final SerBiConsumer<Cell, Object> cellHandler) {
|
||||
checkNotClosed();
|
||||
|
||||
final ConsumerSheetReader reader = new ConsumerSheetReader(startRowIndex, endRowIndex, cellHandler);
|
||||
final WalkSheetReader reader = new WalkSheetReader(startRowIndex, endRowIndex, cellHandler);
|
||||
reader.setExcelConfig(this.config);
|
||||
reader.read(sheet);
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.dromara.hutool.core.func.SerBiConsumer;
|
||||
import org.dromara.hutool.poi.excel.cell.CellEditor;
|
||||
import org.dromara.hutool.poi.excel.cell.CellUtil;
|
||||
|
||||
/**
|
||||
@ -24,7 +25,7 @@ import org.dromara.hutool.poi.excel.cell.CellUtil;
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class ConsumerSheetReader extends AbstractSheetReader<Void> {
|
||||
public class WalkSheetReader extends AbstractSheetReader<Void> {
|
||||
|
||||
|
||||
private final SerBiConsumer<Cell, Object> cellHandler;
|
||||
@ -36,7 +37,7 @@ public class ConsumerSheetReader extends AbstractSheetReader<Void> {
|
||||
* @param endRowIndex 结束行(包含,从0开始计数)
|
||||
* @param cellHandler 单元格处理器,用于处理读到的单元格及其数据
|
||||
*/
|
||||
public ConsumerSheetReader(final int startRowIndex, final int endRowIndex, final SerBiConsumer<Cell, Object> cellHandler) {
|
||||
public WalkSheetReader(final int startRowIndex, final int endRowIndex, final SerBiConsumer<Cell, Object> cellHandler) {
|
||||
super(startRowIndex, endRowIndex);
|
||||
this.cellHandler = cellHandler;
|
||||
}
|
||||
@ -45,6 +46,7 @@ public class ConsumerSheetReader extends AbstractSheetReader<Void> {
|
||||
public Void read(final Sheet sheet) {
|
||||
final int startRowIndex = Math.max(this.cellRangeAddress.getFirstRow(), sheet.getFirstRowNum());// 读取起始行(包含)
|
||||
final int endRowIndex = Math.min(this.cellRangeAddress.getLastRow(), sheet.getLastRowNum());// 读取结束行(包含)
|
||||
final CellEditor cellEditor = this.config.getCellEditor();
|
||||
|
||||
Row row;
|
||||
for (int y = startRowIndex; y <= endRowIndex; y++) {
|
||||
@ -54,8 +56,8 @@ public class ConsumerSheetReader extends AbstractSheetReader<Void> {
|
||||
final short endColumnIndex = (short) Math.min(this.cellRangeAddress.getLastColumn(), row.getLastCellNum());
|
||||
Cell cell;
|
||||
for (short x = startColumnIndex; x < endColumnIndex; x++) {
|
||||
cell = row.getCell(x);
|
||||
cellHandler.accept(cell, CellUtil.getCellValue(cell));
|
||||
cell = CellUtil.getCell(row, x);
|
||||
cellHandler.accept(cell, CellUtil.getCellValue(cell, cellEditor));
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.dromara.hutool.poi.excel.SheetUtil;
|
||||
import org.dromara.hutool.poi.excel.WorkbookUtil;
|
||||
|
||||
import java.io.File;
|
||||
@ -141,7 +142,7 @@ public class BigExcelWriter extends ExcelWriter {
|
||||
* @param sheetName sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public BigExcelWriter(final SXSSFWorkbook workbook, final String sheetName) {
|
||||
this(WorkbookUtil.getOrCreateSheet(workbook, sheetName));
|
||||
this(SheetUtil.getOrCreateSheet(workbook, sheetName));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -33,6 +33,7 @@ import org.dromara.hutool.core.map.multi.RowKeyTable;
|
||||
import org.dromara.hutool.core.map.multi.Table;
|
||||
import org.dromara.hutool.core.reflect.FieldUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.poi.excel.SheetUtil;
|
||||
import org.dromara.hutool.poi.excel.ExcelBase;
|
||||
import org.dromara.hutool.poi.excel.RowUtil;
|
||||
import org.dromara.hutool.poi.excel.WorkbookUtil;
|
||||
@ -163,7 +164,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter, ExcelWriteConfig> {
|
||||
* @param sheetName sheet名,做为第一个sheet名并写出到此sheet,例如sheet1
|
||||
*/
|
||||
public ExcelWriter(final Workbook workbook, final String sheetName) {
|
||||
this(WorkbookUtil.getOrCreateSheet(workbook, sheetName));
|
||||
this(SheetUtil.getOrCreateSheet(workbook, sheetName));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user