Android and JXL: ArrayIndexOutOfBoundException while creating WritableWorkbook
I am trying to modify xls file from my android device using JXL. The first step, if I understand well, is to copy the book. Here is my code that throws an exception on write:
java.io.File licencesFile = new java.io.File(LicencesService.getPath(mCtx) + "/" + pFileName);
java.io.File licencesFiletmp = new java.io.File(LicencesService.getPath(mCtx) + "/" + pFileName + ".tmp");
is = new FileInputStream(licencesFiletmp);
os = new FileOutputStream(licencesFile);
Workbook workbookTmp = Workbook.getWorkbook(is);
WritableWorkbook workbookFinal = Workbook.createWorkbook(os, workbookTmp);
//TODO code will come here when it'll work.
workbookFinal.write(); // <= Here is thrown an Exception
workbookFinal.close();
I haven't found any solution for this ... Any idea?
Thank you so much
Stack:
java.lang.ArrayIndexOutOfBoundsException: src.length=166 srcPos=0 dst.length=112 dstPos=0 length=166
at java.lang.System.arraycopy(Native Method)
at jxl.biff.StringHelper.getBytes(StringHelper.java:127)
at jxl.write.biff.WriteAccessRecord.<init>(WriteAccessRecord.java:59)
at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:726)
at fr.xxx.xxx.tasks.DriveUpdaterAsyncTask.updateXls(DriveUpdaterAsyncTask.java:170)
at fr.xxx.xxx.tasks.DriveUpdaterAsyncTask.doInBackground(DriveUpdaterAsyncTask.java:71)
at fr.xxx.xxx.tasks.DriveUpdaterAsyncTask.doInBackground(DriveUpdaterAsyncTask.java:1)
...etc.
+3
source to share
2 answers
In my case, the problem was caused by copying the sheet and setting some WorkbookSettings. I used these parameters as a parameter to the openWorkbook function and createWorkbook function. Removing them from the createWorkbook function fixed my problem.
//Load template workbook with settings
WorkbookSettings ws = new WorkbookSettings();
ws.setEncoding("Cp1252");
Workbook templateWorkbook = Workbook.getWorkbook(this.context.getAssets().open("template.xls"), ws);
//Create new workbook from templateWorkbook without settings
this.workbook = Workbook.createWorkbook(new File(this.location), templateWorkbook);
+2
source to share