How to read excel file in c# console application. how to read data from excel sheet using c#.
You may also like
- Reading Excel file in C# Windows Form Application
- Create Excel File Using C# Console Application
- Export DataGridView Data to Excel in C#
- Set Background Color in Excel Cell Using C#
This example created for to read an excel file with C# Console Application.
Using the Excel namespace in C#, we can load or open an Excel file and read the cell sheets.
Berfore the creating codes, here is the examples screenshot
C# read Excel file
Step 1: We have to references Excel namespace to the project.
Step 2: Given this the using alias of Excel.
1 2 3 |
using Microsoft.Office.Interop.Excel; |
Note: if you create windows form application you have to add “using” like this. The same name is used for the form application.
Because you will see in following codes Application class for creating new Excel App. Same name is used for Windows Form Application too.
Form App fixes
using ExcelApp = Microsoft.Office.Interop.Excel;
….
ExcelApp.Application excelApp = new ExcelApp.Application();
……
ExcelApp.Workbook excelBook = excelApp.Workbooks.Open(@”E:\readExample.xlsx”);
ExcelApp._Worksheet excelSheet = excelBook.Sheets[1];
ExcelApp.Range excelRange = excelSheet.UsedRange;
Step 3: Creating COM object referances and checking Excel program is exists
1 2 3 4 5 6 7 8 9 10 11 |
//Create COM Objects. Application excelApp = new Application(); if (excelApp == null) { Console.WriteLine("Excel is not installed!!"); return; } |
Step 4: Creating book, pages and range variables
1 2 3 4 5 |
Workbook excelBook = excelApp.Workbooks.Open(@"E:\readExample.xlsx"); _Worksheet excelSheet = excelBook.Sheets[1]; Range excelRange = excelSheet.UsedRange; |
Step 5: Calculating rows and cols.
1 2 3 4 |
int rowCount = excelRange.Rows.Count; int colCount = excelRange.Columns.Count; |
Step 6: Reading step by step cols and rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
for (int i = 1; i <= rows; i++) { //create new line Console.Write("\r\n"); for (int j = 1; j <= cols; j++) { //write the console if (excelRange.Cells[i, j] != null && excelRange.Cells[i, j].Value2 != null) Console.Write(excelRange.Cells[i, j].Value2.ToString() + "\t"); } } |
Step 7: After reading, relase the excel project.
1 2 3 4 5 6 |
//after reading, relaase the excel project excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); Console.ReadLine(); |
Here are the all codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
//dont forget -- using Microsoft.Office.Interop.Excel; class Program { static void Main(string[] args) { //Create COM Objects. Application excelApp = new Application(); if (excelApp == null) { Console.WriteLine("Excel is not installed!!"); return; } Workbook excelBook = excelApp.Workbooks.Open(@"E:\readExample.xlsx"); _Worksheet excelSheet = excelBook.Sheets[1]; Range excelRange = excelSheet.UsedRange; int rows = excelRange.Rows.Count; int cols = excelRange.Columns.Count; for (int i = 1; i <= rows; i++) { //create new line Console.Write("\r\n"); for (int j = 1; j <= cols; j++) { //write the console if (excelRange.Cells[i, j] != null && excelRange.Cells[i, j].Value2 != null) Console.Write(excelRange.Cells[i, j].Value2.ToString() + "\t"); } } //after reading, relaase the excel project excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); Console.ReadLine(); } } |
While reading excel file like this we found that the program execution is very slow…and if we try to print those values in the console..we can clearly observe that..word by word…..
I am the only who is getting this much slow speed or its common with all? Plz confirm.
[…] http://www.csharp-console-examples.com […]
Hi while executing the above code,
I am getting the below exception while writing contents to console.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ”System.__ComObject’ does not contain a definition for ‘Value2”
Please help me to fix the same
Is it possible to store the path in config file rather than mentioning the path in the actual source code??
Path is a string so you can store it in config file