我的代码如下:
导出5万条12钟左右,太长了。
代码如下,请教有没有好的方法:
 public static bool ExportForListView(ListView listView, string fileName, bool isShowExcle)
        {
            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
            try
            {
                string excel = "";      //用于存放要写入的一行文本。
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    excel = excel + listView.Columns[i].Text.ToString().Trim() + Convert.ToChar(9);
                }
                sw.WriteLine(excel);    //写入DataGridView的标题行。
                excel = "";
                for (int i = 0; i < listView.Items.Count; i++)
                {
                    for (int j = 0; j < listView.Columns.Count; j++)
                    {
                        if (listView.Items[i].SubItems[j].Text.ToString() == null)
                            excel = excel + "" + Convert.ToChar(9);    //循环写入每一行
                        else
                            excel = excel + listView.Items[i].SubItems[j].Text.ToString() + Convert.ToChar(9);
                    }
                    sw.WriteLine(excel);
                    excel = "";
                }           }
            catch
            {
                return false;
            }
            finally
            {
                sw.Close();
                fs.Close();
                if (isShowExcle)
                {
                    System.Diagnostics.Process.Start(fileName);
                }
            }
            return true;
        }

解决方案 »

  1.   

    在内存中写出的东西,这个应该比较快了。
    快一点,可以把你LISTVIEW中的数据传给datagrid.
    建立一个datagrid.
    用下面的方法导出就很快了。
    Response.Clear(); 
    Response.Buffer= true; 
    Response.Charset="utf-8";    
    Response.AppendHeader("Content-Disposition","attachment;filename=新建 Microsoft Excel 工作表.xls"); 
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    this.EnableViewState = false;    
    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    this.DG.RenderControl(oHtmlTextWriter);  //DG:Gridname
    Response.Write(oStringWriter.ToString());
    Response.End();不知道行得通不!顶!