2

I'm trying to export data from an asp.net core webapplication (Blazor) to an excel file. Excel file has to show up in the browser, to be downloaded by the user.

Per all the examples I use Response:

using System;
using System.IO;
using ClosedXML.Excel;
using System.Data;
using System.Web;
using Microsoft.AspNetCore.Http;

namespace Sxxx
{
    public class Exporter
    {
        public void Export()
        {
            using (IXLWorkbook wb = new XLWorkbook())
            {
                IXLWorksheet ws = wb.Worksheets.Add("Sample Sheet");

                ws.Cell(1, 1).Value = "Hello World!";

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
                       
        }
    }

However Response is not recognized. The missing assembly that VS 2019 shows is "Ubiety.Dns.Core", but that doesn't seem right.

In learn.microsoft.com HTTPresponse only shows up as part of Framework 4.8, so my question: can I use Response here or do I need something else for a Core 3.1 project?

Thanks in advance

ADBF
  • 93
  • 1
  • 15
  • What is `Response`? You do not instantiate it anywhere in your code as far as I can see. Do you try to create a `HttpResponseMessage` ? – Marco Jan 16 '21 at 22:12
  • 1
    You don't get a `Response` for free any place at all in a .NET project like that! You might get it in a class inheriting from `Controller` (not sure if that's true in Core). – Noah Jan 16 '21 at 22:26
  • 1
    Does this answer your question? [How can one generate and save a file client side using Blazor?](https://stackoverflow.com/questions/52683706/how-can-one-generate-and-save-a-file-client-side-using-blazor) – Marco Jan 16 '21 at 22:29
  • please provide a minimum reproducible example – Jonathan Alfaro Jan 17 '21 at 04:33
  • @Noah, that might be the problem, that in all examples the inherit from Controller, and that I do not have one in my .net core web application. – ADBF Jan 17 '21 at 08:08
  • @Marco, thank you for your suggestion. The answers state that it is not possible in Blazor, and that a js workaround is necessary. That not why I turned to Blazor :-) It is for a client side set up though, I'm doing a server side project, maybe that will make it different. I will follow up. – ADBF Jan 17 '21 at 08:16
  • 1
    @ADBF You can always create a controller, return a FileResult and solve it that way. – Marco Jan 17 '21 at 08:28

1 Answers1

2

I did create a Controller, as suggested by Marco and Noah, many thanks. I did learn a lot... Response inherits from ControllerBase. Most of the examples on the web are for .Net Framework 4.8, so it was a bit of a puzzle to do it for core 3.1 but here it is. Suggestions to improve are more then welcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using ClosedXML.Excel;


namespace Sxxx.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DownLoadController : ControllerBase
    {
        private readonly IWebHostEnvironment environment;
        public DownLoadController(IWebHostEnvironment environment)
        {
            this.environment = environment;
        }

        [HttpGet("[action]")]
        public async Task DownLoadFile()    
        {
            IXLWorkbook wb = new XLWorkbook();
            IXLWorksheet ws = wb.Worksheets.Add("Sample Sheet");
            ws.Cell(1, 1).Value = "Hello World!";
            MemoryStream ms = new MemoryStream();
            wb.SaveAs(ms);
            byte[] result = ms.ToArray();

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.Headers.Add("Content-Disposition", "attachment; filename=somefile.xlsx");
            
            await Response.Body.WriteAsync(result);
            await Response.StartAsync();
                  
        }
    }
}
ADBF
  • 93
  • 1
  • 15