How to activate Excel cell in C#, VB.NET in WinForms FileFormat libraries ?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can activate single cell or a range of cells in Excel worksheet.
Steps to set active cell in Excel worksheet, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: Include the following namespace in Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
You can activate a cell in Excel worksheet through method call for the cell. Activate() method available under the IRange interface helps to activate the Excel cell.
Step 4: Include the following code snippet in main method of Program.cs file to activate an Excel cell.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Create a new Excel workbook IWorkbook workbook = application.Workbooks.Create(1); //Get the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; //Activate a cell and assign text in it worksheet.Range["E5"].Activate(); worksheet.Range["E5"].Text = "Active cell in this worksheet is E5"; //Autofit the column worksheet.Range["E5"].AutofitColumns(); //Save the Excel document workbook.SaveAs("Output.xlsx"); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Create a new Excel workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) 'Get the first worksheet in workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Activate a cell and assign text in it worksheet.Range("E5").Activate() worksheet.Range("E5").Text = "Active cell in this worksheet is E5" 'Autofit the column worksheet.Range("E5").AutofitColumns() 'Save the Excel document workbook.SaveAs("Output.xlsx") End Using
A complete working sample to activate an Excel cell can be downloaded from ActivateCell.zip.
By executing the program, you will get the output Excel file as follows.
Cell activated in Excel document
Syncfusion .NET Excel library also supports activating a range of cells. Use the following code snippet to activate a range of cells in Excel worksheet.
C#
//Activate cells and assign text worksheet.Range["E5:E10"].Activate(); worksheet.Range["E5:E10"].Text = "Active cells in this worksheet are from E5 to E10";
VB.NET
'Activate cells and assign text worksheet.Range("E5:E10").Activate() worksheet.Range("E5:E10").Text = "Active cells in this worksheet are from E5 to E10"
Range of cells activated in Excel document
Take a moment to peruse the documentation where you will find other options like accessing a cell or range, relative range, used range of worksheet, precedent and dependent cells, copy or move a range, skip blank cells, find and replace, data sorting, data filtering, hyperlinks and more with code examples.
See Also:
Merge Excel cells in C#, VB.NET
Protect certain cells in C#, VB.NET
Delete rows and columns in an Excel worksheet
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.