C# – Unable to open exported xlsx file

cexcel

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.

Best Answer

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?