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.
To follow along with the examples in this article, you will need: 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. 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: 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. Published on May 31, 2023 Tags: ASP.NET MVC and Web API Tutorial
| excel
Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article
that you just finished reading.
No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner
or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in
and improve your skillset with any of the tutorials below.
Prerequisites:
Creating a New Excel File (.XLSX):
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"));
}
}
}
Creating a Legacy Excel File (.XLS):
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);
}
}
}
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.