How to Create an Excel (.XLS and .XLSX) File in C# without Installing Microsoft Office

If you’re a C# developer looking to create Excel files without the need to install Microsoft Office, you’re in luck! There are several libraries available that can help you achieve this task easily and efficiently. In this article, we will explore two popular libraries, ExcelLibrary and EPPlus, and discuss how to use them to create both .XLS and .XLSX files.

ExcelLibrary

ExcelLibrary is a free and open-source library that provides a simple and easy-to-use solution for creating Excel files in C#. It supports the older .XLS format and offers a DataSetHelper that allows you to work with Excel data using DataSets and DataTables.

To get started with ExcelLibrary, you can download the library from its Google Code repository. Once you have the library added to your project, you can use the following code snippet to create an Excel file:

// Create the data set and table
DataSet ds = new DataSet("New_DataSet");
DataTable dt = new DataTable("New_DataTable");

// Set the locale for each
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

// Open a DB connection (in this example with OleDb)
OleDbConnection con = new OleDbConnection(dbConnectionString);
con.Open();

// Create a query and fill the data table with the data from the DB
string sql = "SELECT Whatever FROM MyDBTable;";
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter adptr = new OleDbDataAdapter();

adptr.SelectCommand = cmd;
adptr.Fill(dt);
con.Close();

// Add the table to the data set
ds.Tables.Add(dt);

// Create the Excel worksheet from the data set
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);

This code snippet demonstrates how to create an Excel file by retrieving data from a database and populating a DataTable. The DataSetHelper class provided by ExcelLibrary makes it easy to convert the DataTable into an Excel worksheet.

EPPlus

EPPlus is another popular library for creating Excel files in C#. Unlike ExcelLibrary, EPPlus supports both the older .XLS format and the newer .XLSX format used by Excel 2007 and later versions. It offers a wide range of features, including support for formatting, formulas, and charts.

To use EPPlus, you can download the library from its official website or install it via NuGet. Here’s an example of how to create an Excel file using EPPlus:

using OfficeOpenXml;

// Create a new Excel package
using (ExcelPackage package = new ExcelPackage())
{
    // Create a new worksheet
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");

    // Set cell values
    worksheet.Cells["A1"].Value = "Hello";
    worksheet.Cells["B1"].Value = "World";

    // Save the Excel file
    package.SaveAs(new FileInfo("MyExcelFile.xlsx"));
}

In this code snippet, we first create a new Excel package using the ExcelPackage class. We then create a new worksheet and set values for specific cells. Finally, we save the Excel file using the SaveAs method.

EPPlus provides a more modern and feature-rich solution for creating Excel files in C#. It is actively maintained and offers excellent performance even with large datasets.

If you need to work with the older .XLS format, ExcelLibrary is a suitable choice. It offers a simple API and supports working with DataSets and DataTables.

On the other hand, if you require support for the newer .XLSX format and additional features like formatting and formulas, EPPlus is the recommended library. It provides a comprehensive set of tools for creating professional Excel files.

Whether you choose ExcelLibrary or EPPlus, you can confidently create Excel files in C# without the need for Microsoft Office. These libraries offer reliable and efficient solutions for your Excel file generation needs.

Categories C#

Related Posts

C# Triple Double Quotes: What are they and how to use them?

In C# programming language, triple double quotes (“””) are a special syntax known as raw string literals. They provide a convenient way to work with strings that contain quotes or embedded language strings like JSON, XML, HTML, SQL, Regex, and others. Raw string literals eliminate the need for escaping characters, making it easier to write ...

Read more

Best Practices in Using a Lock in C#

What is a Lock? A lock in C# is implemented using the lock keyword, which ensures that only one thread can enter a specific section of code at a time. When a thread encounters a lock statement, it attempts to acquire a lock on the specified object. If the lock is already held by another ...

Read more

Usage of ‘&’ versus ‘&&’ in C#

‘&’ Operator The ‘&’ operator in C# is a bitwise AND operator. It operates at the bit level, meaning that it performs the AND operation on each corresponding pair of bits in the operands. This operator is commonly used when working with binary data or performing low-level bit manipulation. For example, consider the following code ...

Read more

How to Add a Badge to a C# WinForms Control

Have you ever wanted to add a badge to a C# WinForms control? Maybe you want to display a notification count on a button or indicate the status of a control. In this article, I will show you how to easily add a badge to a C# WinForms control using a static Adorner class. What ...

Read more

Leave a Comment