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

时间:2015-10-12 10:05:50

标签: java excel apache-poi

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

java.lang.ArrayIndexOutOfBoundsException: 255

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

以下是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;
}

1 个答案:

答案 0 :(得分:1)

你的问题就在这一行:

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

您不能只插入1000,因为您不知道每张工作表是否有1000行。只需使用getRows()对象中的Sheet方法(正如它已在行上方的注释中所示):

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

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