You can create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office by using a library such as EPPlus. EPPlus is a .NET library that allows you to create, read, and edit Excel files without the need for Microsoft Office. In this article I will tell you how to use the library EPPlus to create an excel file in C#.
1. What is EPPlus?
- EPPlus is a .NET library that allows you to create, read, and edit Excel files without the need for Microsoft Office.
- To use EPPlus, you will need to install the library into your project.
- Once installed, you can use the library to create a new Excel file, read an existing Excel file, or edit an existing Excel file.
- You can also use the library to add formulas, charts, and other features to your Excel files.
2. How to install EPPlus Into C# Project?
- To install EPPlus into a C# project, you will need to download the EPPlus library from the official website.
- The official download URL for EPPlus is https://www.nuget.org/packages/EPPlus/.
- Once downloaded, you can add the library to your project by right-clicking on the project in the Solution Explorer and selecting “Add Reference“.
- Then, select the EPPlus library from the list of available references and click “OK“.
- This will add the library to your project and you will be able to use it to create, read, and edit Excel files.
3. How to use EPPlus to create, read, edit excel file example code?
- The following example code shows how to use EPPlus to create, read, and edit an Excel file.
// Create a new Excel file using (ExcelPackage package = new ExcelPackage()) { // Create a new worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Add data to the worksheet worksheet.Cells["A1"].Value = "Hello World!"; // Save the Excel file package.SaveAs(new FileInfo("example.xlsx")); } // Read an existing Excel file using (ExcelPackage package = new ExcelPackage(new FileInfo("example.xlsx"))) { // Get the first worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; // Read the data from the worksheet string data = worksheet.Cells["A1"].Value.ToString(); } // Edit an existing Excel file using (ExcelPackage package = new ExcelPackage(new FileInfo("example.xlsx"))) { // Get the first worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; // Edit the data in the worksheet worksheet.Cells["A1"].Value = "Goodbye World!"; // Save the changes package.Save(); }
- The following example code shows how to use EPPlus to add formulas to an Excel file.
// Create a new Excel file using (ExcelPackage package = new ExcelPackage()) { // Create a new worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Add data to the worksheet worksheet.Cells["A1"].Value = 10; worksheet.Cells["A2"].Value = 20; // Add a formula to the worksheet worksheet.Cells["A3"].Formula = "A1+A2"; // Save the Excel file package.SaveAs(new FileInfo("example.xlsx")); }
- The following example code shows how to use EPPlus to add charts to an Excel file.
// Create a new Excel file using (ExcelPackage package = new ExcelPackage()) { // Create a new worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Add data to the worksheet worksheet.Cells["A1"].Value = 10; worksheet.Cells["A2"].Value = 20; // Add a chart to the worksheet var chart = worksheet.Drawings.AddChart("Chart1", eChartType.ColumnClustered); chart.SetPosition(0, 0, 5, 0); chart.SetSize(800, 600); chart.Series.Add("A1:A2", "A1:A2"); // Save the Excel file package.SaveAs(new FileInfo("example.xlsx")); }