读取单元格值与poi有特殊字符?

时间:2015-01-28 10:14:11

标签: excel selenium selenium-webdriver apache-poi

<script> 
    alert("Hello") 
</script>

我有:

Cell <value> = <script>alert("Hello")</script> and value = 2 bytes<characters>

我无法通过poi从excel文件中读取值 我使用selenium webdriver

请帮助我!!

我的代码:

public static String getCellData(int RowNum, int ColNum) throws Exception {
    String CellData = "";

    try {
        // get Test case name

        Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);

        if(Cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC){
            CellData = String.format("%.0f", Cell.getNumericCellValue());
        }

        if(Cell.getCellType()== XSSFCell.CELL_TYPE_STRING)
        {
            CellData = String .format("%s", Cell.getRichStringCellValue());
        }

        return CellData;
    } catch (Exception e) {
        return "";
    }
}

我的代码:阅读excel

public static Map<String, String> getTableArray(String FilePath, String SheetName,
        String sTestCaseName) throws Exception {

    //List tabArray = new ArrayList();
    // create Map
    Map<String, String> tabArray = new HashMap<String, String>();
    try {
        // input file excel from path local<FilePath>
        FileInputStream ExcelFile = new FileInputStream(new File(FilePath));
        // create a workbook as excel file
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        // get Sheet in workbook
        ExcelWSheet = ExcelWBook.getSheet(SheetName);

        int startCol = 2; // column start in excel file
        int totalCols = 10; // total column in excel file
        int iTestCaseRow = getRowContains(sTestCaseName, 1); // 
        //tabArray = new String[totalRows];
        for (int j = startCol; j <= totalCols; j++)
        {
            if(!getCellData(iTestCaseRow, j).equals("")){
                tabArray.put(getCellData(0,j), getCellData(iTestCaseRow, j));
            } else {
                continue;
            }

        }
    }
    catch (FileNotFoundException e)
    {
        System.out.println("Could not read the Excel sheet1");
        e.printStackTrace();
    }
    catch (IOException e)
    {
        System.out.println("Could not read the Excel sheet2");
        e.printStackTrace();
    }
    return (tabArray);
}

0 个答案:

没有答案