Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Import data from Excel in Asp.Net

Import data from Excel in Asp.Net

Date- Sep 20,2021 Updated Feb 2026 12786 Free Download Pay & Download
ExcelDataReader AspNet

Overview of ExcelDataReader

ExcelDataReader is a lightweight and fast library designed for reading Excel files in ASP.NET applications. It supports both the old Excel format (.xls) and the newer format (.xlsx), making it versatile for various use cases. By converting Excel data into a DataTable or DataSet, developers can easily manipulate and display the data in their applications.

This library is particularly useful for applications that require data import functionality, such as reporting tools, data analysis applications, or any system that needs to process bulk data from Excel spreadsheets. The ability to read Excel files programmatically can save time and reduce errors compared to manual data entry.

Prerequisites

Before you start importing data from Excel using ExcelDataReader, ensure you have the following:

  • A working ASP.NET project (either ASP.NET Core or ASP.NET MVC).
  • Visual Studio or any other compatible IDE.
  • Basic knowledge of C# and ASP.NET development.
  • Excel files (.xls or .xlsx) that you want to import.

Installing ExcelDataReader

To begin, you need to install the required NuGet packages for ExcelDataReader. Follow these steps:

  1. Open your project in Visual Studio.
  2. Right-click on the project in the Solution Explorer and select Manage NuGet Packages.
  3. Search for ExcelDataReader and install it.
  4. Also, install ExcelDataReader.DataSet to enable DataSet support.

After installation, you will have access to the necessary classes and methods to read Excel files.

Import data from Excel in AspNet

Reading Data from Excel

Once you have installed the required packages, you can start reading data from an Excel file. Below is a sample method that demonstrates how to read Excel data into a DataTable.

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

In this method:

  • We define the file path of the Excel file.
  • We open the file stream and create an appropriate reader based on the file extension.
  • Using ExcelDataSetConfiguration, we specify that the first row should be treated as column headers.
  • Finally, we extract the first table from the DataSet.

Ensure that you have placed a sample Excel file named Test.xlsx in the ExcelFile folder within your project root.

Handling Different Excel Formats

ExcelDataReader can handle both .xls and .xlsx formats. However, it’s important to note the differences in how these files are structured:

  • .xls files are binary files that can be read using CreateBinaryReader.
  • .xlsx files are XML-based files and require CreateOpenXmlReader.

In the provided code, we check the file extension and select the appropriate method to read the file. This ensures that our application can handle both formats seamlessly.

Edge Cases & Gotchas

When working with Excel files, there are several edge cases to be aware of:

  • Empty Rows: Ensure that your data does not contain empty rows that could lead to unexpected results in your DataTable.
  • Data Types: Excel does not enforce data types strictly, so numeric columns may contain text values. Handle potential exceptions when parsing data.
  • File Path Issues: Ensure that the file path is correct and that the application has permission to access the file.

Performance & Best Practices

To optimize performance when importing data from Excel:

  • Limit Data Size: If possible, limit the amount of data being imported to only what is necessary to reduce memory usage.
  • Use Async Operations: Consider using asynchronous file reading methods to avoid blocking the main thread, especially for large files.
  • Validate Data: Implement validation checks on the imported data to ensure it meets your application’s requirements before processing.

Conclusion

In this blog post, we explored how to import data from Excel files into an ASP.NET application using the ExcelDataReader library. We discussed installation steps, reading data, handling different Excel formats, and best practices for performance optimization.

Key Takeaways:

  • ExcelDataReader is a powerful library for reading Excel files in ASP.NET.
  • Always validate and sanitize imported data to prevent errors.
  • Handle different Excel formats appropriately to ensure compatibility.
  • Implement performance best practices to improve the efficiency of your data import functionality.
Import data from Excel in AspNet 2

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
Previous in ASP.NET Core
Real Time Chat using SignalR with .Net core and Vue.js
Next in ASP.NET Core
Sending FCM Mobile Notification in Asp.net for Android
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26009 views
  • Exception Handling Asp.Net Core 20766 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20244 views
  • How to implement Paypal in Asp.Net Core 19636 views
  • Task Scheduler in Asp.Net core 17543 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 | 1770
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
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor