我一直在尝试读取 excel 表并将单元格中的数据插入程序中。我在某种程度上是成功的,但是当我尝试从位置 4 及以后的单元格中获取数据时,我得到了一个越界异常。不知道为什么会这样,因为我可以调用位置 0、1、2 和 3 就好了。这是我的代码。
public void readExcel(File file) {
try {
//Workbook newBook = new Workbook();
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook workbookOutput = Workbook.createWorkbook(new File("output.xls"));
// Workbook workbook = Workbook.getWorkbook(new File(“Res.xls”));
WritableSheet sheetOutput = workbookOutput.createSheet("First Sheet", 0);
sheet = workbook.getSheet(0);
boolean isnotnull = true;
int i = 0;
while(isnotnull){
//Cell a1 = sheet.getCell(0, 2);
try{
Cell a2 = sheet.getCell(0, i);
Cell a3 = sheet.getCell(1, i);
Cell a4 = sheet.getCell(2, i);
stringa1 = a2.getContents();
stringa2 = a3.getContents();
stringa3 = a4.getContents();
char c = stringa2.charAt(0);
char d = stringa2.charAt(1);
if(Character.isDigit(c) || Character.isDigit(d)){
System.out.println("found one");
stringa1 = stringa1 + "" + stringa2;
stringa2 = stringa3;
Cell a5 = sheet.getCell(3, i);
try{
Cell a10 = sheet.getCell(4, i);
// System.out.print("Cell" + a6.getContents());
}
catch(Exception e){
System.out.println(e);
}
stringa4 = a5.getContents();
stringa3 = stringa4;
// checkCells(1, i);
}
Label label = new Label(0, i, stringa1);
Label label2 = new Label(1, i, stringa2);
Label label3 = new Label(2, i, stringa3);
sheetOutput.addCell(label);
sheetOutput.addCell(label2);
sheetOutput.addCell(label3);
i++;
System.out.println(stringa1 + " |||||||||||| " + stringa2 + " |||||||| " + stringa3 + " " + i);
}catch(Exception e){
System.out.println(e);
isnotnull = false;
}
}
workbookOutput.write();
try{
workbookOutput.close();
}catch(Exception r){
System.out.println(r);
}
System.out.println(stringa1);
workbook.close();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}`enter code here`