Read Excel File In C# WinForm

Introduction

Here, we are going to learn to read Excel files in C# WindowsForm applications. We are going to make a small single-form application which takes an Excel file from the user and shows the data in GridView of Windows Form.

Targeted Audiences

The targeted audience is people with basic knowledge of C#.

Explanation

Things to do,

  • Make a C# WinForm application.
  • Add a reference.
  • Create UI
  • Code

After creating a project, you have to add a reference named “Microsoft Excel xx.xx Object library” in your project.C#

Now, make a UI which contains a button and a single GridView. A button is used to open the Excel file and the GridView is used to show this data in Excel file.

So, here, our UI looks like the following.

C#

It contains a single button and a GridView in your application.

Now, code the click event of the button to browse computer files so that the user can select his Excel file and it starts showing in the GridView of the application. So, double-click on the button to open the click event the of the button.

Code

  1. using Microsoft.Office.Interop.Excel;  
  2. using System.Runtime.InteropServices;  

Code for the button click event.

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.       
  4.     string fname = "";  
  5.     OpenFileDialog fdlg = new OpenFileDialog();  
  6.     fdlg.Title = "Excel File Dialog";  
  7.     fdlg.InitialDirectory = @"c:\";  
  8.     fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";  
  9.     fdlg.FilterIndex = 2;  
  10.     fdlg.RestoreDirectory = true;  
  11.     if (fdlg.ShowDialog() == DialogResult.OK)  
  12.     {  
  13.         fname = fdlg.FileName;  
  14.     }  
  15.     
  16.   
  17.     Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();  
  18.     Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname);  
  19.     Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];  
  20.     Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;  
  21.   
  22.     int rowCount = xlRange.Rows.Count;  
  23.     int colCount = xlRange.Columns.Count;  
  24.   
  25.    // dt.Column = colCount;  
  26.     dataGridView1.ColumnCount = colCount;  
  27.     dataGridView1.RowCount = rowCount;  
  28.   
  29.     for (int i = 1; i <= rowCount; i++)  
  30.     {  
  31.         for (int j = 1; j <= colCount; j++)  
  32.         {  
  33.   
  34.   
  35.             //write the value to the Grid  
  36.   
  37.   
  38.             if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)  
  39.             {  
  40.                 dataGridView1.Rows[i - 1].Cells[j - 1].Value = xlRange.Cells[i, j].Value2.ToString();  
  41.             }  
  42.             // Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");  
  43.              
  44.             //add useful things here!     
  45.         }  
  46.     }  
  47.   
  48.     //cleanup  
  49.     GC.Collect();  
  50.     GC.WaitForPendingFinalizers();  
  51.   
  52.     //rule of thumb for releasing com objects:  
  53.     //  never use two dots, all COM objects must be referenced and released individually  
  54.     //  ex: [somthing].[something].[something] is bad  
  55.   
  56.     //release com objects to fully kill excel process from running in the background  
  57.     Marshal.ReleaseComObject(xlRange);  
  58.     Marshal.ReleaseComObject(xlWorksheet);  
  59.   
  60.     //close and release  
  61.     xlWorkbook.Close();  
  62.     Marshal.ReleaseComObject(xlWorkbook);  
  63.   
  64.     //quit and release  
  65.     xlApp.Quit();  
  66.     Marshal.ReleaseComObject(xlApp);  
  67.   
  68.   
  69.   
  70. }  

Explanation

When a user clicks the button, she/he can browse all the files on the computer. Also, you can add filters too so that only Excel files are opened. After clicking on the button, it will start reading Excel files and displaying result in GridView. And, after reading all the data, it will clean, release, and quit the object.

Let’s start our application and select an Excel file to see the output.

Output

C#


Recommended Free Ebook
Similar Articles