Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Visual Studio Tools for Office
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Studio Tools for Office Be 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 15th, 2009, 01:21 PM
Friend of Wrox
Points: 2,514, Level: 20
Points: 2,514, Level: 20 Points: 2,514, Level: 20 Points: 2,514, Level: 20
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 407
Thanks: 57
Thanked 1 Time in 1 Post
Default 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
dgExcelOutput.DataBind()
dgExcelOutput.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.End()

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:

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("Content-Disposition", "attachment;Filename=test.xlsl")

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 01:23 PM. Reason: Changed Title
Reply With Quote
  #2 (permalink)  
Old October 15th, 2009, 10:03 PM
Friend of Wrox
Points: 2,950, Level: 22
Points: 2,950, Level: 22 Points: 2,950, Level: 22 Points: 2,950, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 788
Thanks: 1
Thanked 51 Times in 47 Posts
Default

Most of these warning messages in Excel can be suppressed by setting the Displayalerts to false

Application.DisplayAlerts = False

Not sure if it will work for you

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #3 (permalink)  
Old October 16th, 2009, 09:46 AM
Friend of Wrox
Points: 2,514, Level: 20
Points: 2,514, Level: 20 Points: 2,514, Level: 20 Points: 2,514, Level: 20
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 407
Thanks: 57
Thanked 1 Time in 1 Post
Default

Sashur,

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using Page.RenderControl() net_hunk .NET Framework 2.0 2 May 26th, 2014 04:26 AM
RenderControl is Giving the Error. dpkbahuguna ASP.NET 3.5 Basics 1 June 12th, 2009 02:43 PM
RenderControl and Render methods joxa83 ASP.NET 1.x and 2.0 Application Design 4 August 24th, 2006 08:10 AM
Using rendercontrol with parameters? shenku C# 2 October 13th, 2005 09:21 AM
Format datagrid AliH Access VBA 1 June 18th, 2003 02:16 AM



All times are GMT -4. The time now is 09:54 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.