title: Excel doesn't want to open XLSX files downloaded through ASP output
body: <p>I use ASP MVC3 framework, created an Excel file and outputted it using FileResult action with content type "<strong>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</strong>".</p>
  
  <p>When attempting to open it Excel just says "<strong>file is corrupt and cannot be opened</strong>."</p>
  
  <p>When I open the source generated Excel file that was used to send the output it works without any problems. I also run file comparison on the bytes for both copies and the files are identical. I tried to email the corrupt file to myself and the attachment opens fine.</p>
  
  <p>This leads me to believe it's a problem with headers or some sort of Excel/Windows security config.</p>
  
  <p>If it is the latter, then I need a solution that won't make clients change their security settings.</p>
  
  <p><strong>EDIT - Found the setting:</strong></p>
  
  <p>I've found what setting causes this - "<strong>Enable protected view from files originated from the internet</strong>" in Excel's <strong>Trust Center / Protected View</strong> settings.</p>
  
  <p>So I guess the question is - <strong>Is there a way for the file to appear trusted?</strong></p>
  
  <p><strong>Here are the response headers:</strong></p>
  
  <ul>
  <li>Cache-Control:private </li>
  <li>Content-Disposition:attachment;</li>
  <li>filename="Report - Monday, March 19, 2012.xlsx" Content-Length:20569</li>
  <li>Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</li>
  </ul>
  
  <p><strong>The action method that makes the output:</strong></p>
  
  <pre><code>[HttpPost]
  public virtual FileResult Export()
      {   
          try
          {
              ...
              string newFilePath = createNewFile(...);
              string downloadedFileName = "Report - " + DateTime.Now.ToString("D") + ".xlsx";
              return File(newFilePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", downloadedFileName);
  
          }
          catch (Exception ex)
          {
              ...
          }
      }
  </code></pre>
  
  <p><strong>How I create the Excel file:</strong></p>
  
  <p>I have a template XLSX file witch column names and some pivot charts in other sheets. From C# I create a copy of this template and then call SQL Server which outputs data into 1st sheet using OLEDB connector:</p>
  
  <pre><code>set @SQL='insert into OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;Database=' +     @PreparedXLSXFilePath + ';'', ''SELECT * FROM [Data$]'')  ...
  </code></pre>
  
  <p>Thanks in advance for any help.</p>