Solved

stop gridview export to excel exporting style

Posted on 2012-03-23
9
326 Views
Last Modified: 2012-04-15
Hi,

I have the following method which works fine in terms of exporting my gridview to excel

Protected Sub ExportToExcel2(ByVal objDT As DataTable, ByVal authAbbreviation As String)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls")
        Response.Charset = ""
        EnableViewState = False

        Dim sw = New System.IO.StringWriter()
        Dim htw = New System.Web.UI.HtmlTextWriter(sw)

        gvResults.DataSource = objDT
        gvResults.DataBind()

        gvResults.RenderControl(htw)

        Response.Write(sw.ToString())
        Response.End()
    End Sub

Open in new window


The only problem is in the folder where the file is saved also gets created a directory that holds html and css for the file.

How can I avoid this so all the user gets is an excel file?
0
Comment
Question by:scm0sml
  • 5
  • 4
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37756366
Does this work?
Protected Sub ExportToExcel2(ByVal objDT As DataTable, ByVal authAbbreviation As String)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls")
        Response.Charset = ""
        EnableViewState = False

        Dim sw = New System.IO.StringWriter()
rem        Dim htw = New System.Web.UI.HtmlTextWriter(sw)

        gvResults.DataSource = objDT
        gvResults.DataBind()

rem        gvResults.RenderControl(htw)

        Response.Write(sw.ToString())
        Response.End()
    End Sub

Open in new window

0
 

Author Comment

by:scm0sml
ID: 37756379
does rem mean remove?

If I do sw never gets set to anything?

SOrry if I am asking something stupid!
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37756389
That looked like VB code - in VB a rem is a 'remark'.  It means that line of code is ignored by the compiler.
In other words just delete those two lines.



You want to not have the html and css output.
The line of code:
System.Web.UI.HtmlTextWriter
is telling the program to create html and css output - hence my suggestion to remove that line (and the other which depends on it) and see if it cures your problem.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:scm0sml
ID: 37756391
ok but if i do that at no point am i exporting the data in the data table...

what should go in its place?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37756398
I assumed lines 12 & 13 did that:
        gvResults.DataSource = objDT
        gvResults.DataBind()

Just what is gvResults ?  and what is Response ?
0
 

Author Comment

by:scm0sml
ID: 37756403
gvResults is a gridview.

Basically I was setting the datasource of the gridview and then epxorting that...
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37756430
Hmmm.
I am convinced your problem is with this HtmlTextWriter - that doesn't just dump the data but everything about the control (gridview) and so you are getting the folder with the html and css info.

I just don't see an easy way around this.
0
 

Author Comment

by:scm0sml
ID: 37756443
An export of the datatable direct?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37756488
There is a WriteXML member of a DataTable - I don't know if you could use that to export to an excel file directly.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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