程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c# 通過流導出數據庫數據,寫進excel中

c# 通過流導出數據庫數據,寫進excel中

編輯:C#入門知識

從網絡上看到excel一條一條的插入數據很慢,有人通過流寫入excel,速度很快,具體原理如下:

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;
方法如下:
 private void button1_Click(object sender, EventArgs e)
        {           
            saveFileDialog1.Title = "保存的excel文件";
            saveFileDialog1.InitialDirectory = "c:\\";
            saveFileDialog1.Filter = "Excel97-2003 (*.xls)|*.xls|Excel07-2010(*.xlsx)|*.xlsx;
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName == "" || saveFileDialog1.FileName == null)
            {
                MessageBox.Show("文件名不能為空!");
                return;
            }
            string path = saveFileDialog1.FileName;
            string constr = "Data Source=.;Initial Catalog=Exhibition;User ID=sa;Password=";
            string sql = GetStrSql();
            DataSet ds=new DataSet();
            using (SqlConnection con = new SqlConnection(constr))
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(ds);
            }
            if (ds == null)
            {
                MessageBox.Show("數據獲取有誤!");
                return;
            }
            WriteExcel(ds, path);     
        }
 
 public void WriteExcel(DataSet ds, string path)
        {
            try
            {
                long totalCount = ds.Tables[0].Rows.Count;
                lblTip.Text = "共有" + totalCount + "條數據。";
                Thread.Sleep(1000);
                long rowRead = 0;
                float percent = 0;
 
                StreamWriter sw = new StreamWriter(path, false,Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();
                for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                {
                    sb.Append(ds.Tables[0].Columns[k].ColumnName.ToString() + "\t");
                }
                sb.Append(Environment.NewLine);
 
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    rowRead++;
                    percent = ((float)(100 * rowRead)) / totalCount;
                    Pbar.Maximum = (int)totalCount;
                    Pbar.Value = (int)rowRead;
                    lblTip.Text = "正在寫入[" + percent.ToString("0.00") + "%]...的數據";
                    System.Windows.Forms.Application.DoEvents();
 
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        sb.Append(ds.Tables[0].Rows[i][j].ToString() + "\t");
                    }
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("已經生成指定Excel文件!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
        }
 
public string GetStrSql()
        {
            string strSql = "select a from b";
            return strSql;
        }

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved