mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add null edit
This commit is contained in:
@@ -73,7 +73,7 @@ public class RowUtil {
|
||||
Object cellValue;
|
||||
boolean isAllNull = true;
|
||||
for (int i = startCellNumInclude; i < size; i++) {
|
||||
cellValue = CellUtil.getCellValue(row.getCell(i), cellEditor);
|
||||
cellValue = CellUtil.getCellValue(CellUtil.getCell(row, i), cellEditor);
|
||||
isAllNull &= StrUtil.isEmptyIfStr(cellValue);
|
||||
cellValues.add(cellValue);
|
||||
}
|
||||
|
@@ -74,10 +74,7 @@ public class CellUtil {
|
||||
* @return 值,类型可能为:Date、Double、Boolean、String
|
||||
*/
|
||||
public static Object getCellValue(Cell cell, CellEditor cellEditor) {
|
||||
if (null == cell) {
|
||||
return null;
|
||||
}
|
||||
return getCellValue(cell, cell.getCellTypeEnum(), cellEditor);
|
||||
return getCellValue(cell, null, cellEditor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,6 +102,9 @@ public class CellUtil {
|
||||
if (null == cell) {
|
||||
return null;
|
||||
}
|
||||
if(cell instanceof NullCell){
|
||||
return null == cellEditor ? null : cellEditor.edit(cell, null);
|
||||
}
|
||||
if (null == cellType) {
|
||||
cellType = cell.getCellTypeEnum();
|
||||
}
|
||||
@@ -235,7 +235,23 @@ public class CellUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已有行或创建新行
|
||||
*获取单元格,如果单元格不存在,返回{@link NullCell}
|
||||
*
|
||||
* @param row Excel表的行
|
||||
* @param cellIndex 列号
|
||||
* @return {@link Row}
|
||||
* @since 5.5.0
|
||||
*/
|
||||
public static Cell getCell(Row row, int cellIndex) {
|
||||
Cell cell = row.getCell(cellIndex);
|
||||
if (null == cell) {
|
||||
return new NullCell(row, cellIndex);
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已有单元格或创建新单元格
|
||||
*
|
||||
* @param row Excel表的行
|
||||
* @param cellIndex 列号
|
||||
|
240
hutool-poi/src/main/java/cn/hutool/poi/excel/cell/NullCell.java
Normal file
240
hutool-poi/src/main/java/cn/hutool/poi/excel/cell/NullCell.java
Normal file
@@ -0,0 +1,240 @@
|
||||
package cn.hutool.poi.excel.cell;
|
||||
|
||||
import org.apache.poi.ss.formula.FormulaParseException;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.Comment;
|
||||
import org.apache.poi.ss.usermodel.Hyperlink;
|
||||
import org.apache.poi.ss.usermodel.RichTextString;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.util.CellAddress;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 当单元格不存在时使用此对象表示,得到的值都为null,此对象只用于标注单元格所在位置信息。
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.5.0
|
||||
*/
|
||||
public class NullCell implements Cell {
|
||||
|
||||
private final Row row;
|
||||
private final int columnIndex;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param row 行
|
||||
* @param columnIndex 列号,从0开始
|
||||
*/
|
||||
public NullCell(Row row, int columnIndex) {
|
||||
this.row = row;
|
||||
this.columnIndex = columnIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnIndex() {
|
||||
return this.columnIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowIndex() {
|
||||
return getRow().getRowNum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sheet getSheet() {
|
||||
return getRow().getSheet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Row getRow() {
|
||||
return this.row;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellType(CellType cellType) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlank() {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellType getCellType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellType getCellTypeEnum() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellType getCachedFormulaResultType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellType getCachedFormulaResultTypeEnum() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(double value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(Date value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(LocalDateTime value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(Calendar value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(RichTextString value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(String value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellFormula(String formula) throws FormulaParseException, IllegalStateException {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFormula() throws IllegalStateException {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCellFormula() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getNumericCellValue() {
|
||||
throw new UnsupportedOperationException("Cell value is null!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getDateCellValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime getLocalDateTimeCellValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RichTextString getRichStringCellValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStringCellValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellValue(boolean value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellErrorValue(byte value) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBooleanCellValue() {
|
||||
throw new UnsupportedOperationException("Cell value is null!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getErrorCellValue() {
|
||||
throw new UnsupportedOperationException("Cell value is null!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellStyle(CellStyle style) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellStyle getCellStyle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAsActiveCell() {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellAddress getAddress() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCellComment(Comment comment) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comment getCellComment() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCellComment() {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hyperlink getHyperlink() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHyperlink(Hyperlink link) {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeHyperlink() {
|
||||
throw new UnsupportedOperationException("Can not set any thing to null cell!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellRangeAddress getArrayFormulaRange() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPartOfArrayFormulaGroup() {
|
||||
throw new UnsupportedOperationException("Cell value is null!");
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package cn.hutool.poi.excel.test;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.poi.excel.ExcelReader;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import lombok.Data;
|
||||
@@ -201,4 +202,20 @@ public class ExcelReadTest {
|
||||
Console.log(list);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullValueEditTest(){
|
||||
final ExcelReader reader = ExcelUtil.getReader("null_cell_test.xlsx");
|
||||
reader.setCellEditor((cell, value)-> ObjectUtil.defaultIfNull(value, "#"));
|
||||
final List<List<Object>> read = reader.read();
|
||||
|
||||
// 对于任意一个单元格有值的情况下,之前的单元格值按照null处理
|
||||
Assert.assertEquals(1, read.get(1).size());
|
||||
Assert.assertEquals(2, read.get(2).size());
|
||||
Assert.assertEquals(3, read.get(3).size());
|
||||
|
||||
Assert.assertEquals("#", read.get(2).get(0));
|
||||
Assert.assertEquals("#", read.get(3).get(0));
|
||||
Assert.assertEquals("#", read.get(3).get(1));
|
||||
}
|
||||
}
|
||||
|
BIN
hutool-poi/src/test/resources/null_cell_test.xlsx
Normal file
BIN
hutool-poi/src/test/resources/null_cell_test.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user