To read an Excel file in C#, you can use the Microsoft.Office.Interop.Excel
library. Here's an example:
First, make sure you have added a reference to the Microsoft.Office.Interop.Excel
assembly.
main.cs1060 chars37 lines
In this example, we create an Excel.Application
object, then open the Excel file using the Workbooks.Open()
method. We retrieve the first worksheet using the Sheets
property and iterate over the cells in the used range of the worksheet using a foreach
loop.
Remember to replace "path_to_your_excel_file"
with the actual path to your Excel file.
Note that this approach requires the Excel application to be installed on the machine running the code. Alternatively, you can use third-party libraries like EPPlus
or NPOI
to read Excel files without the dependency on Excel itself.
gistlibby LogSnag