Visual Studio Tools for OfficeBe sure to specify which version you are working with.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Studio Tools for Office section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Datagrid.RenderControl creates excel spreadsheet in invalid format?
I am using this code to turn a datagrid's contents into an excel spreadsheet:
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim dgExcelOutput As New DataGrid
dgExcelOutput.DataSource = r_dvReport
It worked fine with Office 2003 . Upon upgrading to Office 2007 it's displaying this message:
The file you are trying to open, "test.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 the file. Do you want to open the file now?
Answering YES opens the spreadsheet just fine. It does not appear to me to be corrupted in any way.
Changing the ContentType and file extesion as some sites suggest:
Returns the same message even though the file extension is now "xlsl" (and the rest of the file name specified in Response.Addheader appears to be ignored):
The file you are trying to open, "frmExcel.xlsl" 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 the file. Do you want to open the file now?
So, what is necessary to suppress this *&^%$ message box?
Last edited by Ron Howerton; October 15th, 2009 at 02:23 PM.
Reason: Changed Title
Thanks for your response! I would be happy just to prevent the box from being displayed, but there doesn't seem to be anywhere I can insert the suggested code. I can't reference Excel on the server because I want it to open on the client. But the result of the code in question is a file download dialog box on the client, not a web page. The only reference to Excel is in the server code specifying the Response.ContentType and the extension of the file name if it is specified. IOW, I can't seem to tell Excel to suppress messages before it opens because it's being opened by the file download dialog box, not via script, and I therefore cannot pass it parms first. But I'd be happy to be proven wrong about that if somebody can suggest how I might do so!
Otherwise, it appears that I need to resolve the apparent conflict between the Response.ContentType and extension of the file name specified (which, incidentally, defaults to .XLS when not specified). The .XLSX extension Excel 2007 wants to save does not appear to be associated with either "application/vnd.ms-excel" (the original ContentType that worked with Excel 2003) or
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", which I have found referenced on several sites as needed by Excel 2007. I've tried playing with combinations of file extensions and ContentType with no success. I did discover that the ContentType is ignored when .DOC file extension is specified; it will open Word, not Excel despite the ContentType indicated. So Excel apparently cares about the file format where Word does not. .HTM opens a new browser page, again regardless of whether ContentType is indicated.
I'm beginning to wonder whether overlaying MS Office 2003 with 2007 is the cause of the problem? In any case, our users are just going to have to live with another annoying message box. This is a minor hassle compared to other issues.