Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all.
i m exporting data from grid view to excel.
though its working perfectly but its taking too much time to export.
is there any other faster way to export.?
if so please help me.

giving codes.

C#
private void btnExport_Click(object sender, EventArgs e)
        {
            
            Cursor.Current = Cursors.WaitCursor;
            lblWait.Visible = true;

            try
            {
                if (rbAll.Checked == true)
                {
                    filename = "All Record_"+divisionname;
                }
                else
                {
                    filename = divisionname +"-" + dtSearchDate.Value.ToString("d-MM-yyy");
                }
                if (dt.Rows.Count > 0)
                {
                    oXL = new Excel.Application();

                    // Set some properties
                    oXL.Visible = false;
                    oXL.DisplayAlerts = false;

                    // Get a new workbook.
                    oWB = oXL.Workbooks.Add(Missing.Value);

                    // Get the active sheet
                    oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                    oSheet.Name = "Record";

                    int rowCount = 1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        rowCount += 1;
                        for (int i = 1; i < dt.Columns.Count + 1; i++)
                        {
                            // Add the header the first time through
                            if (rowCount == 2)
                            {
                                oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                            }
                            oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
                        }
                    }

                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.FileName = filename.ToString();
                    sfd.Filter = "Excel Documents (*.xls)|*.xls";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        ToCsV(gvDetail, sfd.FileName); // Here gvDetail is your grid view name
                        oXL.Visible = false;
                        MessageBox.Show("Export Complete..!");
                        oXL.Application.Quit();
                        lblWait.Visible = false;

//----close previous process of excel if opened----//
                        foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
                        {
                            if (process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE"))
                            {
                                process.Kill();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("File Not Saved..!");
                        lblWait.Visible = false;
                    }
                }
                else
                {
                    MessageBox.Show("No Record Found To Export..!");
                    lblWait.Visible = false;
                }
            }
            catch
            {
                MessageBox.Show("Export Failed..!");
                clearcontrols();
                lblWait.Visible = false;
            }
        }



thanks in advance
Posted
Updated 29-Sep-13 23:53pm
v3

1 solution

Found SomeThing For You:
C#
string filename = "DownloadMobileNoExcel.xls";
     System.IO.StringWriter tw = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
     DataGrid dgGrid = new DataGrid();
     dgGrid.DataSource = dt;
     dgGrid.DataBind();

       //Get the HTML for the control.
       dgGrid.RenderControl(hw);
       //Write the HTML back to the browser.
       //Response.ContentType = application/vnd.ms-excel;
       Response.ContentType = "application/vnd.ms-excel";
   Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
       this.EnableViewState = false;
       Response.Write(tw.ToString());
       Response.End();
 
Share this answer
 
v2
Comments
dibyaaryan007 3-Oct-13 0:47am    
hello Adhikari Surendra sir
Thanks For your precious reply but i am working on windows application.
this code is for web applications.
dibyaaryan007 3-Oct-13 0:51am    
i am hunting for codes that ll help me to export more than 1 lac record from grid view.
Surendra Adhikari SA 4-Oct-13 3:11am    
http://www.codeproject.com/Articles/21519/Fast-Exporting-from-DataSet-to-Excel
this may help

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