ASP.NET Excel Export using textbox

Posted on 2011-05-04
Last Modified: 2013-12-16
I have C# code that exports an ASP.NET page perfectly to Excel when the GridView being exported is just plain text.  However, when exporting textboxes, Excel runs very, very slow and Red X's show up for the data for each textbox for a very long time before the correct data shows up in a textbox, if at all, on the Excel file iteself.  Any idea how I can get these to export as just plain text and not a textbox?
Question by:melli111
    LVL 17

    Expert Comment

    by:Carlos Villegas
    Hi, please post the code that export your gridview to excel so I can able to help you.
    LVL 16

    Accepted Solution

    use this code to export data of ur grid to excel
    Private Sub cmdExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdExport.Click
                Dim dt As New DataTable
              dt=GetMyDataFromDB()'this is a function you have to write to return the data from db into dt
                 if you want to remove some column use this code
                dt.Columns.Remove("You Coloumn name")
                HttpContext.Current.Response.ContentType = "Application/x-msexcel"
                HttpContext.Current.Response.Clear() '
                'add the response headers
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=data.xls")
                Response.Write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />")
                'create our datagrid object
                Dim csvGrid As DataGrid = New DataGrid()
                'set the datasource for datatable
                csvGrid.DataSource = dt 'DsClon.Tables(0)
                'bind the values in our datatable to our grid
                'create our stringwriter object
                Dim StringWriter As System.IO.StringWriter = New System.IO.StringWriter()
                'create our htmltextwriter
                Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(StringWriter)
                'render the contents of our datagrid to the htmlwriter
                'since the convert to string the value of our stringwriter object.
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8
                '                       //close the connection
                '                       command.Connection.Close();
                'end the http response
            Catch ex As Exception
            End Try
        End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now