您尝试打开'file.xls'的文件格式与文件扩展名指定的格式不同

我想导出一个包含一个包含html的string的excel。 它给出错误信息:

The file you are trying to open 'ExportedHTML.xls' is in different format than specified by the file extension. Verify that the file is not corrupted and is from trusted source before opening the file . Do you want to open the file ? 

在这个错误信息上点击“是”,打开Excel。

我试图做两种方法,每个都给出相同的错误:

 First : Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls"); Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.ms-excel"; this.EnableViewState = false; Response.Write(sb.ToString()); Response.End(); Second: excelexport obj = new excelexport(); obj.downloadFile(htmlContent.ToString(), "File.xls"); 

如何解决?

xls是excel到2007年的地方,它被改为xlsx(基于xml格式)或xlsm,如果它有macros。 找出你正在创build什么types的Excel文件,并确保正确地排列扩展名。 另外对于2007年以后的excel文件,你想要的内容types是:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet

如果你能把所有这三样东西排列起来,它应该使信息消失。

可能会有一些帮助http://www.whatisexcel.net/excel-file-extensions.html