日期:2014-05-20 浏览次数:20799 次
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();
}
}
}
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)