每当单元格出现空白时,我都会在Excel文件中输入数据。但是当我输入下面的代码时,它会给出错误。 :
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 :(得分:0)
Cell有很多类型,你应该根据它的类型得到值,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;
}