This commit is contained in:
Looly 2024-08-24 14:33:01 +08:00
parent 1879292a11
commit 01e81de806
3 changed files with 31 additions and 5 deletions

View File

@ -318,11 +318,7 @@ public class ExcelBase<T extends ExcelBase<T, C>, C extends ExcelConfig> impleme
* @since 4.0.6
*/
public Cell getCell(final int x, final int y, final boolean isCreateIfNotExist) {
final Row row = isCreateIfNotExist ? RowUtil.getOrCreateRow(this.sheet, y) : this.sheet.getRow(y);
if (null != row) {
return isCreateIfNotExist ? CellUtil.getOrCreateCell(row, x) : row.getCell(x);
}
return null;
return CellUtil.getCell(this.sheet, x, y, isCreateIfNotExist);
}

View File

@ -22,6 +22,7 @@ import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.ss.util.SheetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.poi.excel.RowUtil;
import org.dromara.hutool.poi.excel.cell.editors.CellEditor;
import org.dromara.hutool.poi.excel.cell.editors.TrimEditor;
import org.dromara.hutool.poi.excel.cell.setters.CellSetter;
@ -190,6 +191,24 @@ public class CellUtil {
// endregion
// region ----- getCell
/**
* 获取指定坐标单元格如果isCreateIfNotExist为false则在单元格不存在时返回{@code null}
*
* @param sheet {@link Sheet}
* @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号
* @param isCreateIfNotExist 单元格不存在时是否创建
* @return {@link Cell}
* @since 6.0.0
*/
public static Cell getCell(final Sheet sheet, final int x, final int y, final boolean isCreateIfNotExist) {
final Row row = isCreateIfNotExist ? RowUtil.getOrCreateRow(sheet, y) : sheet.getRow(y);
if (null != row) {
return isCreateIfNotExist ? getOrCreateCell(row, x) : row.getCell(x);
}
return null;
}
/**
* 获取单元格如果单元格不存在返回{@link NullCell}
*

View File

@ -1011,6 +1011,17 @@ public class ExcelWriter extends ExcelBase<ExcelWriter, ExcelWriteConfig> {
// region ----- fill
public ExcelWriter fillRow(final Map<?, ?> rowMap){
rowMap.forEach((key, value)->{
});
return this;
}
public ExcelWriter fillCell(final String name, final Object value){
final Cell cell = this.templateContext.getCell(name);
if(null != cell){
CellUtil.setCellValue(cell, value, this.config.getCellEditor());
}
return this;
}