Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag 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

Avatar of Kimputer
Kimputer

Open Nuget in your project, add  EPPlus.

More info here:

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

ASKER

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both very much