By default, after creating an Excel workbook, the worksheets will be named as sheet1, sheet2 and sheet3. In order to show readers what the workbook includes, people may rename worksheet according to data they import. The other situation we may need to rename is that people have renamed one worksheet. But they change data several days later, so they want to rename worksheet to suit for recent data. For example, I import customers contact information in the first sheet. So, I change the default name as ContactInfo.
In this post, I will show how to rename a worksheet by using C#. I prepare an Excel file which I want to rename. This file just has one worksheet.
The following steps show details to rename worksheet.
Note: I use Spire.XLS in this method. So I add its DLL as reference at the beginning.
- Load file from my computer.
- Select worksheet I want to rename and assign name string to worksheet.Name to rename.
- Save and Launch file
static void Main(string args)
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\work\My Documents\CommissionReport.xlsx", ExcelVersion.Version2010);
Worksheet worksheet = workbook.Worksheets;
worksheet.Name = "Commission Report";
//Save and Launch File
After running, you can get the following result:
Click Here to learn more about Spire.XLS
Click Here to download