jxl在linux环境下报java.lang.ArrayIndexOutOfBoundsException


转自:http://lzqdll.blog.163.com/blog/static/200607183201111635447767/


java.lang.ArrayIndexOutOfBoundsException
Caused by: java.lang.System.arraycopy(Native Method)
Caused by: jxl.biff.StringHelper.getBytes(StringHelper.java:127)
Caused by: jxl.write.biff.WriteAccessRecord.<init>(WriteAccessRecord.java:59)
Caused by: jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:726)


以上错误只有在linux环境下才会出现,在windows下正常。


根据错误信息的提示,发现问题出在WriteAccessRecord文件里,分析代码发现,byte数组data的最大长度被定义为112,当被传入的参数userName达到一定长度时就会抛错。    
跟踪代码WritableWorkbookImpl发现,userName实际就是WorkbookSettings类中的writeAccess字段,亦即生成Excel是的用户信息。可能在linux环境UTF8下每个汉字的字节数为3位(Windows中是2位)的缘故,出现了上诉的奇异现象。  

只要在代码中强制设置变量WorkbookSettings.writeAccess的值即可,例如:  

WorkbookSettings settings = new WorkbookSettings ();
settings.setWriteAccess(null);
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/test.xls"), wb, settings);


你可能感兴趣的:(java,linux,Excel,JXL)