Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

stop gridview export to excel exporting style

Posted on 2012-03-23
9
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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