-2

Using the NPOI package, can an API receive an Excel file (xlsx), use that file in memory to process, then return a second Excel file as the response, all without saving either file to disk, or do the files need to be saved to disk before being used?

I'm trying to return from an API, a Task, and I am getting a NotImplementedException: System.NotImplementedException: The method or operation is not implemented. at NPOI.XSSF.UserModel.XSSFWorkbook.get_IsHidden()

My API takes an IFormFile that is an Excel workbook as the parameter, parses the data from it, performs some actions including adding a sheet and results from several API calls to that added sheet, and then I want to return the workbook. The workbook will have the original sheet, and the new "Results" sheet. This is for bulk-testing our API by a client so that they can look at batch results at once, rather than one at a time through our web page that was set up for testing.

I did find this page, but not being all that familiar with writing APIs, I'm not sure how to change this to work as returning a Task with it. NPOI export excel directly to frontend without saving it on server. So it seems possible, I'm just not sure how to get over that last hump.

The other examples I've found so far use disk, and if possible I would like to use the files in memory only, since there is no need for them to be kept beyond doing the processing that the API will do. If it is possible, can someone point me to a resource with example?

Rich Hopkins
  • 1,861
  • 15
  • 29
  • You shared the error you're getting, but you failed to share the exact code you're using. Please provide a [mcve] – mason Dec 13 '22 at 18:50
  • My code was changing by the minute, trying different things. I ended up figuring it out, below. – Rich Hopkins Dec 13 '22 at 20:07

1 Answers1

0

I ended up figuring it out. In the business logic, return the IWorkbook object to the controller, and then in the controller, write the memory stream to an array, and return that.

using (var memoryStream = new MemoryStream())
  {
    response.Write(memoryStream, false);
    return File(memoryStream.ToArray(), "application/vnd.ms-excel", filename);
  }
Rich Hopkins
  • 1,861
  • 15
  • 29
  • Are you sure you've used the correct MIME type here? You are generating a .xlsx file, right? See https://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc – mason Dec 13 '22 at 20:28
  • It looks like you're right. But somehow it works... Thanks. I'll try it with the updated mime type. – Rich Hopkins Dec 14 '22 at 20:42