How To Create An Excel (.xls And .xlsx) File In C# Without Installing Microsoft Office

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?

  1. EPPlus is a .NET library that allows you to create, read, and edit Excel files without the need for Microsoft Office.
  2. To use EPPlus, you will need to install the library into your project.
  3. Once installed, you can use the library to create a new Excel file, read an existing Excel file, or edit an existing Excel file.
  4. 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?

  1. To install EPPlus into a C# project, you will need to download the EPPlus library from the official website.
  2. The official download URL for EPPlus is https://www.nuget.org/packages/EPPlus/.
  3. Once downloaded, you can add the library to your project by right-clicking on the project in the Solution Explorer and selecting “Add Reference“.
  4. Then, select the EPPlus library from the list of available references and click “OK“.
  5. 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?

  1. 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();
    }
  2. 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"));
    }
  3. 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"));
    }

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.