Solved

stop gridview export to excel exporting style

Posted on 2012-03-23
9
325 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I build my own IDE using ASP.NET MVC? 2 34
About delegates in c sharp 3 38
Server Error 11 48
How would you add MULTITHREADING to the attached C# code? 4 53
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

21 Experts available now in Live!

Get 1:1 Help Now