1. 程式人生 > 其它 >c# 大批量資料匯出到excel

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. private void button1_Click(object sender, EventArgs e)
    2. {
    3. saveFileDialog1.Title = "儲存的excel檔案";
    4. saveFileDialog1.InitialDirectory = "c:\\";
    5. saveFileDialog1.Filter = "Excel97-2003 (*.xls)|*.xls|All Files (*.*)|*.*";
    6. saveFileDialog1.ShowDialog();
    7. if (saveFileDialog1.FileName == "" || saveFileDialog1.FileName == null)
    8. {
    9. MessageBox.Show("檔名不能為空!");
    10. return;
    11. }
    12. string path = saveFileDialog1.FileName;
    13. string constr = "Data Source=.;Initial Catalog=Exhibition;User ID=sa;Password=";
    14. string sql = GetStrSql();
    15. DataSet ds=new DataSet();
    16. using (SqlConnection con = new SqlConnection(constr))
    17. {
    18. SqlDataAdapter da = new SqlDataAdapter(sql, con);
    19. da.Fill(ds);
    20. }
    21. if (ds == null)
    22. {
    23. MessageBox.Show("資料獲取有誤!");
    24. return;
    25. }
    26. WriteExcel(ds, path);
    27. }
    28. public void WriteExcel(DataSet ds, string path)
    29. {
    30. try
    31. {
    32. long totalCount = ds.Tables[0].Rows.Count;
    33. lblTip.Text = "共有" + totalCount + "條資料。";
    34. Thread.Sleep(1000);
    35. long rowRead = 0;
    36. float percent = 0;
    37. StreamWriter sw = new StreamWriter(path, false,Encoding.GetEncoding("gb2312"));
    38. StringBuilder sb = new StringBuilder();
    39. for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
    40. {
    41. sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString() + "\t");
    42. }
    43. sb.Append(Environment.NewLine);
    44. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    45. {
    46. rowRead++;
    47. percent = ((float)(100 * rowRead)) / totalCount;
    48. Pbar.Maximum = (int)totalCount;
    49. Pbar.Value = (int)rowRead;
    50. lblTip.Text = "正在寫入[" + percent.ToString("0.00") + "%]...的資料";
    51. System.Windows.Forms.Application.DoEvents();
    52. for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
    53. {
    54. sb.Append(ds.Tables[0].Rows[i][j].ToString() + "\t");
    55. }
    56. sb.Append(Environment.NewLine);
    57. }
    58. sw.Write(sb.ToString());
    59. sw.Flush();
    60. sw.Close();
    61. MessageBox.Show("已經生成指定Excel檔案!");
    62. }
    63. catch (Exception ex)
    64. {
    65. MessageBox.Show(ex.Message);
    66. }
    67. }
    68. public string GetStrSql()
    69. {
    70. 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號館]";
    71. return strSql;
    72. }