CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    231

    Export datatable to excel file

    Hello gurus!

    I use the following code (see bottom of post) to create a native xls file, with Microsoft Office 2007 installed. However, when I open the file there's always a warning telling me:

    "The file you are trying to open, 'file.xls' is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening this file. Do you want to open the file now?"

    When clicking "Yes" the file opens just fine.

    My question is: How can I get rid of this warning? It is obviously not corrupted, and the datatable contains no special characters. I don't see how the format is related to "trusted source", whatever that means. Can someone please explain?

    Code:
    Protected Sub ExportAsExcelFile(ByVal dtExport As DataTable)
    
            Response.Clear()
            Response.ContentType = "application/vnd.ms-excel"
    
            Dim objStringWriter As New System.IO.StringWriter
            Dim objHtmlTextWriter As New System.Web.UI.HtmlTextWriter(objStringWriter)
    
            Dim objDatagrid As New DataGrid
            objDatagrid.DataSource = dtExport
            objDatagrid.DataBind()
    
            objDatagrid.RenderControl(objHtmlTextWriter)
    
            Response.Write(objStringWriter.ToString())
            Response.End()
    
    End Sub

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Export datatable to excel file

    Nothing wrong in your source code. It's only because you have Office 2007 installed. Did you ever try on another computer having different verson of Office? Webpage only exports 97-2003 excel format, you would get the warning if you use 2007 to open it.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured