Import Excel to Datagridview – C#

Import Excel – This project is intended to read excel using Microsoft.Office.interop.Excel namespace.There are a lot of ways to read an excel file in C# but for beginners like me this might be a help. Below is a simple way on how to read and import data in DataGridView.

Let’s Start:

  1. Create a new project by going to File->New->Project

    Import Excel

    New project

  2. From New Project window Name your project as Upload_Data_From_Excel.(You can also name it to your preference).Then click “OK” to proceed. See image below.

    Import Excel

    Name Project (Upload_Data_From_Excel)

  3. After you have successfully created your project. Add controls to your form.
  • Add button for upload click event.
  • Add OpenFileDialog for browsing files
  • DatagridView for viewing of data

 Design it to whatever you like.

Import Excel

Create design

  1. Rename your button. To rename click on the button you added to your form and navigate to its property located below your solution explorer. In my case I name it as btn_Upload.

    Import Excel

    Change button name

  2. Now navigate to your solution explorer and right click on Reference and choose Add Reference to add Microsoft.Office.Interop.Excel .

    Import Excel

    Add Reference

  3. From reference manager window expand Assemblies and click Extensions from the left pane and search for Micorosft.Office.Interope.Excel from the middle pane.

    Import Excel

    Add Microsoft Office Interop

  4. To see if reference was successfully added go to your project solution explorer and expand Reference then look for Microsoft.Office.Interop.Excel.

    Import Excel

    Check Reference if Added

  5. After we verify and successfully added Microsoft.Office.Interop.Excel. Open your form design and double click on btn_Upload button to add button click event.

    Import Excel

    Double Click on button upload

  6. Copy and paste the code below to your btn_Upload Click Event

  1. To test, run your application by pressing ctrl + f5 without debugging or f5 for debugging mode.

    Import Excel

    Run Project

  2. Click on upload button and select your excel file.
Import Excel

Open File

Note: Data on excel file should be in table format


Final output:

Excel File:

Import Excel

Excel File

Project Output:


Import Excel

Final Output

Full Code:

Thank you for reading!

Happy Coding!!


Post you may like:

SOLVED: An unexpected error occurred in the .NET Framework Data Provider for MySQL

If you are here I’m expecting that you encountered same problem as mine. I was transferring a DataSet file to ...
Read More

How to create custom 404 error handler in ASP.NET MVC

Custom 404 error handler - 404 Error refer to a not found resource/URL . Here’s how a default 404 Error ...
Read More

AngularJS CRUD Operation using ASP.NET MVC

I am a .NET Developer and I’m new in AngularJS. I started using AngularJS in about a week ago and ...
Read More
SignalR Chat App With ASP.NET

SignalR Chat Application with ASP.NET MVC

SignalR Chat App With ASP.NET - This article will show you an actual application using SignalR in ASP.NET MVC. The ...
Read More

Submit Forms using AgularJS with ASP.NET MVC

Ways on how to submit forms using AngularJS: I’m new to AngularJS and I was trying to create a CRUD application ...
Read More
Self Signed Certificate

How: Creating Self Signed Certificate

As defined in A self-signed certificate is a certificate that is signed by the person creating it rather than a trusted ...
Read More

AngularJS with Asp.Net MVC

What is Angular? AngularJS is a JavaScript framework that extends HTML attribute to help develop a dynamic Web application. AngularJS ...
Read More

Importing Excel with ASP.NET MVC

Importing excel, In my previous article, I demonstrated how to perform simple way of downloading data into excels. Now, this ...
Read More

Export Data to Excel with ASP.NET MVC

In creating a system there are times that we need to export data to excel, it could either be for ...
Read More

Easy Steps to Implement Partial View in Asp.Net MVC

One of the best ways to implement a dynamic table without loading the whole page is by using Jquery Ajax ...
Read More

Leave a Reply

Your email address will not be published. Required fields are marked *