检查Apache POI库

时间:2015-12-23 13:02:11

标签: java apache-poi

我是Java Apache POI库。在这里,我想在下面的方法(getExcellContent)中检查给定的单元格是否为空。我该怎么检查?

public String getExcellContent(String sheetName, int row, int col){     
    String data = null;
    sheet1              = wb.getSheet(sheetName);       
    int getType = sheet1.getRow(row).getCell(col).getCellType();
    if(getType == 0) {
        Double data1 = sheet1.getRow(row).getCell(col).getNumericCellValue();
        int x1  =  data1.intValue();
        data = String.valueOf(x1);
    }
    else if(getType == 1) {
        data = sheet1.getRow(row).getCell(col).getStringCellValue();
    }else {
        data = "Nothing";
    }
    return data;        
}

如果传递的单元格为空,我将收到致命错误,如果我正在使用getCellType()(“int getType = sheet1.getRow(row).getCell(col).getCellType();”)。我想在使用getCellType之前检查给定的单元格是否为空。

int getType = sheet1.getRow(row).getCell(col).getCellType();

请参阅完整代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcellData {
    XSSFWorkbook wb;
    XSSFSheet sheet1;

    public ReadExcellData(String excelPath){        
        try {
            File src            = new File(excelPath);
            FileInputStream fis = new FileInputStream(src);
            wb                  = new XSSFWorkbook(fis);
            sheet1              = wb.getSheet("user");
        } catch (FileNotFoundException e) {         
            e.printStackTrace();
        } catch (IOException e) {           
            e.printStackTrace();
        }
    }

    public String getExcellData(String sheetName, int row, int col){        
        sheet1              = wb.getSheet(sheetName);
        String data = sheet1.getRow(row).getCell(col).getStringCellValue();
        return data;        
    }

    public String getExcellContent(String sheetName, int row, int col){     
        String data = null;
        sheet1              = wb.getSheet(sheetName);       
        int getType = sheet1.getRow(row).getCell(col).getCellType();
        if(getType == 0) {
            Double data1 = sheet1.getRow(row).getCell(col).getNumericCellValue();
            int x1  =  data1.intValue();
            data = String.valueOf(x1);
        }
        else if(getType == 1) {
            data = sheet1.getRow(row).getCell(col).getStringCellValue();
        }else {
            data = "Nothing";
        }
        return data;        
    }

    public int getActualDataType(String sheetName, int row, int col){
        sheet1              = wb.getSheet(sheetName);
        int getType = sheet1.getRow(row).getCell(col).getCellType();
        return getType;
    }

    public Double getExcellNumericData(String sheetName, int row, int col){     
        sheet1              = wb.getSheet(sheetName);
        double data = sheet1.getRow(row).getCell(col).getNumericCellValue();
        return data;        
    }

    public int getRowCount(String sheetName) {
        int rowNum = wb.getSheet(sheetName).getLastRowNum();
        rowNum = rowNum + 1;
        return rowNum;
    }
}

任何人都请帮忙。

1 个答案:

答案 0 :(得分:1)

正如评论中所建议的,您需要创建单元格&检查其 null 值。 您可能希望更改getExcelContent方法,如下所示。

 public String getExcellContent(String sheetName, int row, int col){     
        String data = null;
        sheet1  = wb.getSheet(sheetName);  
        XSSFCell gcell=null;
        XSSFRow row1=sheet1.getRow(row);//Create Row.
        if(row1!=null)
        {

        gcell=row1.getCell(col);//Create Cell
        if(gcell!=null)
        {
        int getType = gcell.getCellType();
        if(getType == 0) {
            Double data1 = gcell.getNumericCellValue();
            int x1  =  data1.intValue();
            data = String.valueOf(x1);
        }
        else if(getType == 1) {
            data = gcell.getStringCellValue();
        }else {
            data = "Nothing";
        }
        }
        }
        return data;        
    }

这对我有用。试一试。