首页 > 解决方案 > “java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:68”是什么意思?

问题描述

当我使用 Workbook 获取文件路径中的 |XL 表的工作簿时,我不断收到此错误“java.lang.StringIndexOutOfBoundsException: String index out of range: 68”。

我最初认为这与文件路径的存储有关,但现在由于以下建议,我能够确定引发异常的位置。异常是从

Workbook wb = Workbook.getWorkbook(fs);

完整的代码。

public void readxls() {

try{
String FilePath = "C:\\Users\\names\\Documents\\details.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);

// TO get the access to the sheet
Sheet sh = wb.getSheet("Sheet1");

// To get the number of rows present in sheet
int totalNoOfRows = sh.getRows();

// To get the number of columns present in sheet
int totalNoOfCols = sh.getColumns();

for (int row = 1; row < totalNoOfRows; row++) {
System.out.println();



String a = sh.getCell(0, row).getContents();
String b = sh.getCell(1, row).getContents();
String c = sh.getCell(2, row).getContents();
String d = sh.getCell(3, row).getContents(); 
System.out.println("A= "+a+ "B=" +b+ "C=" +c+"D=" +d);
}}

catch (Exception e) {
System.out.println(e);
}}

我想遍历 XL 表中的单元格并将其打印出来。

标签: javastringexception

解决方案


使用那个

try{
   Workbook wb = Workbook.getWorkbook(new File("C:\\Users\\names\\Documents\\details.xls"));
} catch(Exception e){
   e.printStackTrace();
}

您应该使用 File 而不是 FileInputStream。如果发生任何错误,您将在 catch 块中看到


推荐阅读