How to read excel file in c# console application. how to read data from excel sheet 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.
| 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
| //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
| Workbook excelBook = excelApp.Workbooks.Open(@"E:\readExample.xlsx"); _Worksheet excelSheet = excelBook.Sheets[1]; Range excelRange = excelSheet.UsedRange; |
Step 5: Calculating rows and cols.
| int rowCount = excelRange.Rows.Count; int colCount = excelRange.Columns.Count; |
Step 6: Reading step by step cols and rows.
| 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.
| //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(); } } |
No comments:
Post a Comment