3

我正在尝试使用 JXL 从我的 Android 设备修改 xls 文件。第一步,如果我很好理解的话,就是复制工作簿。这是我在 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();

我没有找到任何解决方案...任何想法?

非常感谢

堆栈:

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.
4

2 回答 2

3

您需要指定写访问权限,如下所示:

WorkbookSettings settings = new WorkbookSettings();
settings.setWriteAccess("something");
WritableWorkbook workbook = Workbook.createWorkbook(out, Workbook.getWorkbook(getClass().getResourceAsStream("report.xls")),settings);
于 2013-02-26T19:51:44.953 回答
2

在我的情况下,问题是由复制工作表和设置一些 WorkbookSettings 引起的。我将这些设置用作 openWorkbook 函数和 createWorkbook 函数的参数。从 createWorkbook 函数中删除它们解决了我的问题。

//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);
于 2013-09-05T10:03:02.307 回答