Download file with ClosedXML

前端 未结 7 2078
走了就别回头了
走了就别回头了 2020-12-03 04:46

All

How can I download a file so the user sees that it is downloading (like with a stream?)

I am currently using ClosedXML, but if I use the SaveAs method, I

相关标签:
7条回答
  • 2020-12-03 05:37

    If using Asp.Net MVC, basically the same but slightly neater (well I think so:)).

    public ActionResult DownloadFile(XXXModel model)
    {
        using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
        {
            // create worksheets etc..
    
            // return 
            using (var stream = new MemoryStream())
            {
                workbook.SaveAs(stream);
                stream.Flush();
    
                return new FileContentResult(stream.ToArray(),
                       "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                       {
                           FileDownloadName = "XXXName.xlsx"
                       };
            }
        }
    
    0 讨论(0)
提交回复
热议问题