日期:2014-05-20  浏览次数:20773 次

关于用jxl来操作excel文档进行数据插入内存溢出问题?希望有经验的帮忙下,谢谢!
Java code
import jxl.*;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.*;

public class UpdateExcel {
    public static void main(String[] args) {
        try {
            Workbook wb1 = Workbook.getWorkbook(new File("F:\\a.xls"));
            WritableWorkbook wwb1 = Workbook.createWorkbook(new File("F:\\a.xls"), wb1);
            Workbook wb2 = Workbook.getWorkbook(new File("F:\\b.xls"));
            WritableSheet sheet1 = wwb1.getSheet(0);
            Sheet sheet2 = wb2.getSheet(0);
            String id2 = "123";
            String id1 = "456";
            int count = 0;
            
            double number = 0;
            String song = "s";
            String il = "l";
            
            for(int i=1;i<sheet1.getRows();i++){
                id1 = sheet1.getCell(0, i).getContents().trim();
                System.out.println("i原始::"+i);
                for(int j=1;j<sheet2.getRows();j++){
                    id2 = sheet2.getCell(0,j).getContents().trim();
                    if(id1.equals(id2)){
                        System.out.println("j::"+j);
                        count = Integer.parseInt(sheet2.getCell(3, j).getContents());
                        System.out.println("count::"+count);
                        for(int m=0;m<count;m++){
                            sheet1.insertRow(++i);
                        }
                        for(int n=0;n<count;n++){
                            number = Double.parseDouble(sheet2.getCell(4, j).getContents().trim());
                            song = sheet2.getCell(5, j).getContents().trim();
                            il = sheet2.getCell(6, j).getContents().trim();    
                            Number ln = new jxl.write.Number(6, i, number);
                            Label ls = new Label(7, i, song);
                            Label li = new Label(8, i, il);
                            sheet1.addCell(ln);
                            sheet1.addCell(ls);
                            sheet1.addCell(li);
                            wwb1.write();
                        }
                        j+=count;
                    }
                }
            }
            wwb1.close();
            wb1.close();
            wb2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


这个就是将2个excel文档中的数据进行比对,如果匹配上的话就将需要更新的excel文档中插入几行空行,然后把数据插入到空行的对应列去,如此循环,可是当插入第一个匹配项目时候就抛内存溢出了,这是怎么回事啊,我做测试时候写两个简单的excel进行一条数据的插入时候没有问题啊,忘高手能帮忙解决下,不胜感激!抛的异常如下:
Java code
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at jxl.write.biff.MemoryDataOutput.write(MemoryDataOutput.java:72)
    at jxl.write.biff.File.write(File.java:149)
    at jxl.write.biff.RowRecord.writeIntegerValues(RowRecord.java:393)
    at jxl.write.biff.RowRecord.writeCells(RowRecord.java:341)
    at jxl.write.biff.SheetWriter.write(SheetWriter.java:480)
    at jxl.write.biff.WritableSheetImpl.write(WritableSheetImpl.java:1558)
    at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:950)
    at test.UpdateExcel.main(UpdateExcel.java:52)


------解决方案--------------------
我帮你顶上去吧,,,这个没用过。。。