我有3列与Excel文档中的数据。 例如,在中间的列上,有一些没有数据的空白空间。计算数据矩阵内部的空单元格xlsx
我已经创建了一个对象来按照排序的方式按行检索数据。
我遇到的问题是,我无法将空单元存储为空,存在于我的对象中,而且我正在丢失具有此问题的数据。 所以就像在下面的例子中,我只从行中获取数据,每列都写有一些数据。如果一列是空的,则不会返回任何数据。
我得到的一般方法单元格的值:
public static Object getCellValue(Cell cell)
{
Object cellValue = null;
if(cell.getCellTypeEnum() == CellType.STRING){
cellValue = cell.getStringCellValue();
}else if(cell.getCellTypeEnum() == CellType.NUMERIC){
cellValue = cell.getNumericCellValue();
}else if(cell.getCellTypeEnum() == CellType.BOOLEAN){
cellValue = cell.getBooleanCellValue();
}else if(cell.getCellTypeEnum() == CellType.BLANK){
cellValue = "";
}
return cellValue;
}
创建细胞的对象。例如,对输出进行排序非常重要:列1行1必须严格分配给同一行的第2列和第3列。即使单元格为空白/空,数据中也必须保持它们的顺序矩阵。
public class ColumnsOrder {
private String fristColumn;
private String secondColumn;
private String thirdColumn;
public ColumnsOrder() {
}
public ColumnsOrder(String fristColumn) {
super();
this.fristColumn = fristColumn;
}
public ColumnsOrder(String fristColumn, String secondColumn) {
super();
this.fristColumn = fristColumn;
this.secondColumn = secondColumn;
}
public ColumnsOrder(String fristColumn, String secondColumn,
String thirdColumn) {
super();
this.fristColumn = fristColumn;
this.secondColumn = secondColumn;
this.thirdColumn = thirdColumn;
}
public String getFristColumn() {
return fristColumn;
}
public String getSecondColumn() {
return secondColumn;
}
public String getThirdColumn() {
return thirdColumn;
}
}
在这里,我从xlsx中拿出数据,可能在这里我做错了什么。
for(rowIndex = rowStart; rowIndex <= rowEnd; rowIndex++) {
Cell cellOne = null;
Cell cellTwo = null;
Cell cellThree = null;
row = sheet.getRow(rowIndex);
if (row != null) {
// getting data from each column
cellOne = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(0))) ? row.getCell(0) : "");
cellTwo = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(1))) ? row.getCell(1) : " ");
cellThree = (Cell) ((!CheckIfCellIsEmpty.isCellEmpty(row.getCell(2))) ? row.getCell(2) : " ");
// Creating the object using its constructor
columnsExcelTemp.add(new ColumnsOrder(
(String)GetCellValue.getCellValue(cellOne),
(String)GetCellValue.getCellValue(cellTwo),
(String)GetCellValue.getCellValue(cellThree)
)
);
}
workbook.close();
}
检查IsEmpty函数
public static boolean isCellEmpty(final Cell cell)
{
if (cell == null)
{
return true;
}
/*
if (cell.getCellTypeEnum() == CellType.BLANK) {
return false;
}
if (cell.getCellTypeEnum() == CellType.STRING)
{
return false;
}
if(cell.getStringCellValue().isEmpty())
{
return false;
}
*/
return false;
}
您的最终目标是什么?可能有不同的方法来处理这种情况 – Zac
包含此excel数据表的任何行的对象的列表,即使其中一列为空(在该区域内) –