Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在读取excel文件时识别单元格为空、null或空_Java_Excel_Apache Poi_Xssf - Fatal编程技术网

Java 如何在读取excel文件时识别单元格为空、null或空

Java 如何在读取excel文件时识别单元格为空、null或空,java,excel,apache-poi,xssf,Java,Excel,Apache Poi,Xssf,我读取excel文件以传递某些数据字段的输入。但是当我运行程序时,一些单元格值返回为null,一些为空。实际上,当我打开excel文件时,单元格中没有可用的值 如何手动识别excel单元格是否为空 对于某些场景,我需要将空白值传递给字段。 如果单元格为空,我无法将值传递给字段 引导我伸出手来 代码: public static void readAllData() throws IOException{ Object result = null; String sheetNa

我读取excel文件以传递某些数据字段的输入。但是当我运行程序时,一些单元格值返回为null,一些为空。实际上,当我打开excel文件时,单元格中没有可用的值

如何手动识别excel单元格是否为空

对于某些场景,我需要将空白值传递给字段。 如果单元格为空,我无法将值传递给字段

引导我伸出手来

代码:

public static void readAllData() throws IOException{

    Object result = null;

    String sheetName = "Testsheet";

    String filePathxlsx = "C:/Users/MSTEMP/Documents/Files/Testxssf.xlsx";

    try

    {

     FileInputStream in = new FileInputStream(filePathxlsx);

     File file = new File(filePathxlsx);



    if(file.isFile() && file.exists()){

        XSSFWorkbook xworkbook = new XSSFWorkbook(in);
        XSSFSheet xsheet=xworkbook.getSheet(sheetName);
        int totalRows = xsheet.getLastRowNum();

        for(int row =1; row<totalRows;row++){
            xrow=xsheet.getRow(row);
            int totalCells=xrow.getLastCellNum();

            for(int cell =0; cell<totalCells;cell++){
                if(xrow != null)
                {
                    xcell= xrow.getCell(cell);

                    if(xcell!=null)
                    {
                        switch (xcell.getCellType()) {

                            case Cell.CELL_TYPE_NUMERIC:// numeric value in excel
                                if(DateUtil.isCellDateFormatted(xcell)){

                                    Date myDate = xcell.getDateCellValue();
                                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
                                    result = formatter.format(myDate);
                                    //System.out.println("Today : " + result);  
                                }
                                else{
                                    result = new BigDecimal(xcell.getNumericCellValue()).toPlainString();
                                }
                                break;

                            case Cell.CELL_TYPE_STRING: // string value in excel
                                result = xcell.getStringCellValue();
                                break;

                            case Cell.CELL_TYPE_BOOLEAN: // boolean value in excel
                                result = xcell.getBooleanCellValue();
                                break;

                            case Cell.CELL_TYPE_BLANK: // blank value in excel
                                result = xcell.getStringCellValue();
                                break;

                            case Cell.CELL_TYPE_ERROR: // Error value in excel
                                result = xcell.getErrorCellValue()+"";
                                break;
                            }
                        }
                        else
                        {
                            System.out.println("Cell is empty");
                        }
                    System.out.println("Value "+result);
                }
                else
                {
                    System.out.println("Row is empty");
                }
            }    
        }
    }
    inputStream.close();
}
catch (Exception ex){
    ex.printStackTrace();
}
public static void readAllData()引发IOException{
对象结果=空;
String sheetName=“Testsheet”;
字符串filePathxlsx=“C:/Users/MSTEMP/Documents/Files/Testxssf.xlsx”;
尝试
{
FileInputStream in=新的FileInputStream(filePathxlsx);
File File=新文件(filePathxlsx);
if(file.isFile()&&file.exists()){
XSSFWorkbook xworkbook=新XSSFWorkbook(在中);
XSSFSheet xsheet=xworkbook.getSheet(sheetName);
int totalRows=xsheet.getLastRowNum();

对于(int row=1;row不确定为什么要手动标识单元格类型,但如果要将大小写“null”或“blank”视为单个大小写,则可以使用row.getCell的另一个版本,该版本使用第二个参数指定单元格类型

因此:

将成为:

 xcell = xrow.getCell(cell, Row.RETURN_NULL_AS_BLANK);

因此,在任何情况下,单元格都有值或为空,但从不为空。

不确定为什么要手动标识单元格类型,但如果要将大小写“null”或“blank”视为单个大小写,则可以使用Row.getCell的另一个版本,该版本使用第二个参数来指定大小写

因此:

将成为:

 xcell = xrow.getCell(cell, Row.RETURN_NULL_AS_BLANK);
因此,在任何情况下,单元格都有一个值或为空,但从不为空