Export large amounts of data to Excel (Microsoft Excel does not use controls) --------------- Transfer CSDN - rocket2010

Technical points: 1 using StreamWriter sw = new StreamWriter (path, false, Encoding.GetEncoding ( "gb2312")); generating a final document
2. StringBuilder sb = new StringBuilder (); check out the class data combination a long string inserted to excel in disposable,
sb.append (ds.Tables [0] .Columns [K] .ColumnName.ToString () + "\ t");
Note that can not be missed "\ t" this is very important! Because c # "\ t" is equal Tab on the keyboard [friends can try: open a new txt and then enter a press Tab, enter 2 press Tab, enter 3 Press Tab to save, and then open the excel file to just saved txt file pull into the open you will find the original. Write 123 words will be in each cell, respectively. So just use the above "\ t" to link the pile of data out of the database, so a one-time lead in, and they will be filled in accordance with each cell! ]

Need to refer to:
the using the System.Threading;
the using the System.IO;
follows:

         private void button1_Click(object sender, EventArgs e)
        {            
            saveFileDialog1.Title = "保存的excel文件";
            saveFileDialog1.InitialDirectory = "c:\\";
            saveFileDialog1.Filter = "Excel97-2003 (*.xls)|*.xls|All Files (*.*)|*.*";
            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 ( " data acquisition bad! " );
                 return ; 
            } 
            WriteExcel (DS, path);       
        } 

 public  void WriteExcel (DS the DataSet, String path) 
        { 
            the try 
            { 
                Long The totalCount ds.Tables = [ 0 ] .Rows.Count; 
                lblTip.Text = " total " + + The totalCount " of data. " ; 
                the 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 ( " already generate the specified Excel file! " ); 
            } 
            The catch (Exception EX) 
            { 
                MessageBox.Show (ex.Message); 
            }             
        } 

public  String GetStrSql () 
        { 
            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.Hall 3] from tblReadCard group by cBarcode) d left join (select cBarcode viewer barcode 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. the audience barcode group by cBarcode, [Hall No. 1.1], [Hall No. 2.1], [Hall No. 3.1], [No. Hall 4.1], [Hall No. 5.1], [Hall No. 6.1] , [No. 8.1 Hall], [No. 1.2 Hall], [No. 2.2 Hall], [No. 3.2 Hall], [Hall 4.2], [5.2, Hall], [No. Hall 9.1], [No. 10.1 Hall], [ No. 11.1 Hall], [No. 12.1 Hall], [No. 13.1 Hall], [No. Hall 9.2], [No. 10.2 Hall], [11.2 Hall], [No. Hall 12.2], [13.2 Hall], [No. 9.3 Hall], [Hall No. 10.3], [11.3 No. Hall], [No. VIP3.2 Museum,], [No. VIP11.3], [No. VIP14.3 Hall][No. Hall 9.2], [No. 10.2 Hall] [Hall 11.2],, [No. 12.2 Hall] [Hall 13.2],, [No. 9.3 Hall], [No. 10.3 Hall], [No. 11.3 Hall], [VIP3 No. .2 Hall], [No. VIP11.3 Hall], [No. VIP14.3 Hall][No. Hall 9.2], [No. 10.2 Hall] [Hall 11.2],, [No. 12.2 Hall] [Hall 13.2],, [No. 9.3 Hall], [No. 10.3 Hall], [No. 11.3 Hall], [VIP3 No. .2 Hall], [No. VIP11.3 Hall], [No. VIP14.3 Hall]";
            return strSql;
        }

 

Guess you like

Origin www.cnblogs.com/brian93/p/11271484.html