【问题标题】:Reading Excel sheet using jxl is not working more than 255 rows使用 jxl 读取 Excel 工作表的行数不超过 255 行
【发布时间】:2015-10-12 10:05:49
【问题描述】:

我尝试使用 jxl jar 读取旧格式的 Excel 表格,它工作正常,但在 255 行之后它会抛出错误:

java.lang.ArrayIndexOutOfBoundsException: 255

我无法使用 Poi jar 阅读,因为它是旧格式,非常感谢任何帮助!

下面是java代码sn-p:

 public HashMap<String, Float> readAllRowsOnePoint(String path, String sheetname) throws BiffException, IOException {
    System.out.println("Path download" + path);
    FileInputStream fs = new FileInputStream(path);
    Workbook wb = Workbook.getWorkbook(fs);
    Sheet sheet = wb.getSheet(sheetname);
    // To get the number of rows present in sheet
    int totalNoOfRows = 500;
    System.out.println("The rows count is " + totalNoOfRows);
    HashMap<String, Float> map = new HashMap<>();
    for (int row = 2; row < totalNoOfRows; row++) {
        try {
            Cell dateCell = null;
            Cell psidCell = null;
            Cell nameCell = null;
            Cell quantityCell = null;
            Cell billingCell = null;

            try {
             dateCell = sheet.getCell(0, row);
             psidCell = sheet.getCell(1, row);
             nameCell = sheet.getCell(2, row);
             quantityCell = sheet.getCell(8, row);
             billingCell = sheet.getCell(10, row);
            } catch(Exception e){
                //  e.printStackTrace();
            }
        String date = dateCell.getContents().trim();
        String psid = psidCell.getContents().trim();
        String name = nameCell.getContents().trim();
        String quantity = quantityCell.getContents().trim();
        String billing = billingCell.getContents().trim();

        System.out.println();
        } catch(Exception e) {
            System.out.println("Error in row " + row + " Exception " );
        }
    }
    return map;
}

【问题讨论】:

  • 你找到答案了吗?我很确定它可以处理大于 255 的行,因为我的开始像你的一样窒息(ArrayIndexOutOfBoundsException),但我的在 1315+

标签: java excel apache-poi


【解决方案1】:

你的问题出在这一行:

for (int i = 0 ; i < 1000; i++)

您不能只插入1000,因为您不知道每张工作表是否有 1000 行。只需使用来自Sheet 对象的getRows() 方法(正如它已经在行上方的评论中显示的那样):

for (int i = 0 ; i < s.getRows(); i++)

这样你就不会收到 ArrayIndexOutOfBoundsException 因为循环在所有行完成后停止。

【讨论】:

  • 出现异常并被捕获,代码 sn-p 如下:try { dateCell = sheet.getCell(0, row); psidCell = sheet.getCell(1, row); nameCell = sheet.getCell(2, row); quantityCell = sheet.getCell(8, row); billingCell = sheet.getCell(10, row); } catch(Exception e){ // e.printStackTrace(); }
  • 在您的问题中添加信息。还要显示发生错误的确切行。
  • dateCell = sheet.getCell(0, row);哪里出错
  • 那一行的单元格是空的。但是你已经完全改变了你的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 2014-11-05
  • 1970-01-01
  • 2011-03-24
相关资源
最近更新 更多