在创建excel,部分代码如下:
public static Workbook createWorkbook(String source){
try {
File sourcefile = new File(source);
System.out.println(sourcefile.getName());
if(!sourcefile.exists()){
System.out.println("Can not find the source file!");
return null;
}
Workbook book = Workbook.getWorkbook(sourcefile);
return book;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
执行时,会有异常信息:
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at jxl.biff.StringHelper.getUnicodeString(StringHelper.java:189)
at jxl.read.biff.WriteAccessRecord.<init>(WriteAccessRecord.java:50)
at jxl.read.biff.WorkbookParser.parse(WorkbookParser.java:820)
at jxl.Workbook.getWorkbook(Workbook.java:237)
at jxl.Workbook.getWorkbook(Workbook.java:198)
后来查下原因,才发现我的excel文件大于6M时,才会报以上错误。解决办法就是:增加java虚拟机的内存空间。
修改方法参考:
方法1.单独修改class的VM大小,在eclipse中,右键Run Configurations ->在“VM arguments”中设置参数-Xms256m -Xmx1024m ,就可以将参数设置为1G。
方法2.在eclipse安装目录下,修改eclipse.ini文件,修改-Xmx相应的值。
方法3.选中eclipse的快捷图标,右键点 属性,然后设置
“d:\eclipse\eclipse.exe -vmargs -Xms128m -Xmx256m”。