在ASP.NET MVC 5.1中保存Excel文件导致COMException – >它试图将它保存在我的文档中,而不是在项目资源中

我想在服务器端创buildexcel文件,然后将其发送到客户端(启用下载):

public ActionResult ExportToExcel() { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); xlApp.Visible = true; Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet ws = (Worksheet)wb.Worksheets[1]; // Select the Excel cells, in the range c1 to c7 in the worksheet. Range aRange = ws.get_Range("C1", "C7"); // Fill the cells in the C1 to C7 range of the worksheet with the number 6. Object[] args = new Object[1]; args[0] = 6; aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args); // Change the cells in the C1 to C7 range of the worksheet to the number 8. aRange.Value2 = 8; wb.SaveAs(@"~/Content/export.xlsx"); return RedirectToAction("Index"); } 

它会导致: InteropServices.COMException

“Microsoft Excel无法访问文件”C:\ Users \ s8359_000 \ Documents \〜\ Content \ 03AB3A00“。有以下几种可能的原因:\ n \ n•文件名或path不存在。\ n•文件为正在被另一个程序使用。\ n?您尝试保存的工作簿与当前打开的工作簿具有相同的名称。

问题:基本上我想保存在项目内(将在Azure中),或者只是将其发送到客户端(用户)而不保存。 我使用了MSDN的示例,但它一直保存在我的文件系统中。

编辑:

丹尼斯的解决scheme工作,但额外的元素已被添加到输出:

 public ActionResult ExportToExcel() { var datasource = new List<string> { "TEST1", "TEST2", "TEST3" }; // Your Data Source if (datasource != null) { var gv = new GridView(); gv.DataSource = datasource; gv.DataBind(); System.Web.HttpContext.Current.Response.ClearContent(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.AddHeader( "content-disposition", string.Format("attachment; filename={0}", "FileName.xls")); System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; System.Web.HttpContext.Current.Response.Charset = "UTF-8"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; System.Web.HttpContext.Current.Response.Charset = ""; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { gv.RenderControl(htw); System.Web.HttpContext.Current.Response.Output.Write(sw.ToString()); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); } } return new EmptyResult(); } return RedirectToAction("Index"); } 

在这里输入图像说明

这里是你如何可以将数据下载到客户端的Excel中。 在运行时创build一个GridView,并将你的数据源绑定到gridview。 你将不得不引用using System.Web.UI.WebControls; 和其他命名空间。

  public ActionResult ExcelDownload() { var datasource = GetDataSource (); // Your Data Source if (datasource != null) { var gv = new GridView(); gv.DataSource = datasource; gv.DataBind(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader( "content-disposition", string.Format("attachment; filename={0}", "FileName.xls")); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.Charset = ""; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { gv.RenderControl(htw); HttpContext.Current.Response.Output.Write(sw.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } } return new EmptyResult(); } return RedirectToAction("Index", "Home"); } 

您可以通过@Html.ActionLink调用ExcelDownload ActionResult方法

  @Html.ActionLink("Excel Download", "ExcelDownload", null, new { @title = "Click to download as an Excel report" }) 

或者替代scheme是考虑像EppPlus这样的工具