How to Rename Excel Worksheets using C# .NET Core

Cloudmersive
2 min readMay 29, 2024

--

Naming Excel worksheets correctly is critical to ensure important spreadsheet data is easily identifiable in our documents.

If we need to rename Excel worksheets at scale, not to worry: there’s an API for that!

Using the below code, we can take advantage of a free API that makes it easy to rename worksheets in Excel documents. We’ll structure our “rename worksheet” API request like the following JSON request model:

{
"InputFileBytes": "string",
"InputFileUrl": "string",
"WorksheetToRename": {
"Path": "string",
"WorksheetName": "string"
},
"NewWorksheetName": "string"
}

And we’ll receive a temporary editing URL version of our file in response. We can use this temporary editing URL to chain several worksheet renaming actions together if we want, or we can finish our editing process by calling a specialized “finish editing” API (this converts the temporary editing URL back into Excel file bytes).

We can structure our API calls in a few quick steps. First, let’s install the client SDK via NuGet by running the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, let’s turn our attention to authorization. We’ll need a free Cloudmersive API key to authorize our requests (this will allow us to make up to 800 API calls per month with zero commitments).

We can now use the following C#/.NET Core examples to structure our “rename worksheet” API call. We can replace the “YOUR_API_KEY” placeholder text with our API key string, and we can pass our input request body into the var input variable:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class EditDocumentXlsxRenameWorksheetExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new EditDocumentApi();
var input = new RenameXlsxWorksheetRequest(); // RenameXlsxWorksheetRequest | Document input request

try
{
// Rename a specific worksheet in a Excel XLSX spreadsheet
RenameXlsxWorksheetResponse result = apiInstance.EditDocumentXlsxRenameWorksheet(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentXlsxRenameWorksheet: " + e.Message );
}
}
}
}

When we’re ready to download our Excel file bytes from the temporary editing URL we received, we can use the following code:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class EditDocumentFinishEditingExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new EditDocumentApi();
var reqConfig = new FinishEditingRequest(); // FinishEditingRequest | Cloudmersive Document URL to complete editing on

try
{
// Finish editing document, and download result from document editing
byte[] result = apiInstance.EditDocumentFinishEditing(reqConfig);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentFinishEditing: " + e.Message );
}
}
}
}

We can now write our Excel file bytes to a new file.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet