Want to work with us? We're on the lookout for digital experts.

We're hiring

Export Datatable to Excel

Web Bureau

17 December 2010 by Web Bureau

To follow up on my last post this is code to export any datatable to excel.

Public Shared Function Export_Excel(ByVal dt As DataTable, ByVal filename As String) As String

        Dim message As String = Nothing

        Dim tw As New System.IO.StringWriter
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)

            Dim dg As DataGrid = New DataGrid
            dg.DataSource = dt

            Dim context As HttpContext = HttpContext.Current

            context.Response.AddHeader("content-disposition", "attachment;filename=" & filename & ".xls")
            context.Response.ContentType = "application/"
            context.Response.Charset = ""


            context.Response.ContentEncoding = System.Text.Encoding.Unicode

            message = "Export Successful"

        Catch ex As Exception

            message = ex.Message.ToString

        End Try

        Return message

    End Function

Grow your businessStart a project with us today.

This site uses essential cookies for parts of the site to operate and have already been set. Find out more about how we use cookies and how you may delete them. You may delete cookies, but parts of the site will not work.