• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Error outputting DataGrid to Excel File

letsgetsilly

Senior member
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()
 
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")
 
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.
 
Back
Top