Gufel.ExcelBuilder 2.1.0

dotnet add package Gufel.ExcelBuilder --version 2.1.0
                    
NuGet\Install-Package Gufel.ExcelBuilder -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gufel.ExcelBuilder" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Gufel.ExcelBuilder" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Gufel.ExcelBuilder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Gufel.ExcelBuilder --version 2.1.0
                    
#r "nuget: Gufel.ExcelBuilder, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Gufel.ExcelBuilder@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Gufel.ExcelBuilder&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Gufel.ExcelBuilder&version=2.1.0
                    
Install as a Cake Tool

Gufel.ExcelBuilder

A powerful and flexible Excel file builder library for .NET applications that makes it easy to create and customize Excel files programmatically.

Features

  • Create Excel files with multiple worksheets
  • Custom column formatting and styling
  • Support for Persian date formatting
  • Customizable cell styles and headers
  • Right-to-Left (RTL) support
  • Auto-fit columns
  • Row numbering
  • Custom value providers
  • Custom column providers
  • Event-based customization
  • Support for dynamic data
  • Memory-efficient processing
  • Thread-safe operations
  • Support sql data reader

Installation

dotnet add package Gufel.ExcelBuilder

Usage

Basic Example

using Gufel.ExcelBuilder;

// Create a list of data
var data = new List<Person>
{
    new Person { Name = "John", Age = 30, BirthDate = new DateTime(1993, 1, 1) },
    new Person { Name = "Jane", Age = 25, BirthDate = new DateTime(1998, 5, 15) }
};

// Create and configure the Excel builder
using var builder = new ExcelBuilder();
builder.AddSheet("People", data);

// Get the Excel file as byte array
byte[] excelFile = builder.BuildFile();

Advanced Example with Custom Styling

using Gufel.ExcelBuilder;

// Create custom settings
var settings = new ExcelBuilderSettings
{
    UseDefaultStyle = true,    
    HasRowNumber = true,
    RowNumberColumnName = "Row",
    AutoFitColumns = true,
    IsRtl = true
};

// Create and configure the Excel builder
using var builder = new ExcelBuilder();
builder.SetSettings(settings);

// Create custom styles
var headerStyle = builder.CreateStyle("CustomHeader");
headerStyle.Object.Style.Fill.PatternType = ExcelFillStyle.Solid;
headerStyle.Object.Style.Fill.BackgroundColor.SetColor(Color.LightBlue);
headerStyle.Object.Style.Font.Bold = true;

var cellStyle = builder.CreateStyle("CustomCell");
cellStyle.Object.Style.Font.Name = "Arial";
cellStyle.Object.Style.Font.Size = 10;

// Add data with custom column rendering
builder.OnRenderColumn += (column, value, excelColumn, rowData) =>
{
    if (column == "Age" && value is int age)
    {
        excelColumn.Value = $"{age} years";
        return true;
    }
    return false;
};

builder.AddSheet("People", data);
byte[] excelFile = builder.BuildFile();

Model with Excel Column Attributes

public class Person
{
    [ExcelColumn(Name = "Full Name")]
    public string Name { get; set; }

    [ExcelColumn(Name = "Age", ColumnFormat = "#,##0")]
    public int Age { get; set; }

    [ExcelColumn(Name = "Birth Date", AsPersianDate = true, PersianDateFormat = "$yyyy/$MM/$dd")]
    public DateTime BirthDate { get; set; }
}

Customization

Custom Value Provider

public class CustomValueProvider : IValueProvider
{
    public Dictionary<string, object?> GetValues(List<ExcelColumnAttribute> columns, object data)
    {
        // Implement custom value extraction logic
    }
}

// Usage
builder.SetValueProvider(new CustomValueProvider());

Custom Column Provider

public class CustomColumnProvider : IColumnProvider
{
    public List<ExcelColumnAttribute> GetColumns(Type dataType, object? data)
    {
        // Implement custom column extraction logic
    }
}

// Usage
builder.SetColumnProvider(new CustomColumnProvider());

Advanced Features

Event Handlers

The library provides several event handlers for customizing the Excel generation process:

  • OnCreateWorksheet: Called when a new worksheet is created
  • OnCreateColumn: Called when a new column is created
  • OnRenderColumn: Called when a column value is being rendered

Persian Date Support

The library includes built-in support for Persian dates with customizable formatting:

[ExcelColumn(Name = "Birth Date", AsPersianDate = true, PersianDateFormat = "$yyyy/$MM/$dd")]
public DateTime BirthDate { get; set; }

RTL Support

Enable Right-to-Left support for worksheets:

var settings = new ExcelBuilderSettings
{
    IsRtl = true
};

Memory Management

The library uses IDisposable pattern for proper resource management:

using var builder = new ExcelBuilder();
// ... use the builder
// Resources will be automatically disposed

Sql data reader

Add sheet with sqlreader, support custom column name and order

using var builder = new ExcelBuilder();
var sqlConnection = new SqlConnection("sql_connection_string");
var sqlCommand = new SqlCommand("Select * from Report", sqlConnection);
sqlConnection.Open();
var reader = sqlCommand.ExecuteReader();

//this will read custom column name and order
var ctx = new List<ExcelColumnAttribute>();
var provider = new ManualColumnProvider(ctx);
excelBuilder.SetColumnProvider(provider);
var fileByte = excelBuilder.AddSheet("SqlData", reader).BuildFile();

reader.Close();
sqlCommand.Dispose();
sqlConnection.Close();
sqlConnection.Dispose();

License

Please note library use epplus so please set license before using.

 ExcelPackage.License.SetNonCommercialPersonal("Personal");

This project is licensed under the terms specified in the LICENSE.txt file.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 152 7/8/2025
2.0.0 145 7/8/2025
1.0.0 151 6/17/2025

Add sql reader (multiple result) to excel builder