每当我尝试使用模板创建 xls 时使用 jxl API 运行到奇怪的 ArrayIndexOutOfBoundException。这是我尝试使用的代码片段
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();
}
不知道我在这缺少什么!!!
我遇到的异常
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)