Search code examples
responsenpoi

Cannot use HTTP Response


I am using NPOI to save files to an .xls format.

I would like to save an empty file for now that a user has a dialog box and is prompted to save it locally.

The problem I have is I cannot set the Response fields, because

The name 'Response' does not exist in the current context.

This is the class I want to use:

https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse?view=netframework-4.8

        //Load all documents sorted by DocNumber and export them
        var documents = await _ctx.Db.Documents
            .AsNoTracking()
            .OrderByDescending(d => d.Number)
            .ToListAsync();

        var workbook = new HSSFWorkbook();
        var sheet = workbook.CreateSheet("Invoicing_Docs");


        using (var exportData = new MemoryStream())
        {
            workbook.Write(exportData);
            string saveAsFileName = string.Format("Invoicing_Docs-{0:d}.xls", DateTime.UtcNow).Replace("/", "-");

            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
            Response.Clear();
            Response.BinaryWrite(exportData.GetBuffer());
            Response.End();
        }

I have installed System.Web in the project.

When I ctrl + . on Response I have 3 choices:

  • Install package "IdentityMode",
  • Install package "Selenium.WebDriver"
  • Install package "Swashbuckle.AspNetCore.Swagger"

Also this is the article, I am following:

https://steemit.com/utopian-io/@haig/how-to-create-excel-spreadsheets-using-npoi

I don't need any of those. What am I doing wrong?


Solution

  • I found out what I need to do.

    I was not using the controller I was writing a service that is called in the controller.

    All I had to do is inject Response in the Method of the service.