微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#WinForm 使用StreamWriter导出数据成Excel文件


  7433人阅读  评论(1)  收藏  举报

  分类
1:导出数据为Excel文件在开发项目时比较常见的一种需求 。以前对于数据量较小的情况使用 Microsoft.Office.Interop.Excel.Workbooks相关类,编写起来也比较麻烦,对于数据量较大的情况,在此与大家共享使用SteamWriter类输出Excel文件方法。经过具体测试,通过在程序中使用多线程配置该方法,导出300000行+17列的约130M的数据需要31秒左右。
[csharp]  view plain  copy
 print ?

在CODE上查看代码片

派生到我的代码片

  1. /// <summary>  
  2. /// 导出文件,使用文件流。该方法使用的数据源为DataTable,导出的Excel文件没有具体的样式。  
  3. /// </summary>  
  4. /// <param name="dt"></param>  
  5. public static string ExportToExcel(System.Data.DataTable dt, string path)  
  6. {  
  7.     KillSpecialExcel();  
  8.     string result = string.Empty;  
  9.     try  
  10.     {  
  11.         // 实例化流对象,以特定的编码向流中写入字符。  
  12.         StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));  
  13.   
  14.         StringBuilder sb = new StringBuilder();  
  15.         for (int k = 0; k < dt.Columns.Count; k++)  
  16.         {  
  17.             // 添加名称  
  18.             sb.Append(dt.Columns[k].ColumnName.ToString() + "\t");  
  19.         }  
  20.         sb.Append(Environment.NewLine);  
  21. // 添加行数据  
  22.         for (int i = 0; i < dt.Rows.Count; i++)  
  23.         {  
  24.             DaTarow row = dt.Rows[i];  
  25.             for (int j = 0; j < dt.Columns.Count; j++)  
  26.             {  
  27.                 // 根据列数追加行数据  
  28.                 sb.Append(row[j].ToString() + "\t");  
  29.             }  
  30.             sb.Append(Environment.NewLine);  
  31.         sw.Write(sb.ToString());  
  32.         sw.Flush();  
  33.         sw.Close();  
  34.         sw.dispose();  
  35.   
  36. // 导出成功后打开  
  37. //System.Diagnostics.Process.Start(path);  
  38.     }  
  39.     catch (Exception)  
  40.     {  
  41.         result = "请保存或关闭可能已打开的Excel文件";  
  42.     finally  
  43.         dt.dispose();  
  44.     return result;  
  45. }  
  46. /// <summary>  
  47. /// 结束进程  
  48. /// </summary>  
  49. private static void KillSpecialExcel()  
  50.     foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))  
  51.         if (!theProc.HasExited)  
  52.             bool b = theProc.CloseMainWindow();  
  53.             if (b == false)  
  54.             {  
  55.                 theProc.Kill();  
  56.             theProc.Close();  
  57.     }  
  58. }  

B/S 导出:

copy
 ?
    // 保存错误信息  
  1.                         GridView gv = new GridView();  
  2.                         gv.DataSource = dtError;  
  3.                         gv.DataBind();  
  4.                         gv.Attributes.Add("style""vnd.ms-excel.numberformat:@");  
  5.                         HttpResponse hResponse = this.Response;  
  6.                         string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  7.                         hresponse.addheader("Content-disposition""attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
  8.                         hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
  9.                         hResponse.ContentType = "application/ms-excel";  
  10.                         this.EnableViewState = false;  
  11.                         StringWriter tw = new StringWriter();  
  12.                         System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
  13.                         gv.RenderControl(hw);  
  14.                         hResponse.Write(tw);  
  15.                         hResponse.End();  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐