Solved

Export Dataset to Excel

Posted on 2007-04-05
4
607 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
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
ethoths earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

22 Experts available now in Live!

Get 1:1 Help Now