Apache POI MissingCellPolicy无法用于excel读写操作

时间:2018-04-23 19:27:58

标签: java excel apache apache-poi

我正在尝试使用Apache POI在Java中进行excel读写操作。当我试图传递excel中没有任何数据的单元格时,它应该返回空白但是 - Java Null指针异常正在抛出。但是,当我传递具有一些数据的单元格时,两个方法getCelldata和setCelldata都完美地工作。 这是代码段

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

    public class ExcelUtils {
                private static XSSFSheet ExcelWSheet;
                private static XSSFWorkbook ExcelWBook;
                private static XSSFCell xCell;
                private static XSSFRow xRow;

            //This method is to set the File path and to open the Excel file, Pass Excel Path and Sheetname as Arguments to this method

            public static void setExcelFile(String Path,String SheetName) throws Exception {
                try {

                    // Access the required test data sheet
                    FileInputStream inputStream = new FileInputStream(new File(Path));         
                    ExcelWBook = new XSSFWorkbook(inputStream);
                    ExcelWSheet = ExcelWBook.getSheet(SheetName);
                    } catch (Exception e){
                        throw (e);
                    }
            }

            //This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num

            public static String getCellData(int RowNum, int ColNum) throws Exception{
                try{
                    xCell = ExcelWSheet.getRow(RowNum).getCell(ColNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    String CellData = xCell.getStringCellValue();
                    return CellData;
                    }catch (Exception e){
                        throw(e);
                    }
            }

            //This method is to write in the Excel cell, Row num and Col num are the parameters

            public static void setCellData(String Result,  int RowNum, int ColNum) throws Exception {

                try{
                    xRow  = ExcelWSheet.getRow(RowNum);
                    xCell = xRow.getCell(ColNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
                    if (xCell == null) {
                        xCell = xRow.createCell(ColNum);
                        xCell.setCellValue(Result);
                        } else {
                            xCell.setCellValue(Result);
                        }
          // Constant variables Test Data path and Test Data file name

                        FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData);
                        ExcelWBook.write(fileOut);
                        fileOut.flush();
                        fileOut.close();
                        }catch(Exception e){
                            throw (e);
                    }
                }
    }

错误抛出线但预期xCell应该有空白值,因为我提供了MissingCellPolicy

xCell = ExcelWSheet.getRow(RowNum).getCell(ColNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);

xCell = xRow.getCell(ColNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);

先谢谢

1 个答案:

答案 0 :(得分:2)

可能你传递的是一个完全空行,这就是你的getRow()方法失败并给你一个NullPointerException的原因。

当你在一行中迭代列时,一些空白的单元格甚至可能不存在,这可能导致毫无疑问的代码抛出NullPointerException。 MissingCellPolicy仅适用于单元格。所以你不能把整行留空

CREATE_NULL_AS_BLANK - 如果返回的Cell不存在,则创建一个单元格类型为“blank”的新Cell,而不是返回null。这有助于方便地避免NullPointerExceptions。

RETURN_BLANK_AS_NULL - 即使单元格存在但单元格类型为“空白”,也返回null。这可以让您忽略容易存在的空白单元格。

RETURN_NULL_AND_BLANK - 不要修改现有结构;对于不存在的单元格返回null,如果存在,则返回空白单元格但其单元格类型为空。这是getCell重载的行为,不会占用MissingCellPolicy。