0

I have a C#/asp.net program that stores data in a datagrid and then uses HttpResponse to create an Excel file from it. This worked well until Microsoft made security changes. Here's some information on that change: http://www.networksteve.com/exchange/topic.php/XLS_file_will_not_open_in_Excel_2016,_only_gray_view/?TopicId=77375&Posts=3. The solutions such as turning off Excel security and uninstalling the patch the caused the problem are not acceptable for my client. The official Microsoft answer is "The best option is to move away from using HTML wrapped as .XLS. If you use native formats (e.g. XLS, XLSX, XLSB) which will open in protected view when untrusted, this will provide some level of protection from the documents being opened." http://answers.microsoft.com/en-us/office/forum/office_2010-excel/windows-installed-updates-and-now-downloaded-excel/069b0fdf-d085-4322-b298-5976d0efa9ce?rtAction=1468539460673

My question is, how do I do that? I tried using the Microsoft.Interop and found multiple people saying this is not a good solution. I could never get that working anyway; see that discussion here: How can I download an Excel file to the user's download folder?

See my code below. I'm hoping that I can change the header or content type and get this to work. I'm open to any coding suggestions. As I said, our client is not going to change security options on their and their clients' machines; this needs to be something I can do in the code so the client doesn't have to do anything. Also, my boss is very hesitant to give me time to learn 3rd party controls so those are probably out as well.

var grid = new DataGrid();
grid.HeaderStyle.Font.Bold = true;
grid.AlternatingItemStyle.BackColor = System.Drawing.Color.DarkGray;
grid.DataSource = GetTable(storerId, bSunday, bMonday, bTuesday, bWednesday, bThursday, bFriday, bSaturday, beginDate, endDate);
grid.DataBind();

response.Clear();

//response.AddHeader("content-disposition", string.Format("{0}{1}{2}{3}{4}", "attachment;filename=", storerId, "KpiExport", DateTime.Today.ToShortDateString(), ".xls"));
//response.AddHeader("Content-Disposition", string.Format("{0}{1}{2}{3}{4}", "attachment;filename=", storerId, "KpiExport", DateTime.Today.ToShortDateString(), ".xls"));
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "sample.xls"));

 response.Charset = "";

 // response.ContentType = "application/vnd.xls";
 response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

 var writer = new StringWriter();
 var htmlWriter = new HtmlTextWriter(writer);
 grid.RenderControl(htmlWriter);
 response.Write(writer.ToString());
 response.End();
Community
  • 1
  • 1
boilers222
  • 1,901
  • 7
  • 33
  • 71
  • Don't use interop, they don't recommend that either. Search for a library capable of generating XLSX files. I'm sure googling "C# excel library" would get you useful results. – mason Aug 03 '16 at 20:56
  • Possible duplicate of [Windows blocking .XLS files downloaded from the Internet where the contents are HTML](http://stackoverflow.com/questions/38643634/windows-blocking-xls-files-downloaded-from-the-internet-where-the-contents-are) – mason Aug 03 '16 at 21:00
  • Thanks for the reply. This is not a duplicate. I saw that post and am asking for a coding solution instead of just turning features off. I see the answer on that page is "Stop using HTML files masquerading as XLS" which I've seen on other sites. My question is, how do I do this? As I said in my original post, my boss is not going to allow a 3rd party library to be used. There must be someway to write a file that can be opened in Excel without triggering the new security features. – boilers222 Aug 03 '16 at 21:11
  • Read my answer there and it explains what you do – mason Aug 03 '16 at 21:12
  • Well you could create a CSV file – mason Aug 03 '16 at 21:12

0 Answers0