我尝试使用jxl读取Excel文件。它给出了StringIndexOutOfBoundsException。我不知道为什么要为这段代码得到它。

public class TestExcelReader {

    public static void main(String args[]) throws BiffException, IOException{
        File file=new File("D:\\TestExcel.xls");
        Workbook workbook=Workbook.getWorkbook(file);  \\Line number 14

        Sheet sheet=workbook.getSheet(0);
        System.out.println("No of rows is "+sheet.getRows());
    }

}


我的例外:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 68
    at java.lang.String.checkBounds(Unknown Source)
    at java.lang.String.<init>(Unknown Source)
    at jxl.biff.StringHelper.getString(StringHelper.java:164)
    at jxl.read.biff.WriteAccessRecord.<init>(WriteAccessRecord.java:56)
    at jxl.read.biff.WorkbookParser.parse(WorkbookParser.java:821)
    at jxl.Workbook.getWorkbook(Workbook.java:237)
    at jxl.Workbook.getWorkbook(Workbook.java:198)
    at ext.gt.test.TestExcelReader.main(TestExcelReader.java:14)

最佳答案

问题是因为您使用的是Excel95文件,但不幸的是,似乎不支持该文件(仅支持Excel97之后的文件)。

在这里看看
http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html#getWorkbook(java.io.InputStream)

和这里

http://www.java-forum.org/allgemeine-java-themen/140138-java-tabelle-auslesen-xls-excel-fehler.html

08-06 19:14