jeudi 19 décembre 2019

This program is about reading data from xlsx file in selenium. but I am getting "Incompatible operand types int and CellType"

I am writing this code to read data from excel file. code is incomplete because and getting in if in loop for this condition "cell.getCellType()==CellType.STRING". i am using this condition to check celltype. the excel file and using is in .xlsx formate. This same program is rinnung properly with .xls file.

package mypackage;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.CellType;
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;

public class ExcelFile_xlsx {

    public static String excelFilePath, testSheetName, QuizTitle;
    public static String[][] InputExcelData;
    public static int numOfInputRows, numOfInputCol,totalNumberOfQue;

    public static void main(String[] args) throws Exception{

        excelFilePath = "excel file path";
        testSheetName = "sheet name";

        File src = new File(excelFilePath);
        FileInputStream file = new FileInputStream(src);
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheet(testSheetName);

        numOfInputRows = sheet.getLastRowNum()+1;
        numOfInputCol = sheet.getRow(0).getLastCellNum();
        totalNumberOfQue = numOfInputRows-1;
        InputExcelData = new String[numOfInputRows][numOfInputCol];

        for (int j=0; j < numOfInputRows; j++){
            XSSFRow row = sheet.getRow(j);
            for (int k = 0; k < numOfInputCol; k++) {
                XSSFCell cell = row.getCell(k);
                String inputValue ;
                 if (cell.getCellType()==CellType.STRING){  // getting error on this line
                     inputValue = cell.getStringCellValue();
                 }
                 else{
                     double numValue = cell.getNumericCellValue();
                     inputValue = String.valueOf(numValue);
                 }
                 InputExcelData[j][k]= inputValue;
            }
        }   
    }
}

Aucun commentaire:

Enregistrer un commentaire