Error outputting DataGrid to Excel File

letsgetsilly

Senior member
Oct 27, 2002
397
0
0
I am using the code located below to output my datagrid to an excel file.

Everything worked great yesterday, but today there is a problem when you select 'Open' from the "Open, Save, or Cancel" dialogue for the XLS file.

The error looks something like:
"C:\....Temporary Internet Files\....\TimeReport[7].xls' Could not be found. Check spelling and verify location is correct"

I'm guessing that internet explorer is trying to populate an excel sheet that existed yesterday but does not exist today. I've cleared my cookies but it still attempts to fill up these strange excel files.

When I choose the "save" option, it works fine.

Any tips on how to point the code in the right direction when I am opening the excel sheet in a temporary directory?

thanks!

Code:
Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=TimeReport.xls")
        Response.Charset = ""
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.ContentType = "application/vnd.xls"
        Dim stringWrite = New System.IO.StringWriter
        Dim htmlWrite = New HtmlTextWriter(stringWrite)

        dataGrid.RenderControl(htmlWrite)
        Response.Write(stringWrite.ToString())
        Response.End()
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Why are you making it uncacheable?

You might also try changing the filename for each download using any method you can think of.
Response.AddHeader("content-disposition", "attachment;filename=TimeReport" & System.Environment.TickCount & ".xls")
 

letsgetsilly

Senior member
Oct 27, 2002
397
0
0
I didn't realize I was making it non cacheable. I was following an example in a book. Thanks for the tip!

*EDIT I've switched to Response.Cache.SetCacheability(HttpCacheability.Private)

I believe that this will set it for user preference, but I wanted to check with you to make sure this is the correct method to make it uncacheable.

It is working with this method...but it was working yesterday so I have my doubts.

Thanks again for your help.