2

This is my first question on this forum, so please forgive any newbie errors.

I have .Net code, within a web application, that creates an html table dynamically from data pulled from a database, then streams it to the user's web browser to be opened in Excel. This works just fine in Chrome. It also works in IE 11 as long as the user, after clicking the "Download" link, chooses the "Save" option (saving the file before opening it), rather than the "Open" option - which tries to read the file being streamed to the client. But in IE 11, if they choose the "Open" option, they will get a warning from Excel about the format of the file (because it is an html file - same thing happens if they save first, and this is acceptable), but when they click "Yes" to open anyway, they see an "Unable to read file." error message.

In one case, I was able to fix this problem by replacing instances if "&" in the data with the word "and". However, in other cases, I cannot determine what particular data value might be causing the issue. And again, the file opens just fine as long as the user saves it first. (In fact, it even works fine when I click "Open" instead of "Save" in my development environment - but not when I do it from the test server where the code is deployed.)

So, I am looking either for a way to remove/replace all problematic data displayed within the "td" elements of the html table, or, better yet, some way to just make the "Open" option have the same result as the "Save" option. Again, the html table content opens just fine as long as the file is saved first.

The html table is generated by a C# class, but the page that streams the file is in VB.Net. I don't think the C# code would be useful (but I can post it in part if anyone thinks it would be), but here is the VB code in relevant part:

Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Dim htmlTable As String
'[snip - populate htmlTable with data from database]
Dim fileName As String = "Whatever.xls"
Response.Buffer = false
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & fileName & """")
Response.AddHeader("Content-Length", ASCIIEncoding.ASCII.GetByteCount(htmlTable).ToString())

Using ms As New MemoryStream(Encoding.ASCII.GetBytes(htmlTable))
  ms.WriteTo(Response.OutputStream)
  Response.Flush()
End Using

UPDATE: I may have fixed this issue by "cleansing" the data with the (C#) code below, but I still don't understand why the file ever has to be saved first, which would have made this unnecessary. Any answer would be welcome.

private string GetDataRow(DataRow dr, List<DataColumnInfo> fieldInfoList)
{
  var sb = new StringBuilder("<tr valign='top' align='left'>");
  // see http://stackoverflow.com/questions/4619909/format-html-table-cell-so-that-excel-formats-as-text
  var forceExcelText = "mso-number-format:\"\\@'\"";

  fieldInfoList.ForEach(c =>
  {
    var content = dr[c.FieldName].ToString()
      .Replace("&", "and")
      .Replace("<", "")
      .Replace(">", "");
    // replace all non-asccii
    content = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(dr[c.FieldName].ToString()));
    sb.Append("<td style='" + forceExcelText + "'>" + content + "</td>");
  });
  sb.Append("</tr>");
  return sb.ToString();
}

NEW UPDATE - the above code did not fix the problem. Still get error in some cases when choose "Open" option instead of "Save" first. If I save first, all is fine.

T.S.
  • 18,195
  • 11
  • 58
  • 78
JRS
  • 569
  • 9
  • 26
  • if you want your users open this file using `Excel`, why not send comma-delimited file `CSV` instead of `HTML`? – T.S. Jun 26 '14 at 17:19
  • Requirement from business users is that users should not have to format their Excel file after opening it (as easy as that is). With an html table, the headers will be bold and the columns will all be expanded to fit the content without the user having to do anything. As far as I know, there is no way to accomplish that with a csv file. With html, they will get the warning prompt, but business users don't care about that. – JRS Jun 26 '14 at 17:38
  • Do they have to edit that file? And, you can also create "actual" xls file as template, with text format etc. Then fill that file using `microsoft ACE oleDb`, just pump data straight from DB to workbook, like between to DBs. Every time you would grab the template, fill and send. – T.S. Jun 26 '14 at 17:55
  • They can do whatever they like with the file, but they would not be uploading it for purposes of saving changes back to the system (which I imagine is what you are getting at). – JRS Jun 26 '14 at 18:24
  • Regarding your suggestion of using microsoft ACE oleDb, that might be an option I need to explore if they are not OK with just Saving the file before opening it (so easy!). But really this doesn't get to the heart of what I'm really trying to figure out - why is the file not readable unless it's first saved to disk? I mean, it's the exact same file - identical content. Why is Excel having a problem opening it from the stream? – JRS Jun 26 '14 at 18:27
  • You see, when excel opens from the browser directly, it opens from internet sandbox. Applications that run out of it don't have same permissions as applications that run by user double-click operation. Basically, it is most likely the security that makes Excel act like this. But I still think that delivering data via `xls` is strange. You could give them a `pdf` if reading data is all you care about. – T.S. Jun 26 '14 at 18:34
  • Oh, on the .pdf option, that would not work at all. These are large data sets, and the users want to be able to filter and sort and do all the stuff Excel allows them to do. A static report would not do. – JRS Jun 26 '14 at 19:52
  • Thanks for the information on the sandbox - maybe it is a security thing. But it's not consistent. Some data sets open up fine, while others generate the error. So that makes me think it's something to do with the parsing of data as it's sent down the stream - hard for me to think of why a security function would impede that. – JRS Jun 26 '14 at 19:54
  • I am having the same issue with my application . Did you find the answer for this or what is the other solution that worked. – Grasshopper Jun 12 '15 at 17:27
  • Never found a solution. Ended up just instructing users to save the file first if using IE. :( – JRS Jun 13 '15 at 23:50

1 Answers1

1

I faced same issue and removing ".vnd" from the below line started working for me.

Response.ContentType = "application/ms-excel"
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
arvind
  • 113
  • 6