如何使用Eclipse中的apache POI从Excel读取Integer值?

时间:2015-08-23 21:12:40

标签: java eclipse excel

    import static org.testng.Assert.*;

    import org.apache.poi.hssf.usermodel.HSSFSheet;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.sl.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Row;
    public class TestCalcAPI {
    CalcAPI clac = new CalcAPI();

    @DataProvider(name = " myData")
    public Integer[][] myData(){
        Integer[][] data = new Integer[][]{
            {4,2,2},
            {1,1,0},
            {-3,3,-6},
            {2,1,1} 
        };
        return data;
        }
    @DataProvider(name = "excelReading")
    public void myExcel() throws Exception 
    {
        File file = new File("DataInputForCalc.xls");

        FileInputStream input = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(input);
        HSSFSheet sheet = workbook.getSheet("Sheet1");
        for (Row r : sheet)
        {
            int j = r.getLastCellNum();
            for (int i = 0; i<j;i++)
            {
                Integer res = (Integer)clac.addition(r.getCell(i+1).getNumericCellValue() , r.getCell(i+2).getNumericCellValue());
                assertEquals(res, r.getCell(0).getNumericCellValue());
            }

        }
    }


  @Test (dataProvider="myData")
  //public void testAdd(Integer exp,Integer a,Integer b)
  public void testAdd(Integer exp,Integer a,Integer b) {
      Integer res = clac.addition(a, b);
      assertEquals(res, exp);
  }
}

我为add方法创建了CalcAPI类,并为add方法调用了clac.addition(a,b)的对象。我使用Integer值作为参数。在这个TestNG类中,我为DataProvider创建了一个注释,一个是另一个从Excel读取的硬编码数据。从Excel读取时,我没有选择读取整数值。那么我该怎么用来从Excel中读取整数值

0 个答案:

没有答案