views:

2698

answers:

9

I've noticed that Internet Explorer adds a number in square brackets to files downloaded from the internet (usually [1]). This creates a big problem with downloading Excel spreadsheets as square brackets are not a valid filename character inside Excel worksheet name. That problem is IE specific, others browsers are keeping same file name.

So, if you have a pivot table auto-refreshed on file opening for example, you'll get an error message saying the name "file[1].yourPivotTableName" is not valid.

Is there any solution to that problem ?

EDIT : It seems that whatever the filename suggested by HTTP directives, IE adds [1] in all cases, which cause the problem ! (So, answers about filenames aren't helpful in that case)

EDIT : I've tried some VBA code to save file under another name when it'll open. However, it doesn't work (same error message than before). Do you think there's a way to fix that with VBA ?

A: 

Actually, the correct .NET-code is as following:

Response.AppendHeader("content-disposition", "attachment;filename=file.xls");
Response.ContentType = "application/vnd.ms-excel";

Note: AppendHeader, not AddHeader, which I think only works in debug web-server and IIS7.

Seb Nilsson
Stop down-voting good answers to badly asked questions.
Seb Nilsson
A: 

In .NET I have found from experience only this seems to work for me:

            Response.AddHeader("Content-Disposition", "attachment; filename=excel.xls");
            Response.AddHeader("Content-Type", "application/vnd.ms-excel");
            Response.ContentType = "application/vnd.ms-excel";

The duplication smells, but so far I have never got to the bottom of it (maybe Sebs post explains this). Also the "content-Disposition" value appears very finicky use a : instead of a ; or ommit the space between it and 'filename' and it blows!

Also if you have compression enabled on IIS this may fix things for you:

Response.ClearHeaders()
HollyStyles
+2  A: 

I think that this happens when you open the spreadsheet in IE and IE saves it to a temporary file. And I think it only happens when the spreadsheet's filename has more than one dot in it. Try it with a simple "sample.xls". Another workaround is to tell users to save the file to the desktop and then open it.

Liam
I can't, in my case, use a single sheet but, you're true, the problem occurs only when you'll try to open the file directly, not if you'll save it.But that's not a good "customer friendly" answer... :)
paulgreg
What's your server setup? IIS? Apache? Using a CMS? Would URL rewriting help?
Liam
My webapp is J2EE based, working on IBM Web Application Server, behind a IBM modified apache server (IHS). URL rewriting will not help, because it's the way IE works. I think more about some VBA macros to rescue me...
paulgreg
+2  A: 

It's a built-in feature in Internet Explorer.

Stop using "Open", start using "Save" in the file-download window, otherwise IE will append "[1]" to filename of the file that it places in some temporary folder.

You could build some .NET application using System.IO.FileSystemWatcher that catches the event of the creation of the downloaded file or something and renames the file.

Seb Nilsson
+2  A: 

I've got it working using VBA provided by this cool guy (think of him fondly). It renames the file and then reattaches the pivots.

http://php.kennedydatasolutions.com/blog/2008/02/05/internet-explorer-breaks-excel-pivot-tables/

Thanks a lot, I think that the solution. I'll try that.
paulgreg
A: 

The following has worked for me:

private string EncodeFileName(string fileName)
 {
  fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", " ");
  if (HttpContext.Current.Request.UserAgent.ToLower().Contains("msie"))
  {
   var res = new StringBuilder();
   var chArr = fileName.ToCharArray();
   for (var j = 0; j < chArr.Length; j++)
   {
    if (chArr[j] == '.' && j != fileName.LastIndexOf("."))
     res.Append("%2E");
    else
     res.Append(chArr[j]);
   }
   fileName = res.ToString();
  }
  return "\"" + fileName + "\"";
 }
A: 

You could just make sure that in the options box for the pivot the auto refresh is switched off. Now even when opened from the server the pivot will work perfectly

Saurabh Pal
A: 

Hi

put these 4 lines in your code

response.reset(); response.setHeader("Expires", "0"); response.setHeader("Cache-Control","must-revalidate,post-check=0, pre-check=0"); response.setHeader("Pragma", "public");

i hope this wiil help sure

Arvind Kumar

Arvind Kumar
A: 

I have encountered the same problem and came up with (imo) a better solution that does not need any VBA.

If you set "Content-Disposition" header to "attachment; filename=<...>" instead of "inline; filename=<...>" the normal browsers will open dialog that will allow to save or open a file with a filename defined in a header, but Internet Explorer will behave in kind of weird way. It will open file download dialog and if you press Save it will suggest a filename that is defined in the header, but if you press Open it will save file to a temporary folder and open it with a name that is the same as your URN (without 'namespace'), e.g. if your URI is http://server/folder/file.html, so IE will save your file as file.html (no brackets, woo hoo!). This leads us to a solution:

Write a script that handles request from http://server/folder/* and when you need to serve an XLS file just redirect to that script (use your filename instead of asterisk) with Content-Disposition set to inline.

Žygimantas