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

x
?
Solved

Export Dataset to Excel

Posted on 2007-04-05
4
Medium Priority
?
618 Views
Last Modified: 2012-06-27
I use the following script to export dataset to excel

Public Class DataSetToExcel

  Public Shared Sub Convert(ByVal ds As DataSet, ByVal response As HttpResponse)
    'first let's clean up the response.object
    response.Clear()
    response.Charset = ""
    'set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel"
    'create a string writer
    Dim stringWrite As New System.IO.StringWriter
    'create an htmltextwriter which uses the stringwriter
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    'instantiate a datagrid
    Dim dg As New DataGrid
    'set the datagrid datasource to the dataset passed in
    dg.DataSource = ds.Tables(0)
    'bind the datagrid
    dg.DataBind()
    'tell the datagrid to render itself to our htmltextwriter
    dg.RenderControl(htmlWrite)
    'all that's left is to output the html
    response.Write(stringWrite.ToString)
    response.End()
  End Sub
End Class

It has been working fine except now that I move the script to a new SharePoint server, it stops working.  Instead of exporting the dataset to an excel, it displays the XML on the screen.  Help.  

0
Comment
Question by:chaochenga
[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
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
ethoths earned 2000 total points
ID: 18856444
Response.Write writes things to the response stream. That usually means it will be displayed on the screen. However, if the client does not know how to handle the fille type it will try to save it. When you installed Sharepoint you probably set IE to recognise xls files so that they could be displayed. If this is the case then changin the line response.ContentType = "application/vnd.ms-excel" will stop it from happening.
 
0
 

Author Comment

by:chaochenga
ID: 18857355
What do I need to change the line response.ContentType = "application/vnd.ms-excel" to?
0
 

Author Comment

by:chaochenga
ID: 18857826
I removed the line response.ContentType = "application/vnd.ms-excel" and it is now showing data on the screen.  However, I need the save as dialog to pop up.  Help.
0
 

Author Comment

by:chaochenga
ID: 18858049
I got it to work with the following

Response.AppendHeader( "content-disposition",  "attachment; filename=" + name )
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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

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