Read excel file from a stream

后端 未结 5 1465
粉色の甜心
粉色の甜心 2020-11-30 12:43

I need a way to read a Excel file from a stream. It doesn\'t seem to work with the ADO.NET way of doing things.

The scenario is that a user uploads a file through a

5条回答
  •  情歌与酒
    2020-11-30 13:12

    I use ClosedXML nuget package to read excel content from stream. It has a constructor overload in XLWorkbook class which takes stream pointing to an excel file (aka workbook).

    imported namespace at the top of your code file:

    using ClosedXML.Excel;
    

    Source code:

    var stream = /*obtain the stream from your source*/;
    if (stream.Length != 0)
    {
        //handle the stream here
        using (XLWorkbook excelWorkbook = new XLWorkbook(stream))
        {
            var name = excelWorkbook.Worksheet(1).Name;
            //do more things whatever you like as you now have a handle to the entire workbook.
            var firstRow = excelWorkbook.Worksheet(1).Row(1);
        }
    }
    

提交回复
热议问题