Creating Excel Files in C# Without Installing Microsoft Office

C

Microsoft Excel is a widely used spreadsheet program for creating, manipulating, and analyzing data. However, to work with Excel files in C#, traditionally, you would need to install Microsoft Office or rely on third-party libraries. In this article, we will explore an alternative approach to generate Excel files (.XLS and .XLSX) using C# without the need for Office installation. We will leverage the EPPlus library, a powerful open-source library for working with Excel files in .NET.

Prerequisites:

To follow along with the examples in this article, you will need:

  1. A basic understanding of C# programming.
  2. Visual Studio or any other C# development environment.
  3. The EPPlus library. You can obtain it via NuGet Package Manager or download it directly from the EPPlus GitHub repository.

Creating a New Excel File (.XLSX):

  1. Start by creating a new C# console application project in Visual Studio.
  2. Add a reference to the EPPlus library. Right-click on the project in Solution Explorer, select “Manage NuGet Packages,” search for “EPPlus,” and install it.
  3. Once the EPPlus library is installed, we can begin creating our Excel file. Add the following code to the `Main` method:

[code]
using OfficeOpenXml;

class Program
{
static void Main(string[] args)
{
// Create a new Excel package
using (var package = new ExcelPackage())
{
// Add a new worksheet to the Excel package
var worksheet = package.Workbook.Worksheets.Add(“Sheet1”);

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

// Save the Excel package to a file
package.SaveAs(new FileInfo(“sample.xlsx”));
}
}
}
[/code]

4. Build and run the application. You should see a new file named “sample.xlsx” generated in the project directory. Open it with Excel or any other compatible spreadsheet software to view the contents.

Creating a Legacy Excel File (.XLS):

To create a legacy Excel file in the .XLS format, we need to include an additional dependency. The EPPlus library itself supports the newer .XLSX format, but we can utilize the NPOI library, which is compatible with both formats.

1. Add a reference to the NPOI library. Install the “NPOI” NuGet package to the project.

2. Modify the previous code to generate a .XLS file instead:

[code]
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

class Program
{
static void Main(string[] args)
{
// Create a new Excel workbook
var workbook = new HSSFWorkbook();

// Add a new worksheet to the workbook
var worksheet = workbook.CreateSheet(“Sheet1”);

// Create a new row and set cell values
var row = worksheet.CreateRow(0);
row.CreateCell(0).SetCellValue(“Hello”);
row.CreateCell(1).SetCellValue(“World!”);

// Save the workbook to a file
using (var fileStream = new FileStream(“sample.xls”, FileMode.Create))
{
workbook.Write(fileStream);
}
}
}
[/code]

3. Build and run the application. This time, you will find a file named “sample.xls” in the project directory, which can be opened with legacy versions of Excel.

In this article, we explored how to create Excel files (.XLS and .XLSX) in C# without the need to install Microsoft Office. By utilizing the EPPlus library, we were able to generate Excel files programmatically, setting cell values and saving them to disk. For the newer .XLSX format, EPPlus provided the necessary functionality, while for the legacy .XLS format, we relied on the NPOI library. With these tools at your disposal, you can easily automate Excel file generation in your C# applications, without requiring Microsoft Office installation.

About the author

By Jamie

My Books