0

I am using JexcelAPI in java in order to manipulate excel files. I need to make 2 copies of a Workbook object, one a WritableWorkbook for further manipulation, and one simply to copy from the original Workbook and then save it, so that in case anything happens to the original object and it's writable copy, I will have a backup. This has been working for a long time until recently I started getting an ArrayIndexOutOfBOunds exception. The stack trace looks like this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 62, Size: 54 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at jxl.biff.FormattingRecords.getXFRecord(FormattingRecords.java:338) at jxl.read.biff.CellValue.getCellFormat(CellValue.java:144) at jxl.write.biff.CellValue.(CellValue.java:129) at jxl.write.biff.LabelRecord.(LabelRecord.java:116) at jxl.write.Label.(Label.java:79) at jxl.write.biff.SheetCopier.shallowCopyCell(SheetCopier.java:808) at jxl.write.biff.SheetCopier.shallowCopyCells(SheetCopier.java:934) at jxl.write.biff.SheetCopier.copySheet(SheetCopier.java:219) at jxl.write.biff.WritableSheetImpl.copy(WritableSheetImpl.java:1584) at jxl.write.biff.WritableWorkbookImpl.copyWorkbook(WritableWorkbookImpl.java:971) at jxl.write.biff.WritableWorkbookImpl.(WritableWorkbookImpl.java:343) at jxl.Workbook.createWorkbook(Workbook.java:339) at jxl.Workbook.createWorkbook(Workbook.java:320) at musicpred.musicpreddebugtest.main(musicpreddebugtest.java:17) Java Result: 1

I have boiled down the problem to the following snippet of code:

package musicpred;

import java.io.File;
 import jxl.Workbook;
import jxl.write.WritableWorkbook;
import java.io.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
public class musicpreddebugtest{
public static void main(String[] args) throws IOException, BiffException,                             
 WriteException{

 Workbook workbook = Workbook.getWorkbook(new File ("NBSCOMBINED.xls"));
 WritableWorkbook backup = Workbook.createWorkbook(new File("BACKUP.xls"),workbook);
    backup.write();
    backup.close();

    WritableWorkbook writableWorkbook = Workbook.createWorkbook(new      

 File("NBSCOMBINEDW.xls"), workbook);

}
}

I noticed that I can create both WritableWorkbooks at the same time, and I can even write() the second one (called "writableWorkbook"), but for some reason when I try to write() the second one ("backup"), it throws the error. I should also note that I don't see any particular significance to the outofbound indeces: I am do not have anything with dimension 54 in my workbook (rows, columns, or sheets), nor am I trying to replace anything with an array of length 62.

Does any have any idea why this may have suddenly started happening? I have a feeling it has an embarrassingly easy answer but I can't figure it out, so any help would be greatly appreciated!

4

2 回答 2

0

我遇到了几乎相同的问题,我通过将代码移动到单独的类来解决它,为每次使用该代码创建新对象并且问题消失了。

于 2013-11-13T19:07:45.810 回答
0

好的,所以我发现了导致此问题的问题。在 JexcelAPI 中,如果您尝试使用重载的 CreateWorkbook 方法多次复制工作簿,如果您过去曾尝试更改原始工作簿中任何单元格的格式,则会发生错误。实际上,您甚至不必更改格式——如果您只选择一个单元格,然后转到格式单元格窗口,单击“常规”格式(默认情况下已经是),然后点击确定并保存再次工作簿,错误被抛出。但是,如果您打开窗口然后选择确定而不实际单击“常规”,则不会引发错误。显然,它单击了此窗口中的一个选项并按下“确定”,即使它与以前的格式相同,也会触发错误。

这个错误最糟糕的部分是它是永久性的......即使考虑更改单元格的格式(打开“格式单元格”窗口并单击周围)也会永远彻底破坏工作簿。如果其他人可以证实这一点,以便我知道我没有疯,我将不胜感激。虽然我很确定情况确实如此,因为现在我在堆栈跟踪的顶部看到“格式化记录”错误。显然,excel 以某种方式存储了有关单元格过去是否已更改其格式的信息。但这至少让我感到意外。

最好的,保罗

于 2013-08-01T22:10:25.820 回答