Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you open a ClosedXML Workbook from a stream?

Tags:

c#

closedxml

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?

like image 932
Jess Avatar asked Sep 16 '25 19:09

Jess


1 Answers

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);
like image 81
Jess Avatar answered Sep 18 '25 10:09

Jess