How to activate Excel cell in C#, VB.NET in .NET MVC FileFormat Libraries?
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files. Using this library, you can activate a single cell or a range of cells in an Excel worksheet.
Steps to set an active cell in an 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 a reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: Include the following namespace in the 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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to activate Excel cell in C#, VB.NET in .NET MVC FileFormat Libraries.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!