How to Create an Excel File in C# using IronXL

In this tutorial, learn to create Excel files in C# using IronXL, a library that supports the latest .NET framework versions. Start by installing IronXL via NuGet Package Manager. Import IronXL into your program file, allowing access to its features. Create a workbook using Workbook.Create, with XLSX as the default format. Add sheets and set cell values or formulas programmatically. Customize cell styling, including borders and background colors. Assign values and properties to cell ranges. Enhance text with different font styles and sizes. Implement formulas with ease and protect sheets from editing. Finally, save the Excel file using the Save As function. IronXL simplifies Excel operations without requiring Microsoft Excel or Excel Interop on your server, making it a versatile tool for developers.

// Import the IronXL namespace
using IronXL;

class ExcelExample
{
    static void Main()
    {
        // Create a new workbook with a default sheet named "Sheet1"
        var workbook = Workbook.Create();

        // Add a new sheet to the workbook
        var sheet = workbook.CreateWorkSheet("MySheet");

        // Set value in a cell
        sheet["A1"].Value = "Hello, IronXL!";

        // Assign a formula to a cell
        sheet["B1"].Formula = "=SUM(1, 2, 3)";

        // Customize cell styling
        var cell = sheet["A1"];
        cell.Style.Font.Bold = true; // Make font bold
        cell.Style.Fill.SetPattern(FillPatternStyle.Solid, Color.Cyan, Color.Empty);

        // Define a range of cells and set a value
        var range = sheet.GetRange("C1:D2");
        range.Value = "Range Test";

        // Set protection on the sheet
        sheet.Protect();

        // Save the workbook to the file system
        workbook.SaveAs("example.xlsx");

        // Inform user of completion
        Console.WriteLine("Excel file 'example.xlsx' created successfully.");
    }
}
// Import the IronXL namespace
using IronXL;

class ExcelExample
{
    static void Main()
    {
        // Create a new workbook with a default sheet named "Sheet1"
        var workbook = Workbook.Create();

        // Add a new sheet to the workbook
        var sheet = workbook.CreateWorkSheet("MySheet");

        // Set value in a cell
        sheet["A1"].Value = "Hello, IronXL!";

        // Assign a formula to a cell
        sheet["B1"].Formula = "=SUM(1, 2, 3)";

        // Customize cell styling
        var cell = sheet["A1"];
        cell.Style.Font.Bold = true; // Make font bold
        cell.Style.Fill.SetPattern(FillPatternStyle.Solid, Color.Cyan, Color.Empty);

        // Define a range of cells and set a value
        var range = sheet.GetRange("C1:D2");
        range.Value = "Range Test";

        // Set protection on the sheet
        sheet.Protect();

        // Save the workbook to the file system
        workbook.SaveAs("example.xlsx");

        // Inform user of completion
        Console.WriteLine("Excel file 'example.xlsx' created successfully.");
    }
}
$vbLabelText   $csharpLabel

Key Points:

  • Workbook.Create(): Initiates a new Excel workbook.
  • sheet["A1"].Value: Set cell value directly.
  • sheet["B1"].Formula: Assign a formula to a cell.
  • cell.Style: Customize cell appearance by modifying its style properties.
  • sheet.Protect(): Protect the sheet to prevent editing.
  • workbook.SaveAs(): Save the workbook to a file.

Further Reading: IronXL Documentation

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Write an Excel File in C#
NEXT >
How to Write to Excel Files in .NET with C#