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:

[pt_view id=”2384ae03it”]

Leave a Reply

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