C#:尝试保存导出的Excel文件,但保存为网页(.html)

以下是我用来导出excel文件的代码。

public void ExportReport() { DataTable dataTable = new DataTable(); dataTable = getDataTable(); HttpResponse response = HttpContext.Current.Response; GridView grdExportData = new GridView(); grdExportData.AllowPaging = false; grdExportData.DataSource = dataTable; grdExportData.DataBind(); //Clear the response and add the content types and headers to it. response.Clear(); response.ClearContent(); response.ContentType = "application/octet-stream"; DateTime currDate = DateTime.Now; response.AddHeader("Content-Disposition", "attachment; filename=report.xls") // Create a dynamic control, populate and render it GridView excel = new GridView(); excel.DataSource = dataTable; excel.DataBind(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(response.Output); htmlTextWriter.Write("<table><tr><td colspan='3'><b>Report Title</b></td><td colspan='1'></td><td colspan='1'><b>Date: &nbsp;</b></td><td colspan='1'>" + DateTime.Today.ToString("MM/dd/yyyy") + "</td></tr></table>"); excel.RenderControl(htmlTextWriter); response.Flush(); response.End(); } 

代码工作并导出报告,但是当我修改“ms excel”中的excel表格并尝试保存它保存为网页格式的文件时。 我想要默认格式为Excel。

提前致谢。

您需要更改ContentType

对于.xls文件,它是"application/vnd.ms-excel"