c# 大批量数据导出到excel

我的方法不是使用微软的excel操作类,因为那种实在爆慢,一个个单元格循环写,实在太慢了。【不信朋友们可以试试】
而使用io写的话 半分钟都不用
我数据库数据量为:120000行 29列

技术要点:1.使用的是StreamWriter sw = new StreamWriter(path, false,Encoding.GetEncoding("gb2312"));最终生成文件
2.使用 StringBuilder sb = new StringBuilder();类把查询出来的数据组合为一句超长字符串一次性插入到excel中,
sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString() + "\t");
注意,可不能漏了"\t" 这个是非常重要的! 因为c# "\t"就等于 键盘上的Tab [朋友们可以试试:打开新的txt然后输入1按Tab,输入2按Tab,输入3按Tab保存,然后打开excel文件 把刚刚保存的txt文件拉进去打开你就发现原来。这样写的话1 2 3 会分别在每个单元格上的了。所以上面才使用 "\t"连起来数据库出来的那堆数据,这样一次性导进去,他们就会按照每个单元格来填充!]

需要引用:
using System.Threading;
using System.IO;
方法如下:

    1.  
    2. private void button1_Click(object sender, EventArgs e)
    3. {
    4. saveFileDialog1.Title = "保存的excel文件";
    5. saveFileDialog1.InitialDirectory = "c:\\";
    6. saveFileDialog1.Filter = "Excel97-2003 (*.xls)|*.xls|All Files (*.*)|*.*";
    7. saveFileDialog1.ShowDialog();
    8. if (saveFileDialog1.FileName == "" || saveFileDialog1.FileName == null)
    9. {
    10. MessageBox.Show("文件名不能为空!");
    11. return;
    12. }
    13. string path = saveFileDialog1.FileName;
    14. string constr = "Data Source=.;Initial Catalog=Exhibition;User ID=sa;Password=";
    15. string sql = GetStrSql();
    16. DataSet ds=new DataSet();
    17. using (SqlConnection con = new SqlConnection(constr))
    18. {
    19. SqlDataAdapter da = new SqlDataAdapter(sql, con);
    20. da.Fill(ds);
    21. }
    22. if (ds == null)
    23. {
    24. MessageBox.Show("数据获取有误!");
    25. return;
    26. }
    27. WriteExcel(ds, path);
    28. }
    29.  
    30. public void WriteExcel(DataSet ds, string path)
    31. {
    32. try
    33. {
    34. long totalCount = ds.Tables[0].Rows.Count;
    35. lblTip.Text = "共有" + totalCount + "条数据。";
    36. Thread.Sleep(1000);
    37. long rowRead = 0;
    38. float percent = 0;
    39.  
    40. StreamWriter sw = new StreamWriter(path, false,Encoding.GetEncoding("gb2312"));
    41. StringBuilder sb = new StringBuilder();
    42. for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
    43. {
    44. sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString() + "\t");
    45. }
    46. sb.Append(Environment.NewLine);
    47.  
    48. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    49. {
    50. rowRead++;
    51. percent = ((float)(100 * rowRead)) / totalCount;
    52. Pbar.Maximum = (int)totalCount;
    53. Pbar.Value = (int)rowRead;
    54. lblTip.Text = "正在写入[" + percent.ToString("0.00") + "%]...的数据";
    55. System.Windows.Forms.Application.DoEvents();
    56.  
    57. for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
    58. {
    59. sb.Append(ds.Tables[0].Rows[i][j].ToString() + "\t");
    60. }
    61. sb.Append(Environment.NewLine);
    62. }
    63. sw.Write(sb.ToString());
    64. sw.Flush();
    65. sw.Close();
    66. MessageBox.Show("已经生成指定Excel文件!");
    67. }
    68. catch (Exception ex)
    69. {
    70. MessageBox.Show(ex.Message);
    71. }
    72. }
    73.  
    74. public string GetStrSql()
    75. {
    76. string strSql = "select d.* from ( select cBarcode ,max(case halldoorid When '36' then '1' else '0' end ) [1.1号馆],max(case halldoorid When '37' then '1' else '0' end ) [2.1号馆],max(case halldoorid When '38' then '1' else '0' end ) [3.1号馆],max(case halldoorid When '39' then '1' else '0' end ) [4.1号馆],max(case halldoorid When '40' then '1' else '0' end ) [5.1号馆],max(case halldoorid When '41' then '1' else '0' end ) [6.1号馆],max(case halldoorid When '42' then '1' else '0' end ) [8.1号馆],max(case halldoorid When '43' then '1' else '0' end ) [1.2号馆],max(case halldoorid When '44' then '1' else '0' end ) [2.2号馆],max(case halldoorid When '45' then '1' else '0' end ) [3.2号馆],max(case halldoorid When '46' then '1' else '0' end ) [4.2号馆],max(case halldoorid When '47' then '1' else '0' end ) [5.2号馆],max(case halldoorid When '48' then '1' else '0' end ) [9.1号馆],max(case halldoorid When '49' then '1' else '0' end ) [10.1号馆],max(case halldoorid When '50' then '1' else '0' end ) [11.1号馆],max(case halldoorid When '51' then '1' else '0' end ) [12.1号馆],max(case halldoorid When '52' then '1' else '0' end ) [13.1号馆],max(case halldoorid When '53' then '1' else '0' end ) [9.2号馆],max(case halldoorid When '54' then '1' else '0' end ) [10.2号馆],max(case halldoorid When '55' then '1' else '0' end ) [11.2号馆],max(case halldoorid When '56' then '1' else '0' end ) [12.2号馆],max(case halldoorid When '57' then '1' else '0' end ) [13.2号馆],max(case halldoorid When '58' then '1' else '0' end ) [9.3号馆],max(case halldoorid When '59' then '1' else '0' end ) [10.3号馆],max(case halldoorid When '60' then '1' else '0' end ) [11.3号馆],max(case halldoorid When '61' then '1' else '0' end ) [VIP3.2号馆],max(case halldoorid When '62' then '1' else '0' end ) [VIP11.3号馆],max(case halldoorid When '63' then '1' else '0' end ) [VIP14.3号馆]from tblReadCard group by cBarcode ) d left join ( select cBarcode 观众条码 from (select distinct cBarcode,cReadDate from tblReadCard where cReadDate between '2011-07-08' and '2011-07-11') a group by cBarcode ) c on d.cBarcode=c.观众条码 group by cBarcode,[1.1号馆],[2.1号馆],[3.1号馆],[4.1号馆],[5.1号馆],[6.1号馆],[8.1号馆],[1.2号馆],[2.2号馆],[3.2号馆],[4.2号馆],[5.2号馆],[9.1号馆],[10.1号馆],[11.1号馆],[12.1号馆],[13.1号馆],[9.2号馆],[10.2号馆],[11.2号馆],[12.2号馆],[13.2号馆],[9.3号馆],[10.3号馆],[11.3号馆],[VIP3.2号馆],[VIP11.3号馆],[VIP14.3号馆]";
    77. return strSql;
    78. }

posted on 2021-07-05 17:17  funiyi816  阅读(143)  评论(0编辑  收藏  举报

导航