Solved

Export Dataset to Excel

Posted on 2007-04-05
4
610 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn on intranet settings 1 55
Deploying to Azure 3 27
tutorial for ebay api 3 32
Need to start a web service from Visual Studio 2015 Pro 2 21
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

15 Experts available now in Live!

Get 1:1 Help Now