获取异常java.lang.IndexOutOfBoundsException:索引:0,大小:0,同时使用Java生成Excel文件

时间:2018-08-13 15:59:44

标签: java excel spring-mvc jxl excelbuilder.js

我们在生成excel文件的过程中遇到日志下面的异常,我们正在使用jxl版本2.6:

SEVERE: Servlet.service() for servlet [webui] in context with path [/newui] threw exception [Request processing failed; nested exception is
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0] with root  causejava.lang.IndexOutOfBoundsException: Index: 0, Size: 0at 
java.util.ArrayList.RangeCheck(ArrayList.java:547)at 
java.util.ArrayList.get(ArrayList.java:322)at 
jxl.write.biff.WritableWorkbookImpl.getSheet(WritableWorkbookImpl.java:399)at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:767)at org.springframework.web.servlet.view.document.AbstractJExcelView.renderMergedOutputModel(AbstractJExcelView.java:142)

生成Excel文件的示例代码为(servlet代码段):

try {
    WritableWorkbook w = null;
    logger.info("ExcelBuilder Called");
    User loggedRcpt = (User)  request.getSession(true).getAttribute("user");
    OutputStream out = response.getOutputStream();
    w = Workbook.createWorkbook(out);
    WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
    WritableCellFormat cf = new WritableCellFormat(wf);
    cf.setAlignment(Alignment.LEFT);
    WritableCellFormat wc = new WritableCellFormat();
    cf.setAlignment(Alignment.LEFT);
    wc.setWrap(true);
    WritableFont writableFormat = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD);
    writableFormat.setItalic(true);
    response.setContentType("application/vnd.ms-excel");


    response.setHeader("Content-Disposition", "attachment; filename=" +filename+".xls");
    WritableSheet sheet = w.createSheet(filename, 0);
    int rowNo = 1;
    Label Empid= new Label(0, 0, "Emp ID", cf);
    sheet.setColumnView(0, 40);
    sheet.addCell(Empid);
    Label forenameCol = new Label(1, 0, "Forename", cf);
    sheet.setColumnView(1, 40);
    sheet.addCell(forenameCol);

    Employee Emp= (Employee) iterator.next();
    Label employeeId = new Label(0, rowNo, Emp.getEmployeeId());
    sheet.addCell(employeeId);
    sheet.addCell(forename);
    Label surname = new Label(2, rowNo, Emp.getSurname());
    }
    catch (Exception e) {
      logger.error(e);
    }
    finally {
      try {
          if(w!=null){
            w.write();
            w.close();
          }
      } catch (Exception e) {
          sysout.error(e);
      }
    }

请帮助我们解决此异常。 谢谢。

0 个答案:

没有答案