java - Apache POI > 无法读取 Excel 工作表

标签 java apache selenium-webdriver webdriver apache-poi

Apache POI > 无法读取 Excel 工作表

  1. 我成功地将数据写入 Excel 工作表
  2. 当我使用代码访问 Excel 文档中的另一张工作表时,它不起作用

以下代码有效:

        priceband = p.getProperty("priceband");
    if(priceband.contains("nationala")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalA");
        ExcelUtils.setCellData("example22222", 1, 1);
    }

以下代码在尝试访问同一工作簿中的其他工作表时不起作用,运行代码时出现空指针异常:

    if(priceband.contains("nationala")) {
    ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalB");
    ExcelUtils.setCellData("example22222", 1, 1);
}


public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public FileInputStream fis = null;
public FileOutputStream fileOut = null;
public static String priceband;

// 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 {
        // Open the Excel file
        FileInputStream ExcelFile = new FileInputStream(Path);
        // Access the required test data sheet
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        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 {
        Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
        String CellData = Cell.getStringCellValue();
        return CellData;
    } catch (Exception e) {
        return "";
    }
}

// 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 {
        Row = ExcelWSheet.getRow(RowNum);
        Cell = Row.getCell(ColNum, Row.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(Constant.Path_TestData + Constant.File_TestData);
        ExcelWBook.write(fileOut);
        fileOut.flush();
        fileOut.close();

    } catch (Exception e) {
        throw (e);
    }
}

public static void setupExcelPriceband() throws Exception {
    Properties p = new Properties();
    FileInputStream fi = new FileInputStream("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PhFramework\\src\\main\\java\\PhFramework\\testData\\Setup.properties");
    p.load(fi);
    priceband = p.getProperty("priceband");
    if(priceband.contains("nationala")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData,"NationalA");
        ExcelUtils.setCellData("example22222", 1, 1);
    }
    else if (priceband.contains("nationalb")) {
        ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData, "NationalB");
        ExcelUtils.setCellData("df", 1, 1);
    }else {
    }
}

}

Excel file

最佳答案

@Potnuru Ravi 感谢您的帮助

下面的代码似乎已经成功了:

public void setCellData(String sheetName, int colNum, int rowNum, String str) {
    int index = workbook.getSheetIndex(sheetName);
    sheet = workbook.getSheetAt(index);
    row = sheet.getRow(rowNum);
    cell = row.createCell(colNum);
    cell.setCellValue(str);
    try {
        fileOut = new FileOutputStream(path);

        try {
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

关于java - Apache POI > 无法读取 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829758/

相关文章:

java - @RequestMapping 在 Spring 版本 1.5.2.RELEASE 中不起作用

php - 在 Digital Ocean Ubuntu 上设置 Laravel 5.1

java - Selenium Grid 将值并行发送到一个浏览器

java - Websocket 文件上传速度问题(Java websocket API 和 Javascript)

Java - 为什么类型参数不能在 catch 子句中?

javascript - AngularJS PHP CORS 不适用于 FireFox

java - WebDriverEventListener 中的 onException 何时触发?

python - Selenium 很慢,还是我的代码有误?

java - 如何使用 play framework 2.1.0 更改 future 超时

来自 Chrome 扩展的 ajax 已处理,但收到 responseText =""和 status=0