2017-11-23 20 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; 
} 
を取得するには、このコードを使用して十分ではありません
関連する問題