Blog Home  Home Feed your aggregator (RSS 2.0)  
Venexus DotNetNuke Blog - Export DataSet to Excel
DotNetNuke Articles, Code Snippets, Errors, and News
 
 Friday, May 19, 2006

   The following Class can be used to export a dataset to XLS format. This code has been tested with Office 2002 and XP:

   Public Class DataSetToExcel
        Public Shared Sub Export(ByVal ds As DataSet, ByVal response As HttpResponse)
            'need to clear the response object
            response.Clear()
            response.Charset = ""
            'set the response type to excel
            response.ContentType = "application/vnd.ms-excel"
            'create a string writer anf HTMLTextWriter that uses it
            Dim StringWrite As New System.IO.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 and bind it
            dg.DataSource = ds.Tables(0)
            dg.DataBind()
            'tell the datagrid to render itself to our htmltextwriter
            dg.RenderControl(HTMLWrite)
            'output the html
            response.Write(StringWrite.ToString)
            response.End()
        End Sub
    End Class

Friday, May 19, 2006 6:40:01 PM (US Eastern Standard Time, UTC-05:00)  #       | 
Copyright © 2010 Venexus, Inc.. All rights reserved.