如何使用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
我已編輯添加表中數據存在問題 – user3083447
@ user3083447更新的新groovy函數...試試嗎?可能需要跳過標題 –