簡體   English   中英

使用Apache POI通過Java代碼運行Excel的邏輯

[英]Need Logic for Running Excel by Java code using Apache POI

**請在此情況下為我提供幫助,在這種情況下,我需要一個場景,該場景將為我返回對應類名稱的所有測試方法(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");// ie Test1 sMethod.add("value from B column");// ie Test2 sMethod.add("value from B column");// ie Test3 sMethod.add("value from B column");// ie Test4 sMethod.add("value from B column");// ie 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] 

Excel工作表圖片

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;
        }
    }
  }
}

在列表或數組中添加此單元值變量

@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"));

這就是我想要的。謝謝大家

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM