How to read an Excel file using C#
The following program illustrates how to open an existing Excel spreadsheet in C# using .NET Framework COM interop capability. Also you can see how to find Named Ranges in Excel and get the range of occupied cells (Used area) in excel sheet.
To access the object model from Visual C# .NET, you have to add the Microsoft Excel 15.0 Object Library to you project.
Create a new project in your Visual Studio and add a Command Button to your C# Form.
How to use COM Interop to Create an Excel Spreadsheet
Form the following pictures you can find how to add Excel reference library in your project.
Select Add Reference dialogue from Project menu of your Visual Studio.
Select Microsoft Excel 15.0 Object Library of COM leftside menu and click OK button.
After import the reference library, we have to initialize the Excel application Object.
Next step is to open the Excel file and get the specified worksheet.
After get the selcted worksheet, next step is to specify the used range in worksheet
How to specify a range in Excel sheet?
If you want to select a specific cell in Excel sheet, you can code like this.
Reading Named Ranges in Excel
If you want to select multiple cell value from Excel sheet, you can code like this.
How to get the range of occupied cells in excel sheet
For reading entire content of an Excel file in C#, we have to know how many cells used in the Excel file. In order to find the used range we use "UsedRange" property of xlWorkSheet . A used range includes any cell that has ever been used. It will return the last cell of used area.
How to properly clean up Excel interop objects
Interop marshaling governs how data is passed in method arguments and return values between managed and unmanaged memory during calls. Most data types have common representations in both managed and unmanaged memory. The interop marshaler handles these types for you. Other types can be ambiguous or not represented at all in managed memory.
It is important to note that every reference to an Excel COM object had to be set to null when you have finished with it, including Cells, Sheets, everything.
The Marshal class is in the System.Runtime.InteropServices namespace, so you should import the following namespace.
Looking for a C# job ?
There are lot of opportunities from many reputed companies in the world. Chances are you will need to prove that you know how to work with .Net Programming Language. These C# Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of .Net Programming. Here's a comprehensive list of .Net Interview Questions, along with some of the best answers. These sample questions are framed by our experts team who trains for .Net training to give you an idea of type of questions which may be asked in interview.
Go to... C# Interview Questions
Open and Read an Excel Spreadsheet Programmatically
Copy and paste the following source code in your C# project file
When you execute this C# source code the program read all used cells from Excel file.