4

I have an ASP.NET MVC app where a user can upload an xlsx file to be processed.

This creates a HttpPostedFileBase object in the upload that has a handy stream method, HttpPostedFileBase.InputStream.

I want to process the file using ClosedXML, but I don't know how to construct a XLWorkbook object from a stream. Other SO answers use a normal file like this:

 string fileName = "C:\\Folder1\\Prev.xlsx";
 var workbook = new XLWorkbook(fileName);

This question explores how to SaveAs as stream, but I want to create from a stream.

So how do you open a XLWorkbook from a stream?

Jess
  • 23,901
  • 21
  • 124
  • 145

1 Answers1

9

I was cruising around the ClosedXML Wiki, but did not find anything, so I went and looked at the source on github.

I found this method:

    /// <summary>
    ///   Opens an existing workbook from a stream.
    /// </summary>
    /// <param name = "stream">The stream to open.</param>
    public XLWorkbook(Stream stream):this(stream, XLEventTracking.Enabled)
    {

    }

Then you should be able to open my HttpPostedFileBase like this:

var workbook = new XLWorkbook(httpPostedFileBase.InputStream);
Jess
  • 23,901
  • 21
  • 124
  • 145