How to export dataGridView data Instantly to Excel on button click?

前端 未结 14 2231
执笔经年
执笔经年 2020-11-28 05:21

I have 10k rows and 15 column in my data grid view. I want to export this data to an excel sheet o button click. I have already tried with the below code.

pr         


        
14条回答
  •  佛祖请我去吃肉
    2020-11-28 05:31

    that's what i use for my gridview, try to use it for yr data , it works perfectly :

            GridView1.AllowPaging = false;
            GridView1.DataBind();
    
            StringBuilder sb = new StringBuilder();
    
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Columns[k].HeaderText+";");
    
            }
    
    
            //append new line
            sb.Append("\r\n");
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                for (int k = 0; k < GridView1.Columns.Count; k++)
                {
                    sb.Append(GridView1.Rows[i].Cells[k].Text+";");
                }
                sb.AppendLine();
            }
    

提交回复
热议问题