excel download性能问题解决

性能问题:
QAC系统的excel下载时速度非常非常的慢,今天User需要下载445条记录,系统就没反应了,等30分钟都无法正常下载;不过下载200条以下的记录时是ok的。说明是内存或者什么性能上的问题了。

问题解决:
Debug跟了一下程序,发现QAC里导出excel是用最原始的方式,就是用循环把所有信息拼成一个html长字符串,然后就直接output出来了。问题就出现在这循环里了,假如下载500条记录,每条记录50个columns,那么就是循环500*50次,而这2.5万次循环里,之前的程序是用String的“+”操作直接拼字符串的,因为String是final类,每一次“+”操作实际上都是重新new一个String对象,把引用指向新的对象,所以速度非常非常的慢!

于是我把String的“+”操作换成了StringBuffer的append操作,效率提高了好几个等级!!之前30分钟都download不下来的excel文件,现在不到30秒就能下载下来了!

StringBuffer的效率真赞啊~

哈哈,解决了一个长期以来遗留下来的性能问题,好有成就感,哈哈:)

附:
1. 之前的程序(String的“+”操作):
public static void exportHtmlFileToXLS(CachedRowSet crs, String strFilePath, String strEncode) throws SQLException, IOException
{
ResultSetMetaData rsmd = crs.getMetaData();
int numColumn = rsmd.getColumnCount();
String temp = "";
// Build Head of Field Name
temp += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
temp += "<table border='1'>";
temp += "<tr>";
for (int i = 1; i <= numColumn; i++) {
temp += "<td align='center'>";
temp += rsmd.getColumnName(i);
temp += "</td>";
} // End for
temp += "</tr>";
temp += "<tr>";
if (crs.size() > 0) {
while (crs.next()) {
System.out.println(System.currentTimeMillis());
long time1=System.currentTimeMillis();
for (int i = 1; i <= numColumn; i++) {
temp += "<td align='center'>";
temp += Utility.notNull(crs.getString(rsmd.getColumnName(i))).replaceAll(",", "");
temp += "</td>";
// if(debug) System.out.println("[" + rsmd.getColumnName(i) + "][" + rsmd.getColumnTypeName(i) + "]");
} // End for
temp += "</tr>";
System.out.println(System.currentTimeMillis());
long time2=System.currentTimeMillis();
long time=time2-time1;
System.out.println(time);
} // End while(crs.next())
} // End if(crs.size()>0)
temp += "</table>";
File csvFile = new File(strFilePath);

FileOutputStream fos = new FileOutputStream(csvFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, strEncode);
//System.out.println("encode="+osw.getEncoding());

osw.write(temp);
osw.close();
}
2. 改进后的程序(StringBuffer的append操作):
public static void exportHtmlFileToXLS(CachedRowSet crs, String strFilePath, String strEncode) throws SQLException, IOException
{
ResultSetMetaData rsmd = crs.getMetaData();
int numColumn = rsmd.getColumnCount();
String temp = "";
//modified by linna at 20090622;start;
StringBuffer tempBuf=new StringBuffer();
tempBuf.append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
tempBuf.append("<table border='1'>");
tempBuf.append("<tr>");
for (int i = 1; i <= numColumn; i++) {
tempBuf.append("<td align='center'>");
tempBuf.append(rsmd.getColumnName(i));
tempBuf.append("</td>");
} // End for
tempBuf.append("</tr>");
tempBuf.append("<tr>");
if (crs.size() > 0) {
while (crs.next()) {
System.out.println(System.currentTimeMillis());
long time1=System.currentTimeMillis();
for (int i = 1; i <= numColumn; i++) {
tempBuf.append("<td align='center'>");
tempBuf.append(Utility.notNull(crs.getString(rsmd.getColumnName(i))).replaceAll(",", ""));
tempBuf.append("</td>");
// if(debug) System.out.println("[" + rsmd.getColumnName(i) + "][" + rsmd.getColumnTypeName(i) + "]");
} // End for
tempBuf.append("</tr>");
System.out.println(System.currentTimeMillis());
long time2=System.currentTimeMillis();
long time=time2-time1;
System.out.println(time);
} // End while(crs.next())
} // End if(crs.size()>0)
tempBuf.append("</table>");
temp=tempBuf.toString();
//modified by linna at 20090622;end;
File csvFile = new File(strFilePath);

FileOutputStream fos = new FileOutputStream(csvFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, strEncode);
//System.out.println("encode="+osw.getEncoding());

osw.write(temp);
osw.close();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值