0

I'm using blazor and .net 5. I'm trying to do an excel export using ClosedXML. Here is the simple method I wrote:

public IActionResult CreateExcelSheet()
        {

            using (var wbook = new XLWorkbook())
            {
                var ws = wbook.Worksheets.Add("Sheet1");
                ws.Cell("A1").Value = "1";

                using (var stream = new MemoryStream())
                {
                    wbook.SaveAs(stream);
                    var content = stream.ToArray();
                    return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Opportunities.xlsx");
                }
            }
        }

The return file has a red underline saying Non-invocable member 'File' cannot be used like a method. I'm not really sure how to correct this... I'm also not sure if I'm doing this right with blazor and .net 5. Any help is greatly appreciated!

EkHolland
  • 1
  • 1
  • 1
  • 1
    It probably thinks you are using System.IO.File. Try fully qualifying it – Crowcoder Jun 18 '21 at 18:01
  • Your IActionResult needs to return a [ControllerBase.File](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.file?view=aspnetcore-5.0) object or equivalent. Look here: https://stackoverflow.com/a/40488246/421195 or here: https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-5.0&pivots=webassembly – paulsm4 Jun 18 '21 at 18:06
  • What kind of class is `CreateExcelSheet` contained in? – Kirk Woll Jun 18 '21 at 18:15

1 Answers1

0

I tried this and it works for me. Your URI is GET https:/api/sample/excel.

Sample: https://localhost:44370/api/Sample/excel

using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using ClosedXML.Excel;

namespace YourProject.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SampleController : ControllerBase
    {
        [HttpGet("excel")]
        [AllowAnonymous]
        public IActionResult CreateExcelSheet()
        {
            try
            {
                using (var wbook = new XLWorkbook())
                {
                    var ws = wbook.Worksheets.Add("Sheet1");
                    ws.Cell("A1").Value = "1";

                    using (var stream = new System.IO.MemoryStream())
                    {
                        wbook.SaveAs(stream);
                        var content = stream.ToArray();
                        return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Opportunities.xlsx");
                    }
                }
            }
            catch (Exception ex)
            {
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }
    }
}
rpocfemia
  • 36
  • 3
  • I suspect one reason it's working for you - and not for the OP - is that you've got `using Microsoft.AspNetCore.Mvc` - which has `FileResult` - and the OP doesn't. As Crowcoder said: "t probably thinks you are using System.IO.File. Try fully qualifying it." – paulsm4 Jun 20 '21 at 23:32
  • Thanks Rodel! I was missing the ControllerBase. I was using CommonBase. So I created a new controller just for the excel methods and it works now! Thanks for your help! – EkHolland Jun 21 '21 at 12:49