-
February 13th, 2009, 05:41 AM
#1
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
-
February 13th, 2009, 10:37 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|