3

i'm trying to modify an xls file from my Android device with JXL. The first step, if I have well understood, is to copy the workbook. Here's my code that throw the exception when 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 didn't found any solution for this... Any idea?

Thanks a lot

The 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.
castrogne
  • 521
  • 1
  • 4
  • 17

2 Answers2

3

You need to specify a write access, like this:

WorkbookSettings settings = new WorkbookSettings();
settings.setWriteAccess("something");
WritableWorkbook workbook = Workbook.createWorkbook(out, Workbook.getWorkbook(getClass().getResourceAsStream("report.xls")),settings);
chtenb
  • 14,924
  • 14
  • 78
  • 116
Richard
  • 31
  • 2
  • Good Solution! I had the same problem on Ubuntu Linux with code that ran without issues for years in a Windows Box. While omitting the Workbook settings also works, this approach is much better since it still allows to control all available settings. I used the System Propery `user.name` as value, since when you look into the Constructor of `jxl.write.biff.WriteAccessRecord` (to which the value is eventually passed) it expects a variable `userName`, even though there are no comments in the javadocs that explain how this value is actually used. – Till Kuhn Apr 13 '16 at 13:50
2

In my case the problem was caused by copying a sheet and setting some WorkbookSettings. I used these settings as a parameter to the openWorkbook function and the 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);