Articles in this section
Category / Section

How to activate Excel cell in C#, VB.NET in .NET WebForms?

5 mins read

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

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

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

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

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 rangerelative rangeused range of worksheetprecedent and dependent cellscopy or move a rangeskip blank cellsfind and replacedata sortingdata filteringhyperlinks and more with code examples.

See Also:

Merge Excel cells in C#, VB.NET

Protect certain cells in C#, VB.NET

Format comments in a cell

Set link break inside a cell

Delete rows and columns in an Excel worksheet

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

Note:

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.


 

Conclusion

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied