我的代码
rs = dbutil.getItemForRowSet(JsonFactory.getRowCountSql(sql));
if (rs.next()) {
RowCount = rs.getInt(1);
}
System.out.println(RowCount);
rs = dbutil.getItemForRowSet(sql);
if (rs != null) {
if (displayType.equals("excel")) {
String[] heads = { "人员编号", "姓名", "卡序号", "使用次数", "发生金额", "卡余额", "发生时间", "采集时间", "设备号", "商户" };
String sq="";
for(int i=0;i<RowCount/5000;i++){
sq="select t.* from (select rownum rm,m.* from ("+sql+") m) t where t.rm <="+(i+1)*5000+" and t.rm >="+i*5000;
rs = dbutil.getItemForRowSet(sq);
String fileName = "消费报表"+i;
// ExcelFactory.excelByRowSetOrResultSet(response, fileName, heads, rs);
File file=new File("E:\\"+fileName+i+".txt");
FileOutputStream os=new FileOutputStream (file);
PrintWriter writefile = new PrintWriter(os);  writefile.write("<?xml version='1.0' encoding='GBK' ?>\r\n");
writefile.write("<?mso-application progid='Excel.Sheet'?>\r\n ");
writefile.write("<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'\r\n");
writefile.write("xmlns:o='urn:schemas-microsoft-com:office:office'\r\n");
writefile.write("xmlns:x='urn:schemas-microsoft-com:office:excel'\r\n");
writefile.write(" xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'\r\n");
writefile.write(" xmlns:html='http://www.w3.org/TR/REC-html40'>\r\n");
writefile.write(" <DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>\r\n");
writefile.write("<Version>11.9999</Version>\r\n");
writefile.write(" </DocumentProperties>\r\n");
writefile.write(" <ExcelWorkbook xmlns='urn:schemas-microsoft-com:office:excel'>\r\n");
writefile.write("<WindowHeight>10005</WindowHeight>\r\n");
writefile.write("<WindowWidth>10005</WindowWidth>\r\n");
writefile.write("<WindowTopX>120</WindowTopX>\r\n");
writefile.write("<WindowTopY>135</WindowTopY>\r\n");
writefile.write("<ActiveSheet>1</ActiveSheet>\r\n");
writefile.write("<ProtectStructure>False</ProtectStructure>\r\n");
writefile.write("<ProtectWindows>False</ProtectWindows>\r\n");
writefile.write("</ExcelWorkbook>\r\n");
writefile.write("<Styles>\r\n" +
"<Style ss:ID='Default' ss:Name='Normal'>\r\n" +
"<Alignment ss:Vertical='Center'/>\r\n" +
"<Borders/>\r\n" +
"<Font ss:FontName='??ì?' x:CharSet='134' ss:Size='12'/>\r\n" +
"<Interior/>\r\n" +
" <NumberFormat/>\r\n" +
"<Protection/>\r\n" +
"</Style>\r\n" +
"<Style ss:ID='s23'>\r\n" +
"<Font ss:FontName='??ì?' x:CharSet='134' ss:Size='12' ss:Color='#0000FF'\r\n" +
" ss:Bold='1'/>\r\n" +
"<Interior ss:Color='#C0C0C0' ss:Pattern='Solid'/>\r\n" +
"</Style>\r\n" +
"</Styles>\r\n");
writefile.write(" <Worksheet ss:Name='"+fileName+"'>\r\n");//sheet的名称
writefile.write(" <Table ss:ExpandedColumnCount='"+heads.length+"' ss:ExpandedRowCount='"+RowCount+"' x:FullColumns='1' x:FullRows='1' ss:DefaultColumnWidth='54' ss:DefaultRowHeight='14.25'>\r\n");
for(int j = 0; j < heads.length; j++){//设置宽度
writefile.write("<Column ss:AutoFitWidth='0' ss:Width='73.5'/>\r\n" );
}
writefile.write("<Row ss:AutoFitHeight='0'>\r\n");
//表头
for (int j = 0; j < heads.length; j++) {
writefile.write("<Cell ss:StyleID='s23'><Data ss:Type='String'>"+heads[j]+"</Data></Cell>\r\n");
}
writefile.write("</Row>\r\n");
while(rs.next()){
String i1=rs.getString(1);
String i2=rs.getString(2);
String i3=rs.getString(3);
String i4=rs.getString(4);
String i5=rs.getString(5);
String i6=rs.getString(6);
String i7=rs.getString(7);
String i8=rs.getString(8);
String i9=rs.getString(9);
String i10=rs.getString(10);
writefile.write("<Row ss:AutoFitHeight='0'>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i1+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i2+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i3+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i4+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i5+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i6+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i7+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i8+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i9+"</Data></Cell>\r\n");
writefile.write("<Cell><Data ss:Type='String'>"+i10+"</Data></Cell>\r\n");
writefile.write("</Row>\r\n");
writefile.flush();
 }
writefile.write(" </Table>\r\n" +
"<WorksheetOptions xmlns='urn:schemas-microsoft-com:office:excel'>\r\n" +
"<Print>\r\n" +
"<ValidPrinterInfo/>\r\n" +
"<PaperSizeIndex>9</PaperSizeIndex>\r\n" +
"<HorizontalResolution>600</HorizontalResolution>\r\n" +
"<VerticalResolution>600</VerticalResolution>\r\n" +
"</Print>\r\n" +
"<Selected/>\r\n" +
"<ProtectObjects>False</ProtectObjects>\r\n" +
"<ProtectScenarios>False</ProtectScenarios>\r\n" +
"</WorksheetOptions>\r\n" +
"</Worksheet>\r\n" +
"</Workbook>\r\n");
writefile.flush();
writefile.close();
os.flush();
os.close();
writefile=null;
os=null;
System.gc();当查询的总数据量是4W多条的时候就报内存溢出,谁帮我看下怎么改进,我用的是分页查找的方式,循环向txt写入内容