How to Insert New Excel Worksheets using C#/.NET Core
Thanks to OpenXML formatting, programmatically building out Excel spreadsheets is straightforward and efficient.
We can create new Excel worksheets and insert them next to existing worksheets in our document without opening the file at any point. This gives us the flexibility to automate repetitive changes to Excel documents.
However, creating this type of workflow usually requires writing a bunch of new code, which can be challenging and time consuming. Thankfully, we can get around that by using a free API in our file processing application.
Using C#/.NET Core code examples provided further down the page, we can take advantage of a free API that simplifies the process of adding new worksheets to our Excel documents. We can structure our “add worksheet” request following the below JSON request model:
{
"InputFileBytes": "string",
"InputFileUrl": "string",
"WorksheetToInsert": {
"Path": "string",
"WorksheetName": "string"
}
}
Our request will return a temporary editing URL version of the document. This allows us to continue making edits to that document — such as adding additional worksheets in a chain of operations — if we wish. We can download the temporary editing URL using a secondary API call (this converts the URL and returns our edited Excel file bytes).
To authorize our API calls, we’ll just need a free Cloudmersive API key. This will allow us to make a limit of 800 API calls per month with zero commitments, and we can use that API key for both actions in our Excel editing chain.
To get started, let’s install the client SDK via NuGet. We can run the following command in our package manager console:
Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1
Next, let’s use the below examples to call our “insert worksheet” API. We can pass our input request through var input
, and we can replace the “YOUR_API_KEY”
placeholder text with our own API key string:
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 EditDocumentXlsxInsertWorksheetExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditDocumentApi();
var input = new InsertXlsxWorksheetRequest(); // InsertXlsxWorksheetRequest | Document input request
try
{
// Insert a new worksheet into an Excel XLSX spreadsheet
InsertXlsxWorksheetResponse result = apiInstance.EditDocumentXlsxInsertWorksheet(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditDocumentApi.EditDocumentXlsxInsertWorksheet: " + e.Message );
}
}
}
}
When our worksheet operation is complete, we can use the below code to call our “finish editing document” API, which converts the temporary editing URL into Excel file bytes:
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 );
}
}
}
}
That’s all there is to it! Now we can easily add new worksheets to our Excel documents with just a few lines of code.