TemplateExport.Excel
0.1.3
dotnet add package TemplateExport.Excel --version 0.1.3
NuGet\Install-Package TemplateExport.Excel -Version 0.1.3
<PackageReference Include="TemplateExport.Excel" Version="0.1.3" />
<PackageVersion Include="TemplateExport.Excel" Version="0.1.3" />
<PackageReference Include="TemplateExport.Excel" />
paket add TemplateExport.Excel --version 0.1.3
#r "nuget: TemplateExport.Excel, 0.1.3"
#:package TemplateExport.Excel@0.1.3
#addin nuget:?package=TemplateExport.Excel&version=0.1.3
#tool nuget:?package=TemplateExport.Excel&version=0.1.3
TemplateExport.Excel
TemplateExport.Excel is a library for exporting Excel documents using templates. It allows you to define templates with placeholders and dynamically replace them with data.
Features
- Load Excel templates from files
- Replace placeholders with data
- Support for conditional rendering
- Support for lists and collections
Installation
To install TemplateExport.Excel, add the following NuGet package to your project:
dotnet add package TemplateExport.Excel
Usage
1. Configure Services
First, configure the services in your Main function or startup class:
using Microsoft.Extensions.DependencyInjection;
using TemplateExport.Excel.Extensions;
var services = new ServiceCollection();
services.AddTemplateExportExcel();
var serviceProvider = services.BuildServiceProvider();
2. Create excel template on given location
Create template xlsx file on location provided to configuration builder template-example.xlsx
3. Create an Excel Export Configuration
Create a configuration for exporting an Excel document:
using TemplateExport.Excel.Models;
var config = ExcelExportConfiguration.CreateBuilder()
.UseTemplatePath("./Resources/template.xlsx")
.UseOutputPath("./Resources/output.xlsx")
.AddDataSet("Person", new Person { Name = "John", Age = 30 })
.Build();
4. Export the Excel
Resolve the ITemplateExportExcel service and call the Export method:
var excelExport = serviceProvider.GetService<ITemplateExportExcel>();
excelExport.Export(config);
5. Configuration options
UseOutputStream(stream) // set output stream for excel
UseTemplateStringStartsWith("{{") // set placeholders starts with string (Default: "{{")
UseTemplateStringEndsWith("}}") // set placeholders starts with string (Default: "}}")
UseTemplateStringSeparator("::") // set placeholders separator string (Default: "::")
EnablePreserveRowHeight(false) // preserve row height of template file. Can be very slow for a lot of rows (Default: true)
EnablePreserveColumnWidth(false) // preserve column width of template file. Can be very slow for a lot of columns (Default: true)
EnablePreserveMergeCells(false) // preserve merge cells of template file. Can be very slow for a lot of merge cells (Default: true)
EnablePreserveCellStyles(false) // preserve cell styles of template file. Can be very slow for a lot of cells (Default: true)
EnableAutoFitColumns(true) // auto fit columns after export. Cannot be true if PreserveColumnWidth is enabled (Default: false)
EnableAutoFitRows(true) // auto fit rows after export. Cannot be true if PreserveRowHeight is enabled (Default: false)
AddDataSet("Person", new Person { Name = "John", Age = 30 }) // add data set to configuration
6. Charts
TemplateExport.Excel supports charts. Just create template with chart referencing cells with placeholders.
var config = ExcelExportConfiguration.CreateBuilder()
.UseTemplatePath("./Resources/chart.xlsx")
.UseOutputPath("./Resources/chartOutput.xlsx")
.AddDataSet("aaa", 2)
.AddDataSet("bbb", 3)
.EnableCopyGraphs()
.Build();
templateExportExcel.Export(config);
Example
Here is a complete example of exporting an Excel document:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using TemplateExport.Excel;
using TemplateExport.Excel.Extensions;
using TemplateExport.Excel.Models;
var services = new ServiceCollection();
services.AddTemplateExportExcel();
var serviceProvider = services.BuildServiceProvider();
var excelExport = serviceProvider.GetService<ITemplateExportExcel>();
var person = new Person { Name = "John", Age = 30 };
var people = new List<Person>
{
new Person { Name = "Alice", Age = 25 },
new Person { Name = "Bob", Age = 35 }
};
var config = ExcelExportConfiguration.CreateBuilder()
.UseTemplatePath("./Resources/template.xlsx")
.UseOutputPath("./Resources/output.xlsx")
.AddDataSet("Person", person)
.AddDataSet("People", people)
.Build();
var stopwatch = new Stopwatch();
stopwatch.Start();
excelExport.Export(config);
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
License
This project is licensed under the MIT License. See the LICENSE file for more details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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 is compatible. 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. |
-
net10.0
- ClosedXML (>= 0.100.0 && <= 0.105.0-rc)
- System.Drawing.Common (>= 10.0.0-preview.2.25163.9)
-
net6.0
- ClosedXML (>= 0.100.0 && <= 0.105.0-rc)
- System.Drawing.Common (>= 10.0.0-preview.2.25163.9)
-
net7.0
- ClosedXML (>= 0.100.0 && <= 0.105.0-rc)
- System.Drawing.Common (>= 10.0.0-preview.2.25163.9)
-
net8.0
- ClosedXML (>= 0.100.0 && <= 0.105.0-rc)
- System.Drawing.Common (>= 10.0.0-preview.2.25163.9)
-
net9.0
- ClosedXML (>= 0.100.0 && <= 0.105.0-rc)
- System.Drawing.Common (>= 10.0.0-preview.2.25163.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.