我正在尝试修复其他人的代码,以从网站上的数据创建 Excel 文件。我们有数据,用户可以通过在网站上使用不同的复选框等来隐藏或显示其中的某些部分。一旦用户有了他们想要显示的内容,我们就可以选择将其导出到 excel 文件中。一切正常,直到文件被打开。打开文件时,报错:
您尝试打开的文件“filename.xls”的格式与文件扩展名指定的格式不同。在打开文件之前,请确认文件没有损坏并且来自受信任的来源。您现在要打开文件吗?
现在,如果单击“是”,文件仍会打开,并且数据的格式也很好。但是,我们希望此错误消失,因为它不是非常用户友好或看起来不干净。这是用于导出的代码:
// perform export
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
objResponse.Clear();
objResponse.Charset = "";
objResponse.Buffer = true;
objResponse.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", "filename.xls"));
objResponse.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter objStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHTMLWriter = new System.Web.UI.HtmlTextWriter(objStringWriter);
m_objDataTable.RenderControl(objHTMLWriter);
objResponse.Write(objStringWriter);
objResponse.End();
任何帮助将非常感激。