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 File format issue in producing Excel file

Hi

I am using the VB.net code below produce an Excel file  from a data table in my ASP.net web app. I get the following message
when opening the Excel file in Excel 2007. Surely Excel 2007 should recognize .xls as an Excel 97 to 2003 file.
Is there a way to stop this message coming up? Can this code be improved?

User generated image
  Public Sub CreateExcelFile(Excel As DataTable)

        'http://www.c-sharpcorner.com/UploadFile/0c1bb2/create-excel-file-from-database-using-Asp-Net-C-Sharp/

        'Clears all content output from the buffer stream.   
        Response.ClearContent()
        'Adds HTTP header to the output stream   
        Response.AddHeader("content-disposition", String.Format("attachment; filename=MyExcelDowbload.xls"))

        ' Gets or sets the HTTP MIME type of the output stream   
        Response.ContentType = "application/vnd.ms-excel"
        Dim space As String = ""

        For Each dcolumn As DataColumn In Excel.Columns

            Response.Write(space + dcolumn.ColumnName)
            space = vbTab
        Next
        Response.Write(vbLf)
        Dim countcolumn As Integer
        For Each dr As DataRow In Excel.Rows
            space = ""
            For countcolumn = 0 To Excel.Columns.Count - 1

                Response.Write(space & dr(countcolumn).ToString())

                space = vbTab
            Next

            Response.Write(vbLf)
        Next
        Response.[End]()
    End Sub

Open in new window

SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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
ASKER CERTIFIED 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
Avatar of Murray Brown

ASKER

Thank you both.