在DataGrid数据导出的时候遇到很奇怪的问题,到数据集中的数据有几百条记录时,数据导出没有任何问题,但到数据集中的数据量高达几千上万条时,数据导出就无法实现,它的表现就是应用程序没有响应,打开进程一看,w3wp.exe这个进程CPU的占用率高达100%,估计出现是IIS已经死了,请问怎么解决这种问题啊,那位兄弟能提出一种好的方案啊?下面是导出数据的函数:
private void DataGridToExcel(HttpResponse response){
response.Clear();
response.AddHeader("content-disposition", "attachment;filename=FileData.xls");
response.Charset = "gb2312";
response.Cache.SetCacheability(HttpCacheability.Server);
response.ContentType = "application/vnd.xls";System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);string sTable = "<table borderColor=\"#000000\" cellSpacing=\"0\" borderColorDark=\"#ffffff\" cellPadding=\"1\" width=\"100%\" align=\"center\" border=\"1\">";
XLSJJ conn = BLL.BLLSeries.GetDataSet(ddlSeries.SelectedItem.Value);
DataSet ds = BLL.BLLSeries.GetDataSet(conn.LJC,conn.SQL);for(int i = 0 ; i < ds.Tables[0].Rows.Count ; i++)
{
sTable += "<tr>";
for(int j = 0 ; j < ds.Tables[0].Columns.Count ; j++)
{
sTable += "<td>";
sTable += ds.Tables[0].Rows[i][j].ToString();
sTable += "</td>";
}
sTable += "</tr>";
}
sTable += "</table>";
stringWrite.Write(sTable); response.Write(stringWrite.ToString());
response.End();
}
}
private void DataGridToExcel(HttpResponse response){
response.Clear();
response.AddHeader("content-disposition", "attachment;filename=FileData.xls");
response.Charset = "gb2312";
response.Cache.SetCacheability(HttpCacheability.Server);
response.ContentType = "application/vnd.xls";System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);string sTable = "<table borderColor=\"#000000\" cellSpacing=\"0\" borderColorDark=\"#ffffff\" cellPadding=\"1\" width=\"100%\" align=\"center\" border=\"1\">";
XLSJJ conn = BLL.BLLSeries.GetDataSet(ddlSeries.SelectedItem.Value);
DataSet ds = BLL.BLLSeries.GetDataSet(conn.LJC,conn.SQL);for(int i = 0 ; i < ds.Tables[0].Rows.Count ; i++)
{
sTable += "<tr>";
for(int j = 0 ; j < ds.Tables[0].Columns.Count ; j++)
{
sTable += "<td>";
sTable += ds.Tables[0].Rows[i][j].ToString();
sTable += "</td>";
}
sTable += "</tr>";
}
sTable += "</table>";
stringWrite.Write(sTable); response.Write(stringWrite.ToString());
response.End();
}
}
解决方案 »
- 如何在HTML代码里 赋值 TEXTBOX的值
- 截取字符串
- 关于.net中后置代码和httpHandler处理类的问题
- 一个CheckBoxList与一个TextBox 请大家帮我这个问题吧 我的分已经发完了以后补发分
- 请问:在C#语言中如何通过事件获得事件源的控件名称,各位大虾看过来啊
- 自定义空间怎样执行一个外部的方法?
- 我有对方提供的公匙如何加密?
- vs2010 beta 网页嵌入winform 控件问题
- 這樣的SQL拼接語句是怎麼回事?
- 送分问题:asp.net和asp有什么不同,有什么区别?
- 我用SQL SERVER2005+VS2005(C#) 做程序,APP.CONFIG配置更改问题:
- 请问将textbox控件属性textmode设为mulitline后,不能进行maxlength属性限制?
哦,好可怕。这么写不慢死才怪,string是不可改变的。string的+=每次都会生成一个更大的数组并且把2个原始数组都复制进去,是个超级浪费时间和空间的事情。解决方法是用StringBuilder,它是数据容器,最好是一开始就分配一个足够大的容量,这样就不会有性能问题了。或者你干脆就直接用Stream.Write(),不用先写到一个字符串里的。Stream本身就有缓存。
在數據量多的情況下,LZ可以換其它的方法導出數據。
建议用System.Text.StringBuilder,然后append.
另外数据量太大也是个问题.