Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what i used to do for normal xls file format .

C#
Response.ClearHeaders();
              string attachment = "attachment; filename=EndOfYearReport.xls";
              Response.ClearContent();
              Response.AddHeader("content-disposition", attachment);
              //Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
              Response.ContentType = "application/ms-excel";
              Response.Write(strEndOfYear.ToString());
              Response.End();



but while opening the file i get the error:-

"The file you are trying to open, '[filename]', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"  (Yes | No | Help)


so tried to upgrade to new version xlsx ..



C#
Response.ClearHeaders();
               string attachment = "attachment; filename=EndOfYearReport.xlsx";
               Response.ClearContent();
               Response.AddHeader("content-disposition", attachment);
               Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
               //Response.ContentType = "application/ms-excel";
               Response.Write(strEndOfYear.ToString());
               Response.End();





but not able to open the file this time ..

error was coming

Error: excel cannot open the file .xlsx because the file format or file extension is not valid


Please suggest
Posted

1 solution

Just changing the file name or extension doesn't mean it will work: you have to change the data format as well.

Think about it: you can put Ferrari badges on a Ford Fiesta, but that isn't going to turn it into a sports car!

You need to look at how you produce strEndOfYear and generate xlsx compatible data, as well as using the right extension.
 
Share this answer
 
Comments
Torakami 9-Jul-14 3:13am    
thats true , but i am not creating any complicated data .. It just i am displying data in table

its simple <table> <tr> <td> thats it .. nothing more than that

my code is simlar to this type

strw.GetStringBuilder.Append("<table>")
strw.GetStringBuilder.Append("</table>")
strw.GetStringBuilder.Append("<table>")
strw.GetStringBuilder.Append("<tr>")
strw.GetStringBuilder.Append("<td colspan=9 align=left>")
strw.GetStringBuilder.Append("")
strw.GetStringBuilder.Append("TEST HTML Data")
strw.GetStringBuilder.Append("
")
strw.GetStringBuilder.Append("</td>")
strw.GetStringBuilder.Append("</tr></table>")
strw.GetStringBuilder.Append("<br>")
Torakami 9-Jul-14 3:42am    
Can any one please provide me the solution for this ..

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