K4.DataExporter 1.0.8

dotnet add package K4.DataExporter --version 1.0.8
                    
NuGet\Install-Package K4.DataExporter -Version 1.0.8
                    
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="K4.DataExporter" Version="1.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="K4.DataExporter" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="K4.DataExporter" />
                    
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 K4.DataExporter --version 1.0.8
                    
#r "nuget: K4.DataExporter, 1.0.8"
                    
#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 K4.DataExporter@1.0.8
                    
#: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=K4.DataExporter&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=K4.DataExporter&version=1.0.8
                    
Install as a Cake Tool

K4.DataExporter

K4.DataExporter is a .NET Standard 2.1 library made to export any IEnumerable<T> to a XLSX file. This library is based of Guillaume Lacasa's work, updated for .NET Standard 2.1

Install

This library is available on Nuget : NuGet

How to use

The data exported will be a file with column titles on the first line, and all your data on the following lines. To export your data, you will need to define how it will be exported

Export using Attributes on your model

Add the attribute ExportColumn on the properties you want to export. The attributes contains a few properties, most important one being : Column (the name of the column) and ColumnOrder (to sort your columns)

public class SampleData
{
    [ExportColumn(Column= "Number", ColumnOrder = 1)]
       public int IntData { get; set; }

    [ExportColumn(Column= "Text", ColumnOrder = 2)]
       public string TextData { get; set; }
}

You also have ColumnFontName, ColumnFontSize, ColumnColorCode and ColumnCellColorCode. Remember that these properties only apply to the column title, not data.

public class SampleData
{
    [ExportColumn(Column= "Number", ColumnOrder = 1, ColumnFontName = "Verdana", ColumnFontColorCode = "FFFFFF", ColumnFontSize = 15, ColumnCellColorCode = "000000")]
       public int IntData { get; set; }

    [ExportColumn(Column = "Text", ColumnOrder = 2, ColumnFontColorCode = "00FF00", ColumnFontSize = 20)]
       public string TextData { get; set; }
}

Create your exporter

var data = new List<SampleData>()
{
    new SampleData{ IntData=5, TextData="Hello"},
    new SampleData{ IntData=20, TextData="Yoo"},
    new SampleData{ IntData=10, TextData="This is some text"},
};

var xlsxExporter = new XlsxDataExporter(true);
var xlsxResult = xlsxExporter.Export(data);

Create Xlsx file with several sheets

While the csv exporter will only allow you to export one set of data, with the xlsx exporter you can create several sheets with different data on each.

var xlsxExporter = new XlsxDataExporter(true);
xlsxExporter.AddSheet(data1, name="sheet title 1");
xlsxExporter.AddSheet(data2, name="sheet title 2");
var xlsxResult = xlsxExporter.GetFile();

Export using DataSet/DataTable

If you want to export a query result without binding it to a model, you can use a DataSet, it can work with Entity too

var xlsxExporter = new XlsxDataExporter(false);
DataSet dataSet = new DataSet();

List<Event> list = _context.Events.ToList();
DataTable dt = ToDataTable<Event>(list);

dataSet.Tables.Add(dt);
xlsxExporter.AddSheet(dataSet);

var xlsxResult = xlsxExporter.GetFile();

------------------------------------

private static DataTable ToDataTable<T>(List<T> items)
{
       DataTable dataTable = new DataTable(typeof(T).Name);

       PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
       foreach (PropertyInfo prop in Props)
       {
            dataTable.Columns.Add(prop.Name);
       }
       foreach (T item in items)
       {
             var values = new object[Props.Length];
             for (int i = 0; i < Props.Length; i++)
             {
                  values[i] = Props[i].GetValue(item, null);
             }
             dataTable.Rows.Add(values);
       }
       return dataTable;
}

You won't be able to use model's attributes with this export option, so make sure to set 'new XlsxDataExporter(false)' on initialization when using this export option.

Result

The exporters will output a MemoryStream. You can directly save it to a file, or return it in a FileResult in an MVC website.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.0.8 509 12/5/2022
1.0.7 505 12/20/2021
1.0.6 486 12/16/2021
1.0.4 474 12/16/2021
1.0.3 469 12/16/2021
1.0.2 494 12/16/2021
1.0.1 469 12/16/2021
1.0.0 553 12/14/2021

- Fixed an issue if string contained hex/not UTF8 values