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

52 篇文章 1 订阅

  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.         }  
  32.         sw.Write(sb.ToString());  
  33.         sw.Flush();  
  34.         sw.Close();  
  35.         sw.Dispose();  
  36.   
  37.         // 导出成功后打开  
  38.         //System.Diagnostics.Process.Start(path);  
  39.     }  
  40.     catch (Exception)  
  41.     {  
  42.         result = "请保存或关闭可能已打开的Excel文件";  
  43.     }  
  44.     finally  
  45.     {  
  46.         dt.Dispose();  
  47.     }  
  48.     return result;  
  49. }  
  50. /// <summary>  
  51. /// 结束进程  
  52. /// </summary>  
  53. private static void KillSpecialExcel()  
  54. {  
  55.     foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))  
  56.     {  
  57.         if (!theProc.HasExited)  
  58.         {  
  59.             bool b = theProc.CloseMainWindow();  
  60.             if (b == false)  
  61.             {  
  62.                 theProc.Kill();  
  63.             }  
  64.             theProc.Close();  
  65.         }  
  66.     }  
  67. }  

B/S 导出:

[csharp]  view plain  copy
 print ?
  1. // 保存错误信息  
  2.                         GridView gv = new GridView();  
  3.                         gv.DataSource = dtError;  
  4.                         gv.DataBind();  
  5.                         gv.Attributes.Add("style""vnd.ms-excel.numberformat:@");  
  6.   
  7.                         HttpResponse hResponse = this.Response;  
  8.                         string fileName1 = "新员工格式验证错误统计" + DateTime.Now.ToString("yyyyMMdd");  
  9.   
  10.                         hResponse.AddHeader("Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8) + ".xls");  
  11.                         hResponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
  12.                         hResponse.ContentType = "application/ms-excel";  
  13.                         this.EnableViewState = false;  
  14.   
  15.                         StringWriter tw = new StringWriter();  
  16.                         System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);  
  17.                         gv.RenderControl(hw);  
  18.   
  19.                         hResponse.Write(tw);  
  20.                         hResponse.End();  
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值