In this article I will explain with an example, how to read and import Excel file directly from Stream without saving in Folder (Directory) on Server’s Disk in ASP.Net using C# and VB.Net.
When the Excel file is uploaded, the Excel File data is available in the InputStream property of the FileUpload control.
Using ClosedXml, the Excel file data can be easily read from the InputStream property.
Download DocumentFormat.OpenXml and ClosedXml Libraries
You can download the libraries using the following download locations.
Note: The DLL files of both OpenXml and ClosedXml are present in the attached sample.
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and a GridView.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="ImportExcel" />
<hr />
<asp:GridView ID="GridView1" runat="server">
You will need to import the following namespaces.
using System.IO;
using System.Data;
using ClosedXML.Excel;
Imports System.IO
Imports System.Data
Imports ClosedXML.Excel
Read Excel file directly from Stream
When the Import Button is clicked, the Excel file is read into the ClosedXml WorkBook class object directly from the PostedFile.InputStream property of the FileUpload control without saving the Excel file in Folder (Directory) on Server’s Disk.
Then the instance of the first Sheet is determined and all the rows present in the Sheet are fetched.
Finally a loop is executed over the fetched rows and a DataTable is populated which is then bound to the GridView.
protected void ImportExcel(object sender, EventArgs e)
    //Open the Excel file using ClosedXML.
    using (XLWorkbook workBook = new XLWorkbook(FileUpload1.PostedFile.InputStream))
        //Read the first Sheet from Excel file.
        IXLWorksheet workSheet = workBook.Worksheet(1);
        //Create a new DataTable.
        DataTable dt = new DataTable();
        //Loop through the Worksheet rows.
        bool firstRow = true;
        foreach (IXLRow row in workSheet.Rows())
            //Use the first row to add columns to DataTable.
            if (firstRow)
                foreach (IXLCell cell in row.Cells())
                firstRow = false;
                //Add rows to DataTable.
                int i = 0;
                foreach (IXLCell cell in row.Cells())
                    dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
            GridView1.DataSource = dt;
Protected Sub ImportExcel(sender As Object, e As EventArgs)
    'Open the Excel file using ClosedXML.
    Using workBook As New XLWorkbook(FileUpload1.PostedFile.InputStream)
        'Read the first Sheet from Excel file.
        Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
        'Create a new DataTable.
        Dim dt As New DataTable()
        'Loop through the Worksheet rows.
        Dim firstRow As Boolean = True
        For Each row As IXLRow In workSheet.Rows()
            'Use the first row to add columns to DataTable.
            If firstRow Then
                For Each cell As IXLCell In row.Cells()
                firstRow = False
                'Add rows to DataTable.
                Dim i As Integer = 0
                For Each cell As IXLCell In row.Cells()
                    dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
                    i += 1
            End If
            GridView1.DataSource = dt
    End Using
End Sub
The Excel File
Read Excel file directly from Stream in ASP.Net
GridView displaying Excel data
Read Excel file directly from Stream in ASP.Net