[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET Excel Export using textbox

Posted on 2011-05-04
2
Medium Priority
?
529 Views
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?
0
Comment
Question by:melli111
2 Comments
 
LVL 17

Expert Comment

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

Accepted Solution

by:
Kamal Khaleefa earned 2000 total points
ID: 35695768
hi
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

        Try


          
            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
            csvGrid.DataBind()
            '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
            csvGrid.RenderControl(htmlWriter)
            'since the convert to string the value of our stringwriter object.
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8
            HttpContext.Current.Response.Write(StringWriter.ToString())
            '                       //close the connection
            '                       command.Connection.Close();
            'end the http response
            HttpContext.Current.Response.End()










        Catch ex As Exception
    
        End Try








      

    End Sub

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

864 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