using OfficeOpenXml; ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // set license context var excelFile = new FileInfo(@"C:\Data\myExcelFile.xlsx"); using (var package = new ExcelPackage(excelFile)) { ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"]; // read cell value var cellValue = worksheet.Cells["A1"].Value; // read range of data var dataRange = worksheet.Cells["A1:C3"]; var data = dataRange.Value; }
using OfficeOpenXml; ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // set license context var excelFile = new FileInfo(@"C:\Data\myExcelFile.xlsx"); using (var package = new ExcelPackage(excelFile)) { ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"]; // write cell value worksheet.Cells["A1"].Value = "Hello, world!"; // write range of data var data = new object[,] { { "Name", "Age" }, { "John", 30 }, { "Jane", 25 } }; worksheet.Cells["A2:B3"].Value = data; package.Save(); // save changes }In this example, we use the OfficeOpenXml package to write data to the "Sheet1" worksheet in an Excel file. We can write a single cell value or a range of data. The cell or range is identified using the Cells property and the corresponding cell address. Once we have made our changes, we save the file using the Save method. The package library used in these examples is OfficeOpenXml, which can be installed using the NuGet Package Manager in Visual Studio.