Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

stop gridview export to excel exporting style

Posted on 2012-03-23
9
Medium Priority
?
332 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 45

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 45

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 45

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 45

Accepted Solution

by:
AndyAinscow earned 1500 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 45

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

877 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