使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
来源:互联网 发布:sqlserver 2012 下载 编辑:程序博客网 时间:2024/05/14 12:40
错误信息如下:......java.lang.ArrayIndexOutOfBoundsException 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) ...... 神奇的是,在项目中发现该现象只有在Linux环境下才发生而Window下则正常运行。根据错误信息的提示,发现问题出在WriteAccessRecord文件里,其源代码如下所示:view plain1.package jxl.write.biff; 2. 3.import jxl.Workbook; 4.import jxl.biff.StringHelper; 5.import jxl.biff.Type; 6.import jxl.biff.WritableRecordData; 7. 8./** 9. * The name used when Excel was installed. 10. * When writing worksheets, it uses the value from the WorkbookSettings object, 11. * if this is not set (null) this is hard coded as 12. * Java Excel API + Version number 13. */ 14.class WriteAccessRecord extends WritableRecordData 15.{ 16. /** 17. * The data to output to file 18. */ 19. private byte[] data; 20. 21. // String of length 112 characters 22. /** 23. * The author of this workbook (ie. the Java Excel API) 24. */ 25. private final static String authorString = "Java Excel API"; 26. private String userName; 27. 28. /** 29. * Constructor 30. */ 31. public WriteAccessRecord(String userName) 32. { 33. super(Type.WRITEACCESS); 34. 35. data = new byte[112]; 36. String astring = userName != null ? 37. userName : 38. authorString + " v" + Workbook.getVersion(); 39. 40. StringHelper.getBytes(astring, data, 0); 41. 42. // Pad out the record with space characters 43. for (int i = astring.length() ; i < data.length ;i++) 44. { 45. data[i] = 0x20; 46. } 47. } 48. 49. /** 50. * Gets the data for output to file 51. * 52. * @return the binary data 53. */ 54. public byte[] getData() 55. { 56. return data; 57. } 58.} 分析上诉代码发现,byte数组data的最大长度被定义为112,当被传入的参数userName达到一定长度时就会抛错。跟踪代码WritableWorkbookImpl发现,userName实际就是WorkbookSettings类中的writeAccess字段,亦即生成Excel是的用户信息。可能在linux环境UTF8下每个汉字的字节数为3位(Windows中是2位)的缘故,出现了上诉的奇异现象。 解决的办法如下:1.修改JXL源代码中WriteAccessRecord文件代码,重新设置变量data的长度,例如:data = new byte[astring.getBytes().length];2.一般我们在读取模板文件生成新的Excel时往往使用如下代码:view plain1.import java.io.File; 2. 3.import jxl.Workbook; 4.import jxl.write.WritableSheet; 5.import jxl.write.WritableWorkbook; 6. 7.public class Test { 8. 9. public static void main(String[] args) throws Exception { 10. 11. Workbook wb = Workbook.getWorkbook(new File("C:/data_template.xls")); 12. WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/data_output.xls"), wb); 13. 14. WritableSheet sheet = workbook.getSheet(3); 15. sheet.getSettings().setSelected(true); 16. 17. workbook.write(); 18. workbook.close(); 19. } 20.} 只要在代码中强制设置变量WorkbookSettings.writeAccess的值即可,例如:Workbook wb = Workbook.getWorkbook(new File("C:/data_template.xls"));WorkbookSettings settings = new WorkbookSettings ();settings.setWriteAccess(null);WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/data_output.xls"), wb, settings);WritableSheet sheet = workbook.getSheet(3);sheet.getSettings().setSelected(true);workbook.write();workbook.close();
转载 http://lzqdll.blog.163.com/blog/static/200607183201111635447767/
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
- Linux环境下 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException
- 在Linux环境下使用JXL生成Excel时发生ArrayIndexOutOfBoundsException错误
- jxl 错误 java.lang.ArrayIndexOutOfBoundsException: 441
- jxl操作java.lang.ArrayIndexOutOfBoundsException: 441 错误
- jxl导出excel异常:java.lang.ArrayIndexOutOfBoundsException: 31
- JXL导出Excel抛java.lang.ArrayIndexOutOfBoundsException: 5
- 解决jxl导出excel异常:java.lang.ArrayIndexOutOfBoundsException
- [经验小结]jxl写excel时产生“java.lang.ArrayIndexOutOfBoundsException: 5”问题的解决办法
- [java.lang.arrayindexoutofboundsexception]错误!
- android使用getItemViewType时出现java.lang.ArrayIndexOutOfBoundsException的错误
- jxl java.lang.ArrayIndexOutOfBoundsException: 164 异常解决方法
- jxl读取excel,Java heap space,ArrayIndexOutOfBoundsException,java.lang.System.arraycopy(Native Method),
- java.lang.ArrayIndexOutOfBoundsException: 0错误
- jxl在linux环境下报java.lang.ArrayIndexOutOfBoundsException
- AXMLPrinter2.jar反编译xml文件时 java.lang.ArrayIndexOutOfBoundsException错误
- 关于visual basic6.0的鼠标滚轮修复
- Oracle Data encrypted
- Linux 固件子系统----如何更新固件
- 协议(Protocol)的用法
- My Review of Hadoop Summit 2011
- 使用JXL生成Excel时发生java.lang.ArrayIndexOutOfBoundsException错误
- 关于self.用法的一些总结
- 黑马程序员-字符串
- 中断处理--上下半部机制
- IOS传值、变量共享方式
- 几个不错的C++/VC++网站(个人收藏额~~)
- MFC学习之路(五)让人菊花一紧的错误
- Spring中创建切面 --- 自动代理
- C#中ref与out的区别