2017-11-23 8 views
0

셀이 발견되면 데이터를 Excel 파일로 저장하는 코드가 있습니다. 그러나 내가 아래 코드를 입력하면 오류가 발생합니다. :Apache POI 오류 메시지가 RETURN_BLANK_AS_NULL에 대해 표시됩니다.

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; 

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; 

import Config.Constants; 

public class ExcelUtils { 

private static XSSFSheet ExcelWSheet; 
private static XSSFWorkbook ExcelWBook; 
private static XSSFCell Cell; 
private static XSSFRow Row, xRow; 

public static void setExcelFile(String Path, String SheetName) throws Exception { 
    // TODO Auto-generated method stub 
    FileInputStream ExcelFile = new FileInputStream(Path); 
    ExcelWBook = new XSSFWorkbook(ExcelFile); 
    // ExcelWSheet = ExcelWBook.getSheet(SheetName); 
} 



public static String getCellData(int RowNum, int ColNum, String SheetName) throws Exception { 
    ExcelWSheet = ExcelWBook.getSheet(SheetName); 
    try { 


    Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); 
    String CellData = Cell.getStringCellValue(); 
    return CellData; 
    } catch (Exception e) { 
    return ""; 
    } 
} 
public static int getRowCount(String SheetName) { 
    ExcelWSheet = ExcelWBook.getSheet(SheetName); 
    int number = ExcelWSheet.getLastRowNum() + 1; 
    return number; 
} 

//This method is to get the Row number of the test case 
//This methods takes three arguments(Test Case name , Column Number & Sheet name) 
public static int getRowContains(String sTestCaseName, int colNum, String SheetName) throws Exception { 
    int i; 
    ExcelWSheet = ExcelWBook.getSheet(SheetName); 
    int rowCount = ExcelUtils.getRowCount(SheetName); 
    for (i = 0; i < rowCount; i++) { 
    if (ExcelUtils.getCellData(i, colNum, SheetName).equalsIgnoreCase(sTestCaseName)) { 
    break; 
    } 
    } 
    return i; 
} 

//This method is to get the count of the test steps of test case 
//This method takes three arguments (Sheet name, Test Case Id & Test case row number) 
public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception { 
    for (int i = iTestCaseStart; i <= ExcelUtils.getRowCount(SheetName); i++) { 
    if (!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))) { 
    int number = i; 
    return number; 
    } 
    } 
    ExcelWSheet = ExcelWBook.getSheet(SheetName); 
    int number = ExcelWSheet.getLastRowNum() + 1; 
    return number; 
} 

@SuppressWarnings("static-access") 
public static void setCellData(String Result, int RowNum, int ColNum, String SheetName) throws Exception { 
    try { 

    ExcelWSheet = ExcelWBook.getSheet(SheetName); 
    Row = ExcelWSheet.getRow(RowNum); 
    xRow = ExcelWSheet.getRow(RowNum); 
    // Row.RETURN_BLANK_AS_NULL; 

    // ExcelWBook.setMissingCellPolicy(Row.RETURN_BLANK_AS_NULL); 
    //Cell = Row.getCell(ColNum, Blank); 
    Cell = xRow.getCell(ColNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL); 

    if (Cell == null) { 
    Cell = Row.createCell(ColNum); 
    Cell.setCellValue(Result); 
    } else { 
    Cell.setCellValue(Result); 
    } 
    // Constant variables Test Data path and Test Data file name 
    FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData); 
    //ExcelWBook.write(fileOut); 
    ExcelWBook.write(fileOut); 
    //fileOut.flush(); 
    fileOut.close(); 
    ExcelWBook = new XSSFWorkbook(new FileInputStream(Constants.Path_TestData)); 
    } catch (Exception e) { 
    DriverScript.bResult = false; 
    } 
} 
} 

오류 메시지 :

MissingCellPolicy를 해결할 수없는 또는 내가 사용할 때 반면 필드

없는 수 "Row.RETURN_BLANK_AS_NULL는"이 방법은 사용되지 않습니다 말한다. 이 두 문제에 대한 코드를 성공적으로 실행하기위한 몇 가지 해결책이 있습니까?

답변

0

세포는 여러 종류가 있고 그 종류에 따라 값을 얻어야한다, getStringCellValue는()

가 나는 종종

switch (cell.getCellTypeEnum()) { 
    case FORMULA: 
     CellValue cellValue = evaluator.evaluate(cell); 
     switch (cellValue.getCellTypeEnum()) { 
      case BOOLEAN: 
       value = Boolean.toString(cellValue.getBooleanValue()); 
       break; 
      case NUMERIC: 
       value = Double.toString(cellValue.getNumberValue()); 
       break; 
      case STRING: 
       value = cellValue.getStringValue(); 
       break; 
     } 
     break; 
    case BOOLEAN: 
     value = Boolean.toString(cell.getBooleanCellValue()); 
     break; 
    case NUMERIC: 
     value = Double.toString(cell.getNumericCellValue()); 
     break; 
    default: 
     value = cell.getStringCellValue(); 
     break; 
} 
를 얻기 위해이 코드를 사용하는 것만으로는 충분하지 않습니다