-1

I read an excel workbook using ClosedXML and add new sheets to it. But when I try to workBook.SaveAs(memoryStream), it throws an exception "Specified method is not supported.".

    var responseContent = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
    using var workBook = string.IsNullOrWhiteSpace(model.TemplatePath) ? new XLWorkbook() : new 
    XLWorkbook(responseContent);
    workBook.AddWorksheet("SheetName");
    using var stream = new MemoryStream();
    workBook.SaveAs(stream);
    stream.Position = 0;
    var content = stream.ToArray();
Alenros
  • 816
  • 7
  • 23
  • Just to make sure, the line that throws the error is the workBook.saveAs(stream)? – Alenros Dec 17 '20 at 15:45
  • Add the full stack trace, if you can reproduce it. – Francois Botha Dec 18 '20 at 14:25
  • How about the issue? Does the answer below resolved your question, If yes, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks. – Alenros Dec 22 '20 at 08:00

1 Answers1

1

Maybe the problem is with an empty responseContent. Running the following adaptation of the code that doesn't contain the responseContent works for me:

     using var workBook = new XLWorkbook();
     workBook.AddWorksheet("SheetName");
     using var stream = new MemoryStream();
     workBook.SaveAs(stream);
     stream.Position = 0;
     var content = stream.ToArray();
Alenros
  • 816
  • 7
  • 23