Solved

stop gridview export to excel exporting style

Posted on 2012-03-23
9
323 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

17 Experts available now in Live!

Get 1:1 Help Now