Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

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

Open in new window

ASP.NETVisual Basic.NETSQL

Avatar of undefined
Last Comment
Murray Brown

8/22/2022 - Mon
Kimputer

Open Nuget in your project, add  EPPlus.

More info here:

https://archive.codeplex.com/?p=epplus
Murray Brown

ASKER
Thanks. Is there not a simpler way to change the code?
ASKER CERTIFIED SOLUTION
Kimputer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Murray Brown

ASKER
Thank you both very much
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes