Whenever I am trying to use template to create xls using jxl API running into weird ArrayIndexOutOfBoundException.
Here is the snippet of code I am trying to use
private static void readWorkSheet()throws Exception{
File file = new File("C:\\reports\\");
Map template = new HashMap();
File[] listFiles = file.listFiles();
for(File file1:listFiles){
if(file1.getName().endsWith(".xls")){
System.out.println(" ==> "+file1.getName());
Workbook workbookTemplate = Workbook.getWorkbook(file1);
template.put(file1.getName(), workbookTemplate);
}
}
System.out.println("template "+template);
Workbook templateWorkBook = (Workbook)template.get("TestReport.xls");
Sheet readSheet = templateWorkBook.getSheet("Sheet1");
WritableWorkbook copy = Workbook.createWorkbook(new File("c://myfile_copy2.xls"));
WritableSheet sheet = copy.createSheet("Test", 0);
for (int i = 0; i < readSheet.getRows(); i++) {
for (int j = 0; j < readSheet.getColumns(); j++) {
Cell readCell = readSheet.getCell(j, i);
CellFormat readFormat = readCell.getCellFormat();
if(readFormat != null && readCell.getContents() != null && readCell.getContents() != ""){
WritableCell newCell = new Label(i,j,readCell.getContents());
WritableCellFormat newFormat = new WritableCellFormat(readFormat);
newCell.setCellFormat(newFormat);
System.out.println("details of cell ["+i+", "+j+"]"+" Name = "+readCell.getContents());
System.out.println("details of newCell ["+i+", "+j+"]"+" Name = "+newCell.getContents());
sheet.addCell(newCell);
}
}
}
copy.write();
copy.close();
}
Not sure what I am missing in this !!!
Exception I am running into
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 56
at jxl.biff.IndexMapping.getNewIndex(IndexMapping.java:68)
at jxl.biff.XFRecord.rationalize(XFRecord.java:1667)
at jxl.biff.FormattingRecords.rationalize(FormattingRecords.java:443)
at jxl.write.biff.WritableWorkbookImpl.rationalize(WritableWorkbookImpl.java:1023)
at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:701)
at com.jxl.test.JXLTest.readWorkSheet(JXLTest.java:83)
at com.jxl.test.JXLTest.main(JXLTest.java:30)
The stack trace above does not appear to refer to any of your code. Did you include the complete stack trace from the exception?
以上就是ArrayIndexOutOfBoundException with jxl api when trying to copy content from one sheet to another的详细内容,更多请关注web前端其它相关文章!