java - “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 表中的单元格并将其打印出来。
解决方案
使用那个
try{
Workbook wb = Workbook.getWorkbook(new File("C:\\Users\\names\\Documents\\details.xls"));
} catch(Exception e){
e.printStackTrace();
}
您应该使用 File 而不是 FileInputStream。如果发生任何错误,您将在 catch 块中看到
推荐阅读
- flutter - 我可以在颤振仪表板中将图标更改为图像或表情符号吗
- python-3.x - 动态规划原始计算器代码优化
- angular - 根据输入在Angular中重新绘制画布线
- android - RecyclerView 滑动删除仍然显示不完整滑动的可绘制
- ip - Google Cloud SQL 上的几个查询
- node.js - 检查电子邮件是否已存在于带有异步和等待的 Mongo Atlas DB 中
- java - 如何将浮动比例转换为长比例?
- python-3.x - 为什么它在结果中显示这个(列表索引超出范围)?
- ios - Xocde - 错误 ITMS-90683 在 Info.plist 文件中缺少目的字符串
- javascript - 达到最大长度值后关注下一个输入 - reactjs