In this article, you will learn how to create Excel XLS/XLSX files in C# without installing MS Office.

Create Excel File in C# API

Spreadsheets nowadays have become an essential part of keeping, organizing and analyzing the data. Spreadsheets, such as MS Excel, are capable of doing calculations and sorting, generating graphs or charts, creating budgets, and performing many other accounting or data analysis tasks. Since automated solutions are more in business these days, the trend of creating and manipulating Excel documents (XLS/XLSX) has emerged and growing at a huge pace. This use case has raised the need for having an Excel automation solution.

In accordance with the above-mentioned scenario, this comprehensive article aims to show you some basic features for Excel automation in C# .NET applications.

C# API to Create Excel Files - Free Download

In order to work with Excel documents, we will use Aspose.Cells for .NET which is a powerful API to create, read, and manipulate spreadsheet documents including XLS and XLSX files. You can either download or install the API using one of the following ways:

Using NuGet Package Manager

C# Excel Automation Library

Using the Package Manager Console

PM> Install-Package Aspose.Cells

Create an Excel XLS or XLSX in C#

An Excel file is also known as a Workbook which is composed of single or multiple worksheets containing the rows and columns to hold the data. Thus, a workbook acts as the container of the worksheets in an Excel file. So in order to create an Excel file, you will first create a workbook and then the worksheets within that workbook. The following are the steps to create an Excel file using Aspose.Cells for .NET.

  • Create an instance of Workbook class.
  • Access the first worksheet (created by default) of the workbook.
  • Access the desired cell(s) of the worksheet and put the value in the cell(s).
  • Save the workbook as an XLS or XLSX file.

The following code sample shows how to create an Excel XLSX file using C#.

Output

Create Excel Files in C#

Write Data to an Excel XLSX File in C#

In case you want to edit and write data to an existing Excel file, you can also do it in a similar fashion. Simply load the source Excel spreadsheet document using the Workbook object and access the desired worksheets and cells. The following are the steps to edit an existing Excel file.

  • Open Excel file in a FileStream object.
  • Create an instance of Workbook and initialize it with the FileStream object.
  • Access the worksheets and cells using the Worksheet and Cell classes respectively.
  • Save the workbook as an Excel .xlsx file.

The following code sample shows how to edit and write data to an existing Excel XLSX file in C#.

C# Create Excel file with Charts or Graphs

Excel spreadsheets provide a fine way of analyzing or presenting the data visually using the graphs and charts. Aspose.Cells for .NET provides a complete set of classes to create and manipulate a variety of charts in Excel spreadsheets where each class is used to perform some specific tasks.

In order to create the charts in an Excel file, you’ll have to follow the following steps:

  • Add some data (to be used as a data source) to the worksheet by accessing its cells.
  • Add a new chart object to the worksheet using Worksheet.Charts collection by passing the type of chart using the ChartType enumeration.
  • Get the newly created chart from the collection in a Chart object.
  • Specify the cells’ range to provide the data source to the chart object.
  • Save the workbook as an Excel .xlsx file.

The following code sample shows how to create an Excel XLSX file and add charts in C#.

Output

Create chart in Excel in C#

Learn more about creating charts in Excel worksheets using C#.

Create Excel XLSX File with Table in C#

You can also create a table from the range of cells in an Excel worksheet and add a row for the total (sum, count, etc.) in the table. The following are the steps to create a an Excel (XLSX) file and add table in C#:

  • Load an Excel workbook or create a new one using Workbook class.
  • Add data to the cells of the worksheet.
  • Add a new ListObject to the worksheet.
  • Set ListObject.ShowTotals property to true.
  • Calculate the total and save the workbook as an Excel .xlsx file.

The following code sample shows how to create a table in Excel file in C#.

Output

Create table in Excel Worksheet in C#

Learn more about working with tables in Excel worksheets using C#.

C# .NET API to Create Excel Files - Get a Free License

You can use Aspose.Cells for .NET without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to create Excel files from scratch using C#. Furthermore, you have seen how to insert data in sheets, generate charts, and insert tables in Excel files. You may have a look at the documentation of Aspose.Cells for .NET to learn the advanced features for manipulation of Excel files in C#.

Related Article

Create and Sort Pivot Tables in Excel Worksheets Create Excel Files in Node.js Applications Disable Compatibility Checker in Excel Files using C#
Disable Excel Compatibility Checker in Java Disable Excel Compatibility Checker in Python Read Excel Files using C#
Read Excel Files using Java Read Excel Files using Python