用JXL.jar来读取excel文件中的文件,当条件超过441条时,或者内存不足时,我碰到的会报两种错误,一个是内存溢出,一个是数组越界。


第一种异常错误信息:数组越界

java.lang.ArrayIndexOutOfBoundsException

    at java.lang.System.arraycopy(Native Method)


    at jxl.read.biff.CompoundFile.getBigBlockStream(CompoundFile.java:482)


    at jxl.read.biff.CompoundFile.getStream(CompoundFile.java:324)


    at jxl.read.biff.File.<init>(File.java:130)


    at jxl.Workbook.getWorkbook(Workbook.java:268)


    at jxl.Workbook.getWorkbook(Workbook.java:253)


  第二种异常信息:内存溢出

[5/21/12 4:20:08:418 CDT] 00000069JobRunShell   E org.quartz.core.JobRunShell run Job DEFAULT.jobDetailthrew an unhandled Exception:

                                org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocationof method 'updateDB' on target class [class com.alu.alsr.service.impl.ImportDataServiceImpl]failed; nested exception is java.lang.OutOfMemoryError: Java heap space

 at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:269)

 at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)

 at org.quartz.core.JobRunShell.run(JobRunShell.java:202)

 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

Caused by: java.lang.OutOfMemoryError: Java heap space

推荐解决方案是:把服务器上的,内存设置为最大。

如果本机的内存已经设置为最大,还是出现上述情况,就可以尝试用下面这个方法:

将jxl.jar包中的jxl/biff/FormattingRecords       private static final int maxFormatRecordsIndex = 441修改成private static final int maxFormatRecordsIndex = 7200;或者更大。


修改参考:

第一步:第官网下载jxl的源码 地址:http://www.andykhan.com/jexcelapi/download.html

第二步:在eclipse里创建个web项目,如图,然后再把源码中的代码,copy到项目中

jxl读取excel,Java heap space,ArrayIndexOutOfBoundsException,java.lang.System.arraycopy(Native Method),_jar

第三步:修改jxl.biff包下面的类FormattingRecords,修改约84行:  private static final int maxFormatRecordsIndex = 0x1c20; 

   注意:0x是十六进制的意思,1c20是7200的意思,如果你想要指定的数:可以去win下,run运行,calc ,运用科学计算器来转换。

            :要留心下,这个类,引用的其它的类,路径要和你替换的那个类,一样,(可以用反编译器,来查看你之前那个类)

第四步:确认过,导出jar,没有异常。可以去jar包出,取出class,FormattingRecords替换下,你目前程序正在使用的jar里面的FormattingRecords,class

注意,最好是想办法比较下,你修改的FormattingRecords类,只是private static final int maxFormatRecordsIndex = 0x1c20; 这个地方修改了,其它 的地方没有变化,这样才能保证,引用这个jar不会有问题。

jxl读取excel,Java heap space,ArrayIndexOutOfBoundsException,java.lang.System.arraycopy(Native Method),_jar_02

修改好以后,重新发布下,应该就可以了。



附件:

我修改好的class文件,最大数是7200,如果你不想修改,可以查看下载下来,替换下你的文件。

去资源里下载,我会把链接放到,下面回复栏中的。


希望对你能有帮助。