locked
Export Datatable to Excel as a "real" XLS file? RRS feed

  • Question

  • User-1289522517 posted
     

    Hi,

     I have been trying to get a datatable exported to excel.  I have tried creating datagrid and binding to the data and rendering the code to HTML (see below), exporting as a CSV file, etc and naming the with a .XLS extension.  All of these created files that user's have issues with because they are not true XLS file.  That is, when rendered to HTML if you open the file in Excel and do a "Save As" the defaults will be a file type of HTML.  Similarly, if you open a CSV file Save changes to CSV.   Does anyone know how to create a "real" XLS file?

    Here is the code I am using to generate the HTML variant

    1            Dim filenameOnly As String = Path.GetFileName(fileName)
    2 Response.Clear()
    3 Response.ClearContent()
    4 Response.ClearHeaders()
    5 Response.Buffer = True
    6
    7 Response.Charset = ""
    8 Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", filenameOnly))
    9 Response.ContentType = "application/excel"
    10
    11 Dim dg As New DataGrid
    12 dg.DataSource = dt
    13 dg.DataBind()
    14 Dim sw As StringWriter = New StringWriter
    15 Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    16 dg.RenderControl(htw)
    17
    18 Response.Write(sw.ToString)
    19 Response.End()

     

    jerry

     

    Wednesday, August 20, 2008 1:53 PM

All replies

  • User-1320279932 posted

    Have you found the solution??Because I've same problem...

    Monday, September 22, 2008 3:09 AM