Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  1. Home
  2. Blogpost

Import data from Excel in Asp.Net

Date- Sep 20,2021

12732

Free Download Pay & Download
ExcelDataReader AspNet

ExcelDataReader

Excel Data Reader is a c# library which is used for reading data from excel in table format and we can fill the data in DataTable or DataSet. So , for installing ExcelDataReader. You have to go to

Project -> Manage Nugget Packages 

Now add two nugget packages. ExcelDataReader and ExcelDataReader.DataSet . You can check in the given image

So, after you have added these two nugget packages in your project. You have to go to your c# code and use this


private DataTable ReadData()
        {
            var filePath = HttpContext.Current.Server.MapPath("~/ExcelFile/Test.xlsx");
            var dataTable = new DataTable();
            using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                IExcelDataReader excelReader;
                if (Path.GetExtension(filePath).ToUpper() == ".XLS")
                {
                    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                else
                {
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }

                var result = excelReader.AsDataSet();
                excelReader = ExcelReaderFactory.CreateReader(stream);

                var conf = new ExcelDataSetConfiguration
                {
                    ConfigureDataTable = _ => new ExcelDataTableConfiguration
                    {
                        UseHeaderRow = true
                    }
                };
                var dataSet = excelReader.AsDataSet(conf);
                dataTable = dataSet.Tables[0];
                dataTable.Rows.RemoveAt(0);
            }
            return dataTable;
        }

So , first of all you must place a xlsx file  in your project. 

var filePath = HttpContext.Current.Server.MapPath("~/ExcelFile/Test.xlsx");

So , this was a sample file. Placed inside Excel folder inside project root.

 var conf = new ExcelDataSetConfiguration
 {
    ConfigureDataTable = _ => new ExcelDataTableConfiguration
    {
        UseHeaderRow = true
    }
 };
 var dataSet = excelReader.AsDataSet(conf);

This , will return you data as dataSet. UseHeaderRow will take first row as column headers. So, this is how you can ready your excel data and populate in datatable or dataset in Asp.Net.


S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Import Excel in Asp.net MVC using OLE DB
Jun 23, 2022
Get random number in asp.net C#
Dec 23, 2023
Integrate Stripe Payment Gateway In ASP.NET Core 8.0
Nov 23, 2023
Integrate Stripe Payment Gateway In ASP.NET Core 7.0
Jul 22, 2023

Comments

Contents

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 25915 views
  • Exception Handling Asp.Net Core 20697 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20172 views
  • How to implement Paypal in Asp.Net Core 19590 views
  • Task Scheduler in Asp.Net core 17480 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page