Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! All,

I am trying to implement export excel functionality in my MVC web application. Can we avoid this message in the time of open file -

C#
The file format and extension of filename.xls don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?.


What I have tried:

Below is the code which I am using for the same.
C#
@model string

@{
    Layout = null;
    
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";
    Response.AddHeader("content-disposition", "attachment;filename=SurveyReport.xls");
    Response.Flush();
    //Response.End();
 }
Posted
Comments
Kornfeld Eliyahu Peter 28-Dec-16 3:04am    
And what the true file-format?
An HTML page? A CSV file? A XLSX file?
Kornfeld Eliyahu Peter 28-Dec-16 3:05am    
Also check the proper MIME type...
XLS => application/vnd.ms-excel
XLSX => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
rummer 28-Dec-16 4:28am    
I have checked with both MIME types but still coming warning....
ZurdoDev 28-Dec-16 8:19am    
1. Reply to comment so that user is notified.
2. You made it an xls so that is the warning you will get. That warning has been there for about 10 years now since the xls format is no longer common. Save it in the xlsx format and then no problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900