1

I am trying to export into excel with xlsx format but getting this error when trying to open the file. Excel cannot open the file 'CertificationMetricsReport.xlsx' because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file.

If I will provide the file name CertificationReport.xls then I am able to open it.

Here is the code snippet

 public void RenderedReportContent(byte[] fileContent)
    {
        try
        {
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("Content-Length", fileContent.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename="CertificationReport.xlsx");
            Response.BinaryWrite(fileContent);
            Response.End();
        }
        catch (Exception ex)
        {
            if (!(ex is System.Threading.ThreadAbortException))
            {
                //Other error handling code here
            }
        }
    }

When I am trying to download CertificationMetricsReport.xls it works fine.

Please help me to download the xlsx file.

1 Answers1

2

maybe you can try to change the contenttype

to contenttype = "application/vnd.ms-excel"; or application/octect-stream

                    Response.Clear();
                    Response.Buffer = True;
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.xslx"
                    Response.BinaryWrite(YourFile);
                    Response.End();

also you can try with response.flush

at the end try it in others navigators like IE, Chrome, Firefox...

EDIT: http://social.msdn.microsoft.com/forums/en-US/3d539969-51c4-42bc-8289-6d70d8db7aff/prompt-while-downloading-a-file-for-open-or-save EDIT2: How to send file in HttpResponse?

Community
  • 1
  • 1
Jnavero
  • 353
  • 6
  • 13
  • Thanks for your response. I tried both the "application/vnd.ms-excel" and "application/octet-stream" but it gives error when I tries to open the excel file. –  Jul 31 '14 at 17:40
  • sorry... xDDD you try two things, 1º. rename your xslx to .zip and you try to open it. maybe your file isn't xlsx. Other thing is open it with notepad. You can see the real format (or HTML if the response not work correctly) – Jnavero Jul 31 '14 at 17:47