troubleshooting Question

ASP.net VB.net Create Excel file with sheet name "Sheet1"

Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NETVisual Basic.NETSQL
5 Comments2 Solutions99 ViewsLast Modified:
Hi

I am using the following VB.net code on my ASP.net web form to populate an Excel spreadsheet
from a data table. At the moment the sheet name is derived from the SQL table that
the data table was pulled from. I need the sheet name to be "Sheet1". How do I do this

    Public Sub ExportToExcel_AsXlsFile(ByVal dt As DataTable, ByVal file_name As String)
        Dim grid = New GridView()
        grid.DataSource = dt
        grid.DataBind()
        Response.ClearContent()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment; filename='" & file_name & "'.xls")
        Response.ContentType = "application/ms-excel"
        Response.Charset = ""
        Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        grid.RenderControl(htw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.[End]()
    End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros