2013-12-17 94 views
-2

如何使用Groovy從數組函數返回數組?我目前正在用下面的代碼嘗試它,但當我執行代碼時出現錯誤!我希望能夠從一個數組函數中獲取值,我從Excel中提取數據,並且希望能夠使用從函數中檢索的任意值。誰能幫忙?如何使用groovy從數組函數中返回數組?

代碼:

package Check 

import org.apache.commons.io.FileUtils; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
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; 

class Second { 

    static main(args) { 
     //def alpo = new String[2][3] 
     def alpo = getTableArray("KSuite"); 
     println "£34." + alpo[1][2] 
    } 

    public static String[][] getTableArray(String xlSheet) throws Exception { 
     FileInputStream input = new FileInputStream(new File("C:\\Temp\\KSuite.xlsx")); 
     //System.out.println(input.path); 
     XSSFWorkbook workbook = new XSSFWorkbook(input); 
     //System.out.println(xlSheet); 
     XSSFSheet sheet = workbook.getSheet(xlSheet); 
     int ci;//,cj; 
     XSSFRow r = sheet.getRow(sheet.getFirstRowNum()); 
     int jMax=Math.max(r.getLastCellNum()-r.getFirstCellNum(),2); 
     int iMax=sheet.getLastRowNum()-sheet.getFirstRowNum(); 
     def tabArray= new String[sheet.getLastRowNum()-sheet.getFirstRowNum()][jMax]; 
     //System.out.println(sheet.getLastRowNum()-sheet.getFirstRowNum()); 
     //System.out.println(jMax); 

     for (int i = sheet.getFirstRowNum()+1; i <= sheet.getLastRowNum(); i++) { 
      ci = i - 1; 
      r = sheet.getRow(i); 
      for (int j = 0; j < jMax; j++) { 
       XSSFCell c = r.getCell(j, r.CREATE_NULL_AS_BLANK); 
       if(c.getCellType() == 1) { 
        tabArray[ci][j]=c.getStringCellValue(); 
       } else { 
        tabArray[ci][j]=((float)(c.getNumericCellValue()) + ""); 
       } 
      } 
     } 
     input.close(); 
     // return(tabArray); 
     // return(cj); 

     return(tabArray); 
     // return(cj); 
    } 
} 

錯誤:

Caught: java.lang.ArrayIndexOutOfBoundsException: 2 
    java.lang.ArrayIndexOutOfBoundsException: 2 
at Check.Second.main(Second.groovy:15) 

表KSuite在KSuite.xlsx:

Script Run 
Login, Yes 
Workspaces No 
Users, Yes 

回答

1

這很難說,因爲我不知道你的輸入文件,而你的代碼有點令人困惑,但作爲猜測,在這一行這裏:

int jMax=Math.max(r.getLastCellNum()-r.getFirstCellNum(),2); 

您設定的最低數量爲次級維度2

然後在這裏:

println "£34." + alpo[1][2] 

你得到的返回數組的第3元。我以爲它只有長2

我也有在Groovifying您getTableArray方法一展身手,並與上前:

String[][] getTableArray(File xls, String sheetName, int skip=1) { 
    xls.withInputStream { input -> 
     new XSSFWorkbook(input).with { workbook -> 
      getSheet(sheetName).with { sheet -> 
       (sheet.firstRowNum+skip..sheet.lastRowNum).collect { rowIdx -> 
        getRow(rowIdx).with { row -> 
         (row.firstCellNum..<row.lastCellNum).collect { cellIdx -> 
          row.getCell(cellIdx, CREATE_NULL_AS_BLANK).stringCellValue 
         } 
        } 
       } 
      } 
     } 
    } 
} 

def results = getTableArray(new File('/tmp/test.xlsx'), 'Sheet1') 

assert results[ 1 ] == [ 'Workspaces', 'No' ] 
+0

我已編輯添加表中數據存在問題 – user3083447

+0

@ user3083447更新的新groovy函數...試試嗎?可能需要跳過標題 –

0

這似乎已經解決了這一問題,並讓我來檢索Excel數據文件中使用函數:

package Check 

import org.apache.commons.io.FileUtils; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
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; 

class Second { 

    static main(args) { 
    //def alpo = new String[2][3] 
     def String[][] alpo = getTableArray("KSuite"); 
     println alpo[1][1] 
    } 

    public static String[][] getTableArray(String xlSheet) throws Exception{ 
     //String[][] tabArray=null; 

          //System.out.println(xlSheet); 
       FileInputStream input = new FileInputStream(new File("C:\\Temp\\KSuite.xlsx")); 
       //System.out.println(input.path); 
       XSSFWorkbook workbook = new XSSFWorkbook(input); 
          //System.out.println(xlSheet); 
        XSSFSheet sheet = workbook.getSheet(xlSheet); 
        int ci;//,cj; 
        XSSFRow r = sheet.getRow(sheet.getFirstRowNum()); 
        int jMax=Math.max(r.getLastCellNum()-r.getFirstCellNum(),2); 
        int iMax=sheet.getLastRowNum()-sheet.getFirstRowNum(); 
        def tabArray= new String[sheet.getLastRowNum()-sheet.getFirstRowNum()][jMax]; 
         //System.out.println(sheet.getLastRowNum()-sheet.getFirstRowNum()); 
         //System.out.println(jMax); 

        //ci=0; 
        for (int i = sheet.getFirstRowNum()+1; i <= 
          sheet.getLastRowNum(); i++) { 
         ci = i - 1; 
          r = sheet.getRow(i); 

         //int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT); 

         //cj=0; 
         for (int j = 0; j < jMax; j++) { 
          //tabArray[1][1] = "rr"; 
          //System.out.println(tabArray[1][1]); 
          XSSFCell c = r.getCell(j, r.CREATE_NULL_AS_BLANK); 
          if(c.getCellType() == 1) { 
          //System.out.println(c.getStringCellValue()); 
           tabArray[ci][j]=c.getStringCellValue(); 
            //System.out.println(tabArray[ci][j]); 
           } else { 
          //println c.getCellType() 
            tabArray[ci][j]=(c.getNumericCellValue().toString()); 
           //tabArray[ci][j]=c.getStringCellValue(); 
            //System.out.println(tabArray[ci][j]); 

           } 
         } 
        } 
        input.close(); 
        // return(tabArray); 
//     return(cj); 

       return(tabArray); 
//    return(cj); 
     } 
} 

輸出結果:

沒有

+0

只需要正確定義變量「alpo」,並將其保持在 – user3083447

+0

範圍內這不就是我在回答中所說的嗎? ;-) –

+0

是的,但我通過將「String [] []」放入def來整理,但由於這不是必須的,所以我會給你一個+1。 – user3083447