Method Workbook.createWorkbook method in JexcelAPI throwing indexoutofbounds exception

StackOverflow https://stackoverflow.com/questions/17984826

  •  04-06-2022
  •  | 
  •  

Frage

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!

War es hilfreich?

Lösung

Ok, so I have found out the issue that was causing this problem. In JexcelAPI, if you try to copy a workbook more than once using the overloaded CreateWorkbook method, there is a bug that happens if you have ever tried to change the format of any cell in the original Workbook in the past. Actually, you do not even have to have changed the format-- if you select only one cell and then go to the format cells window, click the "general" format (which it already was by default), and hit ok and save the workbook again, the error is thrown. But if you open the window and then select ok without actually clicking on "general", the error is not thrown. Evidently, it is having clicked one of the options in this window and pressed "ok", even if it was the same format as before, which triggers the error.

The worst part about this bug is that it is permanent... even thinking about changing the format (opening the "format cells" window and clicking around) of a cell will completely corrupt the workbook FOREVER. If someone else can corroborate this so that I know I'm not crazy, I would appreciate it. though I am very certain this is the case, because now I see a "formattingrecords" error at the top of the stack trace. Obviously somehow excel stores information as to whether a cell has had its format changed in the past. But this comes as a surprise to me at least.

best, Paul

Andere Tipps

I was having nearly same issue and I solved it with moving code to seperate class, make new object for every use of that code and issue dissapeared.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top