如何使用apache poi从excel表读取数值(即double值)

时间:2018-02-08 10:59:21

标签: selenium testng-dataprovider

当我将数值传递到Excel工作表时,它会占用两倍,因此存在DataProvider Mismatch异常。如何处理此异常以及如何仅传递数值而不是双值?

以下是我的代码:

@DataProvider(name = "testdata")

public Object[][] TestDataFeed() throws Exception

{
    FileInputStream fis = new 

    FileInputStream("C:\\Users\\vidhya.r\\Desktop\\Testdata.xlsx");

    XSSFWorkbook wb = new XSSFWorkbook(fis);

    XSSFSheet sh1 = wb.getSheetAt(2);

    int numrow = sh1.getLastRowNum() + 1;

    int colnum = sh1.getRow(0).getLastCellNum();

    System.out.println("numrow ======= " + numrow);

    System.out.println("colnum ======= " + colnum);

    Object[][] data = new Object[numrow - 1][colnum];

    System.out.println("data.length ======= " + data.length);

    for (int i = 1; i < numrow; i++)

    {

        Row row = sh1.getRow(i);

        for (int j = 1; j < row.getLastCellNum(); j++)

        {
            int cellValue = sh1.getRow(i).getCell(j).getCellType();

            if (cellValue == Cell.CELL_TYPE_NUMERIC)

            {

                Object obj = 
                sh1.getRow(i).getCell(j).getNumericCellValue();

                data[i - 1][j-1] = obj;
                System.out.println("obj =num= " + obj);
            }

            else if (cellValue == Cell.CELL_TYPE_STRING)

            {

                Object obj = 
                sh1.getRow(i).getCell(j).getStringCellValue();

                data[i - 1][j-1] = obj;
                System.out.println("obj =str= " + obj);

            }
        }
    }
  return data;
  }

请帮我处理这个

1 个答案:

答案 0 :(得分:0)

以下是解决问题的方法......

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {

        private static FileInputStream fileInputStream;
        private static XSSFWorkbook workbook;
        private static XSSFSheet sheet;
        private static XSSFCell cell;

        @DataProvider
        public Object[][] dataMethod() {
            Object[][] arrayObjects = getExcelData("C:/Eclipse/testdata.xlsx");
            return arrayObjects;    
        }

        @Test(dataProvider = "dataMethod")
        public void testMethod(String username, String password) {
            System.out.println("Username -> "+username+" and Password -> "+username);
        }

        public String[][] getExcelData(String fileName){

            String[][] arrayExcelData = null;
            try{
                fileInputStream = new FileInputStream(fileName);
                workbook = new XSSFWorkbook(fileInputStream);
                sheet = workbook.getSheetAt(0);

                int totalRows = sheet.getPhysicalNumberOfRows();
                int totalCols = sheet.getRow(0).getPhysicalNumberOfCells();

                arrayExcelData = new String[totalRows-1][totalCols];
                int startRow = 1;
                int startCol = 0;

                    for(int i = startRow; i < totalRows; i++){
                        for(int j = startCol; j < totalCols; j++){
                            arrayExcelData[i-1][j] = getCellData(i,j);
                        }
                    } 
                fileInputStream.close();
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }

            return arrayExcelData;
        }

        public static String getCellData(int rowNum, int colNum){
            String cellData = "";
            try{
                cell = sheet.getRow(rowNum).getCell(colNum);
                switch(cell.getCellTypeEnum()) {
                    case BOOLEAN:
                        cellData = cell.getRawValue();
                        break;
                    case NUMERIC:
                        cellData = cell.getRawValue();
                        break;
                    case STRING:
                        cellData = cell.getStringCellValue();
                        break;
                    default:
                        break;
                }

            }catch(Exception e){
                e.printStackTrace();
            }
            return cellData;
        }
    }