【问题标题】:Need Logic for Running Excel by Java code using Apache POI需要使用 Apache POI 通过 Java 代码运行 Excel 的逻辑
【发布时间】:2017-08-28 17:40:12
【问题描述】:

**请帮助我,我想要一个场景,它将返回相应类名的所有测试方法(B 列),在这里我正在尝试创建一个函数,它将返回我特定的方法列表类 //我们可能会使用一些集合框架(因为我是 java 新手,不知道该怎么做)请看附图 **

@Test
	public void excel() throws Exception{

		ExcelUtils.setExcelFile("E:\\EclipseTests\\DemoTests\\src\\main\\java\\testdata\\testdata.xls", "Sheet1");
		int ic=ExcelUtils.getRowUsed();

		int row=utility.ExcelUtils.getRowContains("Class1", 0);
		System.out.println("row num  "+row);
		
		List<String>value=method("Class1",ic);//need to  get all the values of column B in the excel when 
		//i put class name/column 0 value in this function byietrating using loop
		}	
public static List<String>method(String classz ,int ic) throws Exception{
		for(int i=1;i<ic;i++){	
			
			List<String>sMethod=new ArrayList<String>();
			String sClassName=ExcelUtils.getCellData(i, 0);
			String sClassName1=ExcelUtils.getCellData(i-1, 0);
			if(sClassName1.equals(sClassName)  ){
				int row=utility.ExcelUtils.getRowContains(sClassName, 0);
				//need to write some logic which will return me a list which will contains 
				 //[Test1 ,Test2 ,Test3 ,Test4 ,Test5] for class1 
				sMethod.add("value from B column");// i.e Test1
				sMethod.add("value from B column");// i.e Test2
				sMethod.add("value from B column");// i.e Test3
				sMethod.add("value from B column");// i.e Test4
				sMethod.add("value from B column");// i.e Test5
			}

		}
		
		return sMethod;
		}
}

//similarly for class2 it should return [Test6,Test7,Test8,Test9,Test10]
//similarly for class3 it should return [Test6,Test11,Test12,Test13,Test14]

【问题讨论】:

    标签: java excel selenium automation apache-poi


    【解决方案1】:
    for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
      row = sheet.getRow(rowIndex);
      if (row != null) {
        String cellValue = null;
        for (int colIndex = 0; colIndex < row.getPhysicalNumberOfCells(); colIndex++) {
          if (colIndex == 1) { //as index of column B will be 1
            cell = row.getCell(colIndex);
            if (cell != null) {
              // Found column and there is value in the cell.
              cellValue = cell.getStringCellValue();
              break;
            }
        }
      }
    }
    

    将此单元格值变量添加到列表或数组中

    【讨论】:

    • 感谢 Poojan 的努力。它只给了我 B 列值,我想要的是,它应该只返回我相应列 a 值的 B 列值
    【解决方案2】:
    @SuppressWarnings("null")
    public static List<String>methods(String clasName){
    
        List<String>method=new ArrayList<String>();
        for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            row = sheet.getRow(rowIndex);
            if (row != null) {  
                HSSFCell cell=sheet.getRow(rowIndex).getCell(0);
                if(cell.getStringCellValue().equalsIgnoreCase(clasName)){
    
                    String value=sheet.getRow(rowIndex).getCell(1).getStringCellValue();
                    System.out.println( value);
                    method.add(value);
                }
            }
        }
    
        return method;
    }
    
    public static void main(String[] args) throws Exception {
    
        FileInputStream ExcelFile = new FileInputStream("E:\\EclipseTests\\DemoTests\\src\\main\\java\\testdata\\testdata.xls");
        wb=new HSSFWorkbook(ExcelFile);
        sheet=wb.getSheet("Sheet1");
    
        System.out.println("value is "+methods("Class3"));
        System.out.println("value is "+methods("Class1"));
        System.out.println("value is "+methods("Class4"));
    

    这就是我要找的..谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多