Basics of creating an xlsx with WorkBooks for .NET

Welcome to a new series! This time we will go over the latest addition to the Xceed collection: Workbooks for .NET. First, we will start with the basics of how to create a document and learn more advanced topics as we go in the next few articles

Learn more about Xeed Workbooks for .NET

Welcome to a new series! This time we will go over the latest addition to the Xceed collection: Workbooks for .NET

First, we will start with the basics of how to create a document and learn more advanced topics as we go in the next few articles.

What is this new Workbooks for .NET?

Xceed Workbooks for .NET is a fast, lightweight way to create or manipulate Microsoft Excel documents from your .NET applications, without requiring Excel or Office to be installed.

With its easy-to-use API, Xceed Workbooks for .NET lets your application create or modify Microsoft Exccel .xlsx documents and gives you complete control over the content of these documents. It allows you to modify the content of Cells, resize Columns and Rows, create formatted Tables, and more.

It can also be a great reporting tool, for example by creating company reports, and then using them as a template that you programmatically customize before sending each report out.

Xceed Workbooks for .NET can be used to programmatically create invoices, add data to documents, perform formula calculations, and more.

Getting started

Let’s create our first project!

First step, make sure you remember adding the using statement:

using Xceed.Workbooks.NET;

If you are using .NET Core, .NET 5 and beyond, you will need to manually activate the product, but don’t worry, this is a very simple process. You will want to make sure you have your license key on hand for this.

If you are using a console application, simply add the following statement in the main module:

Xceed.Workbooks.NET.Licenser.LicenseKey = "Your-License-Key";

In a Winforms or WPF application, you need to set it before the InitializeComponent(); statement, so it will look like this:

Xceed.Workbooks.NET.Licenser.LicenseKey = "Your-License-Key";
InitializeComponent();

Next, let’s look at how to simply create a blank document. All we need is to specify the filename (or file path and name), then save the document.

// Create a new document as a file
using( var document = Workbook.Create( "NewDocument.xlsx" ) )
{
	// Save the workbook to the file.
	document.Save();
}

Another option is to save the document to a stream:

// Create a new document as a stream
var stream = new MemoryStream();
using( var document = Workbook.Create( stream ) )
{
	// Save the workbook in the stream.
	document.Save();
}

We can also open an existing document instead of creating a new one (by using Load instead of Create), and we can choose to save to a new document instead of overwriting the existing one (by using SaveAs instead of Save). The document to open can be either from a file or a stream, and the new copy can also be saved to a new file or to a stream.

// Open an existing document from a file
using( var document = Workbook.Load( "ExistingDocument.xlsx" ) )
{
	// Save this document to disk to a new file.
	document.SaveAs( "NewCopy.docx" );
	// Save this document to a stream instead
	var stream = new MemoryStream();
	document.SaveAs( stream );
}

// Open an existing document from a stream
using( var document = Workbook.Load( existingStream ) )
{
	// Save this document to disk to a new file.
	document.SaveAs( "NewCopy.docx" );
	// Save this document to a stream instead
	var stream = new MemoryStream();
	document.SaveAs( stream );
}

Notes:

  • The Create, Load and SaveAs methods all expect either a String or Stream as parameter.

Adding and fetching Worksheets

A blank document is not very useful, but before we look into adding content to a Worksheet, let’s look at how we can add Worksheets or get a specific one from our document.

Note:

  • A Workbook will always have a Worksheet by default, whether it is created or loaded.

Adding a new Worksheet is done by calling the Add function on the Worksheets collection of the document; the name of the worksheet can also be specified at this time.

// Insert a new worksheet.
document.Worksheets.Add();

// Insert a new worksheet named "Tests"
document.Worksheets.Add("Tests");

To fetch a specific Worksheet in a document, we use the Worksheets collection once again, and specify either the 0-based index or the name of the sheet we want.

// Get the 2nd worksheet
var worksheetA = document.Worksheets[ 1 ];

// Get the worksheet named "Tests"
var worksheetB = document.Worksheets[ "Tests" ];

We can also check if a specific worksheet exists first by using the Contains function on the Worksheets collection.

// Verify if a worksheet named "Tests" exists in the document
if ( document.Worksheets.Contains( "Tests" ) )
{
	Debug.WriteLine( "Worksheet Found" );
}

There you have it! Your first steps in creating a Workbooks document (xlsx) using C#. The next step will be to add and modify its content. We will start covering this in our next article. See you then!

For more information, please refer to the la documentation.