2017-06-03 150 views
0

问题:什么是ASP.NET Core1.1中以下代码的最后三行的替代方法和/或什么是解决方法?在这些最后三行VS2015抱怨HttpResponse does not contain a definition for OutputStream, Flush(), End()即时创建Excel文件并将其下载/保存到客户端

背景:在我ASP.NET Core 1.1应用我使用EPPlus.Core将数据导出在飞行到Excel,并把它下载/保存在客户端。作为首发,我试图模仿下面的例子(从here拍摄),但VS2015是不承认这段代码的last 3 lines

public void ExportListUsingEPPlus() 
{ 
    var data = new[]{ 
         new{ Name="Ram", Email="[email protected]", Phone="111-222-3333" }, 
         new{ Name="Shyam", Email="[email protected]", Phone="159-222-1596" }, 
         new{ Name="Mohan", Email="[email protected]", Phone="456-222-4569" }, 
         new{ Name="Sohan", Email="[email protected]", Phone="789-456-3333" }, 
         new{ Name="Karan", Email="[email protected]", Phone="111-222-1234" }, 
         new{ Name="Brij", Email="[email protected]", Phone="111-222-3333" } 
       }; 

    ExcelPackage excel = new ExcelPackage(); 
    var workSheet = excel.Workbook.Worksheets.Add("Sheet1"); 
    workSheet.Cells[1, 1].LoadFromCollection(data, true); 
    using (var memoryStream = new MemoryStream()) 
    { 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.Headers.Add("content-disposition", "attachment; filename=Contact.xlsx"); 
     excel.SaveAs(memoryStream); 
     memoryStream.WriteTo(Response.OutputStream); 
     Response.Flush(); 
     Response.End(); 
    } 
} 
+0

你只是不断地问不同的轮换(一旦它是csv,现在是一个excel文件)相同的问题。如果它可以接受'Stream',你总是可以做'新的ExcelPackage(HttpContext.Response.Body)'。只要流的写作是只向前它将工作(不收卷,因为你是在大多数情况下直接写入线/ TCP连接到用户的浏览器)和(非官方至少)EPPlus包确实有构造(见[这里]源(https://github.com/VahidN/EPPlus.Core/blob/master/src/EPPlus.Core/ExcelPackage.cs#L328)),你可能需要调用'保存()'方法 – Tseng

+0

或者只是首先在'MemoryStream'中执行'excel.SaveAs(HttpContext.Response.Body)'。为什么要浪费内存时,你可以直接写入响应流......你只是必须明白你的问题以前的解决方案,并在未来的应用它们,而不是要求一个复制和粘贴代码准备每一次 – Tseng

回答

1

您可以在控制器操作中返回FileStreamResult之一。

它返回与指定的contentType为内容类型和指定fileDownloadName作为建议的文件名指定的FILESTREAM文件。

public virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName) 

编辑:

样的行动方法 -

var data = new[]{ 
         new{ Name="Ram", Email="[email protected]", Phone="111-222-3333" }, 
         new{ Name="Shyam", Email="[email protected]", Phone="159-222-1596" }, 
         new{ Name="Mohan", Email="[email protected]", Phone="456-222-4569" }, 
         new{ Name="Sohan", Email="[email protected]", Phone="789-456-3333" }, 
         new{ Name="Karan", Email="[email protected]", Phone="111-222-1234" }, 
         new{ Name="Brij", Email="[email protected]", Phone="111-222-3333" } 
       }; 

      ExcelPackage excel = new ExcelPackage(); 
      var workSheet = excel.Workbook.Worksheets.Add("Sheet1"); 
      workSheet.Cells[1, 1].LoadFromCollection(data, true); 

      //var memoryStream = new MemoryStream(excel.GetAsByteArray()); 

      //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      //Response.Headers.Add("content-disposition", "attachment; filename=Contact.xlsx"); 
      //excel.SaveAs(memoryStream); 
      //memoryStream.WriteTo(Response.OutputStream); 
      //Response.Flush(); 
      //Response.End(); 

      return File(excel.GetAsByteArray(), "application/vnd.ms-excel", "Contact.xlsx"); 

生成的Excel screenshot-

enter image description here

+0

你的建议,它给出了错误:'不能访问一个封闭的流'。这可能是因为使用{...}语句,最后关闭了流。但是在我删除'使用{...}'后,它下载了一个没有数据的Excel文件。我用'return File(memoryStream,“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,“Contact.xlsx”)替换了最后3行;' – nam

+0

试试这个 - 'new MemoryStream(excel.GetAsByteArray()) ;' – Sanket

+0

我使用了'var memoryStream = new MemoryStream(excel.GetAsByteArray());'然后得到错误在行'excel.SaveAs(memoryStream);':**无法访问封闭的Stream **。 – nam

相关问题