using Microsoft.Office.Interop.Excel; // Open the Excel file Application excel = new Application(); Workbook wb = excel.Workbooks.Open("C:\\example.xlsx"); // Get the contents of cell A1 in the first worksheet Worksheet ws = wb.Worksheets[1]; Range cell = ws.Cells[1, 1]; string value = cell.Value2.ToString(); // Close the file and release resources wb.Close(); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
using ClosedXML.Excel; // Open the Excel file var workbook = new XLWorkbook("C:\\example.xlsx"); // Get the contents of cell A1 in the first worksheet var worksheet = workbook.Worksheet(1); var cell = worksheet.Cell("A1"); string value = cell.GetString(); // Close the file and release resources workbook.Dispose();This example uses the ClosedXML package library to open an Excel file and retrieve the contents of cell A1 in the first worksheet. The value is stored in a string variable. ClosedXML is a popular package for working with Excel files in C#.