• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

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;  

           
        }


0
at999
Asked:
at999
1 Solution
 
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

0
 
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now