export quickly .NET Datagrid to excel

i am trying to write code to export.NET  datagrid to excel in C#

I have written the code below. the code works fine if the record count is less...
However for reccords count greater than 1000, it takes 10-20 minutes!!!!...
is there any other way to export datagrid to excel or pl help me to make this run faster!!

i have tried exporting it to a CSV file but my data has lot of commas and semicolons!!! so that wonot help..

MY Code:
         private void butExportExcel_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                OracleConnection oc = new OracleConnection(this.OraConnectionString);
                oc.Open();
                OracleCommand ocmd=new OracleCommand(this.MainQueryString,oc);
                OracleDataReader odr= ocmd.ExecuteReader();
                Excel.ApplicationClass excel = new Excel.ApplicationClass();                
                Excel.Workbook wb = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                int ColumnIndex = 0;
                int totfield=odr.FieldCount;
                //excel.Visible = true;
                //
                for (int i = 0; i < totfield; i++)
                {
                    String strfield = odr.GetName(i);
                    ColumnIndex++;
                    excel.Cells[1, ColumnIndex] = strfield;
                }
                int rowIndex=1;
                while (odr.Read())
                {
                    rowIndex++;
                    ColumnIndex = 0;
                    for (int i = 0; i < totfield; i++)
                    {
                        ColumnIndex++;
                        System.Object obj;
                        obj = odr.GetValue(i);
                        String StrValue = obj.ToString();
                        excel.Cells[rowIndex, ColumnIndex] = StrValue;
                    }
                }
                excel.Visible = true;
                Excel._Worksheet worksheet = (Excel._Worksheet)excel.ActiveSheet;
                worksheet.Activate();
            }
            catch (OracleException oex)
            {
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error exporting data to excel", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Cursor = Cursors.Default;  

           
        }


at999Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

btumerCommented:
Plz excuse the VB.
This one is actually getting the DataSource passed from another page in the session object (the other page allows the user to filter the date pretty extensively), but otherwise it's the same.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sqlIssues As SqlDataSource
        sqlIssues = DirectCast(Session("datasource"), SqlDataSource)
        gvProjects.DataSource = sqlIssues
        gvProjects.DataBind()
        exportRoutine()
        goBack()
    End Sub
   
    Protected Sub exportRoutine()
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        Dim frm As HtmlForm = New HtmlForm()
        Me.Controls.Add(frm)
        frm.Controls.Add(gvProjects)
        frm.RenderControl(hw)
        Response.Write(tw.ToString())
        Response.End()
    End Sub
    
    Protected Sub goBack()
        Session.Remove("datasource")
        Response.Redirect("~/Default.aspx")
    End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
at999Author Commented:
Thanks btumer,,,

I am not doing this in ASP website..
I am looking code for  a .NET Windows Application = to export datagrid to excel...

Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.