将从excel读取的数据传输到数组



我想将这些数据传输到一个数组中,用我从excel中读取的数据执行数学运算。我该怎么做?

import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import  jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
public class SimMod {
public static void main(String[] args) throws Exception {
File f=new File("C:\Users\data.xls");
Workbook Wb=Workbook.getWorkbook(f);
Sheet sh=Wb.getSheet(0);
int [] mathArray=new int[48];
int row=sh.getRows();
int col= sh.getColumns();
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
Cell c=sh.getCell(j,i);
System.out.print(c.getContents());
}
System.out.println("   ");
}
}
} 

不要使用动态数组。请改用ArrayList。将int [] mathArray=new int[48]更改为ArrayList<Integer> mathArray = new ArrayList<>();

然后在线路System.out.print(c.getContents());之后或之前添加线路mathArray.add(c.getContents())

编辑:如果你想有单独的行和列,你可以这样做:

public static void main(String[] args) throws Exception {
File f=new File("C:\Users\data.xls");
Workbook Wb=Workbook.getWorkbook(f);
Sheet sh=Wb.getSheet(0);
ArrayList<ArrayList<Integer>> mathArray=new ArrayList<>();
int row=sh.getRows();
int col= sh.getColumns();
for (int i=0;i<row;i++){
ArrayList<Integer> colArr = new ArrayList<>();
for (int j=0;j<col;j++){
Cell c=sh.getCell(j,i);
colArr.add(c.getContents());
}
mathArray.add(colArr);
}
}

现在您可以使用mathArray.get(i).get(j)访问第i行和第j列中的元素

最新更新