Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I want to get data from database and export it into excel on button click.There can be more than 2 lacs records in datatable.My code is working fine but it's taking 5 minutes to export data in excel.So its very slow in performance thats main issue for me.

I tried below code:

C#
public void Export()
      {
          Response.Clear();
          Response.Buffer = true;
          Response.AddHeader("content-disposition", "attachment;filename=Summary.xls");
          Response.Charset = "";
          Response.ContentType = "application/vnd.ms-excel";
          using (StringWriter sw = new StringWriter())
          {
              HtmlTextWriter hw = new HtmlTextWriter(sw);

              //To Export all pages
              GVExport.AllowPaging = false;
             // this.BindGrid();

              GVExport.HeaderRow.BackColor = Color.White;
              foreach (TableCell cell in GVExport.HeaderRow.Cells)
              {
                  cell.BackColor = GVExport.HeaderStyle.BackColor;
              }
              foreach (GridViewRow row in GVExport.Rows)
              {
                  row.BackColor = Color.White;
                  foreach (TableCell cell in row.Cells)
                  {
                      if (row.RowIndex % 2 == 0)
                      {
                          cell.BackColor = GVExport.AlternatingRowStyle.BackColor;
                      }
                      else
                      {
                          cell.BackColor = GVExport.RowStyle.BackColor;
                      }

                  }
              }

              GVExport.RenderControl(hw);

              //style to format numbers to string
             // string style = @"<style> .textmode { } </style>";
             // Response.Write();
              Response.Output.Write(sw.ToString());
              Response.Flush();
              Response.End();
          }







Please let me know if you have better approach to do this.

Thanks and regards
Rahul
Posted
Updated 11-Nov-14 20:22pm
v2
Comments
Rahul_Pandit 12-Nov-14 4:47am    
No reply after 2 hours, Its strange.
Richard MacCutchan 12-Nov-14 9:45am    
Not strange at all. How can anyone answer such a question? Exporting that amount of data will obviously take a long time.
Mathew Soji 12-Nov-14 10:13am    
If there is feasibility , may be a SSIS package ( any ETL tool) for exporting to excel and triggering the package from ASP.net export code will help.
Rahul_Pandit 13-Nov-14 1:46am    
You mean its not possible in .net as I see from performance point of view. Can You explain with an example Methew? SSIS is the only solution?? I dont know about it.
Rahul_Pandit 25-Nov-14 0:57am    
Guys help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900