Download file with ClosedXML

前端 未结 7 2077
走了就别回头了
走了就别回头了 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:14

    The SaveAs() method supports stream, so to get the ClosedXml workbook as a stream I use:

    public Stream GetStream(XLWorkbook excelWorkbook)
    {
        Stream fs = new MemoryStream();
        excelWorkbook.SaveAs(fs);
        fs.Position = 0;
        return fs;
    }
    

    And then for downloading the file:

    string myName = Server.UrlEncode(ReportName + "_" + DateTime.Now.ToShortDateString() + ".xlsx");
    MemoryStream stream = GetStream(ExcelWorkbook);
    
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=" + myName);
    Response.ContentType = "application/vnd.ms-excel";
    Response.BinaryWrite(stream.ToArray());
    Response.End();
    
    0 讨论(0)
  • 2020-12-03 05:15
    public ActionResult SendFile()
    {
        // Create the workbook
        XLWorkbook workbook = new XLWorkbook();
        workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World");
    
        // Send the file
        MemoryStream excelStream = new MemoryStream();
        workbook.SaveAs(excelStream);
        excelStream.Position = 0;
        return File(excelStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "MyFileName.xlsx");
    }
    
    0 讨论(0)
  • 2020-12-03 05:16

    If you are using MVC just use the File() method as follows:

    using (XLWorkbook wb = new XLWorkbook())
     {
      //here you put your data in the WorkBook
      //then create a Memory Stream object
    using (MemoryStream stream = new MemoryStream())
      {
       //save the workbook as a MemoryStream
         wb.SaveAs(stream);
    
       //use the File method to return a File
       return File(stream.ToArray(), "filetype","fileName.extension");
       }
     }
    
    0 讨论(0)
  • 2020-12-03 05:17

    The download can be done somewhat simpler and shorter, so the complete action in your controller could look like this - the download part is just one line instead of seven to ten

    public ActionResult XLSX()
    {
        System.IO.Stream spreadsheetStream = new System.IO.MemoryStream();
        XLWorkbook workbook = new XLWorkbook();
        IXLWorksheet worksheet = workbook.Worksheets.Add("example");
        worksheet.Cell(1, 1).SetValue("example");
        workbook.SaveAs(spreadsheetStream);
        spreadsheetStream.Position = 0;
    
        return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "example.xlsx" };
    }
    
    0 讨论(0)
  • 2020-12-03 05:32

    Old thread, but I couldn't quite get the accepted solution to work right. Some more searching came up with this, which worked just great for me:

            // Create the workbook
            XLWorkbook workbook = new XLWorkbook();
            workbook.Worksheets.Add("Sample").Cell(1, 1).SetValue("Hello World");
    
            // Prepare the response
            HttpResponse httpResponse = Response;
            httpResponse.Clear();
            httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            httpResponse.AddHeader("content-disposition", "attachment;filename=\"HelloWorld.xlsx\"");
    
            // Flush the workbook to the Response.OutputStream
            using (MemoryStream memoryStream = new MemoryStream())
            {
                workbook.SaveAs(memoryStream);
                memoryStream.WriteTo(httpResponse.OutputStream);
                memoryStream.Close();
            }
    
            httpResponse.End();
    
    0 讨论(0)
  • 2020-12-03 05:35
    //Method for Export Excel using Closed Xml
    public void ExportDataWithClosedXml_Method(DataTable table, string tabName, string fileType)
    {
        var workbook = new XLWorkbook();
        var ws = workbook.Worksheets.Add(table, tabName);
        int row = 2 + table.Rows.Count;
        int col = table.Columns.Count;
        var redRow = ws.Row(1);
        //redRow.Style.Fill.BackgroundColor = XLColor.Red;
        redRow.InsertRowsAbove(1);
        ws.Cell(1, 1).Value = "Name of Report Type";
        ws.Cell(1, 1).Style.Font.Bold = true;
        ws.Table(0).ShowAutoFilter = false;
        //ws.Row(2).Style.Fill.BackgroundColor = XLColor.Red;
        ws.Range(2, 1, 2, col).Style.Fill.BackgroundColor = XLColor.Green;
        ws.Range(2, 1, 2, col).Style.Font.Bold = true;
        ws.Range(3, 1, row, col).Style.Font.Italic = true;
    
        HttpContext.Current.Response.Clear();
        using (MemoryStream memoryStream = new MemoryStream())
        {
            workbook.SaveAs(memoryStream);
            memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
            memoryStream.Close();
        }
        if (fileType == "xlsx")
        {
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=\"Samplefile.xlsx\"");
        }
        else
        {
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=\"Samplefile.xls\"");
        }
        HttpContext.Current.Response.End();
    }
    
    0 讨论(0)
提交回复
热议问题