简体   繁体   English

使用jxl读取Excel工作表不能超过255行

[英]Reading Excel sheet using jxl is not working more than 255 rows

I try to read the Excel sheet which is old format using jxl jar, it works fine but after 255 row it throws error : 我尝试使用jxl jar读取旧格式的Excel工作表,它工作正常,但在255行后抛出错误:

java.lang.ArrayIndexOutOfBoundsException: 255

I cant read with Poi jar because of it is in old format, Any help much appreciated! 我无法使用Poi jar阅读,因为它是旧格式,非常感谢任何帮助!

Below is java code snippet given below: 下面是下面给出的Java代码段:

 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;
}

Your problem is on this line: 您的问题在这条线上:

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

You can't just insert 1000 because you don't know if every sheet has 1000 rows. 您不能只插入1000因为您不知道每个工作表是否都有1000行。 Just use the getRows() method from the Sheet object(As it already shows in the comment above the line): 只需使用Sheet对象中的getRows()方法(如该行上方的注释中所示):

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

That way you can't receive an ArrayIndexOutOfBoundsException because the loop stops after all rows are done. 这样一来,您就不会收到ArrayIndexOutOfBoundsException,因为循环在所有行完成之后会停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM