ExcelEi 1.0.7
dotnet add package ExcelEi --version 1.0.7
NuGet\Install-Package ExcelEi -Version 1.0.7
<PackageReference Include="ExcelEi" Version="1.0.7" />
<PackageVersion Include="ExcelEi" Version="1.0.7" />
<PackageReference Include="ExcelEi" />
paket add ExcelEi --version 1.0.7
#r "nuget: ExcelEi, 1.0.7"
#:package ExcelEi@1.0.7
#addin nuget:?package=ExcelEi&version=1.0.7
#tool nuget:?package=ExcelEi&version=1.0.7
ExcelEi
Library for excel export-import
It builds on top of EPPlus, ExcelDataReader and AutoMapper. Supported frameworks: .NET Framework 4.6.1; .NET Standard 2.0.
Import example:
public class NameValue
{
public string Name { get; set; }
public string Value { get; set; }
}
public void Foo()
{
var excelFilePath = "c:\\file.xlsx";
var package = new ExcelPackage(new FileInfo(excelFilePath));
var tableReader = ExcelTableReader.ReadContiguousTableWithHeader(package.Workbook.Worksheets[1], 1);
var pocoReader = new TableMappingReader<NameValue>()
.Map(o => o.Name)
.Map(o => o.Value);
IList<NameValue> pocoList = pocoReader.Read(tableReader);
}
Export example:
var outPath = "c:\\out.xlsx";
var package = new ExcelPackage(new FileInfo(outPath));
var dataSetExportConfig = new DataSetExportAutoConfig();
// data mappings
var configurator = new PocoExportConfigurator<PocoOne>("OneSheet", "One");
Expression<Func<PocoOne, int>> refId = o => o.Id;
Expression<Func<PocoOne, DateTime>> refDateTime = o => o.DateTime;
Expression<Func<PocoOne, IList<double?>>> refCollection = o => o.Values;
// calculated column
Expression<Func<PocoOne, string>> refJoinedCollection = o => o.Values != null ? string.Join(",", o.Values.Select(e => e.ToString())) : null;
configurator
.AddColumn(refId)
.AddColumn(refDateTime)
.AddColumn(refJoinedCollection, "Joined Values");
// will produce 5 columns for 5 of the first elements of the list, named 'Values[<index>]'
configurator.AddCollectionColumns(refCollection, 5);
dataSetExportConfig.AddSheet(configurator.Config);
// data to be exported
var dataSet = new DataSetAdapter();
var data1 = Enumerable.Range(0, 100)
.Select(i => new PocoOne(6))
.ToList();
dataSet.Add(data1, "One");
// exporter has data mappings and formatting settings
var exporter = new DataSetToWorkbookExporter(dataSetExportConfig) {DataSet = dataSet};
exporter.Export(package);
package.Save();
// exporter and data mappings can be reused for other data sets
POCO readers can convert column values:
public class PocoOneReader : TableMappingReader<PocoOne>
{
/// <inheritdoc />
public PocoOneReader()
{
Map(o => o.Id);
Map(o => o.DateTime);
Map(o => o.Values, "Joined Values", ParseJoinedValues);
}
private double?[] ParseJoinedValues(object joinedValue)
{
return joinedValue
?.ToString()
.Split(',')
.Select(Parse)
.ToArray();
}
private static double? Parse(string value)
{
if (string.IsNullOrEmpty(value))
return null;
return double.Parse(value);
}
}
Easy export from ADO.NET:
var dataSet = new DataSet();
// ...
var config = new DataSetExportAutoConfig(dataSet);
var exporter = new DataSetToWorkbookExporter(config) {DataSet = new DataSetAdapter(dataSet)};
using(var package = new ExcelPackage(new FileInfo("c:\\output.xlsx")))
{
exporter.Export(package);
package.Save();
}
Supports reflection for difficult situations:
configurator.AddColumn<int?>(nameof(PocoTwo.FieldInt), "Reflected Field");
Mappings for base classes can be encapsulated and reused with generics and inheritance (see wiki for more details). Export:
public class PocoBaseExportConfigurator<T> : PocoExportConfigurator<T>
where T: PocoBase
{
public PocoBaseExportConfigurator(string sheetName, string dataTableName)
: base(sheetName, dataTableName)
{
AddColumn(o => o.Id);
AddColumn(o => o.DateTime);
}
}
public class PocoOneExportBaseConfigurator<T> : PocoBaseExportConfigurator<T>
where T: PocoOne
{
public PocoOneExportBaseConfigurator(string sheetName, string dataTableName)
: base(sheetName, dataTableName)
{
AddColumn(o => o.Values != null ? string.Join(",", o.Values.Select(e => e.ToString())) : null, "Joined Values");
AddCollectionColumns(o => o.Values, 5, "value#{0}");
}
}
Import:
public class PocoOneBaseReader<T> : TableMappingReader<T>
where T: PocoOne
{
public PocoOneBaseReader()
{
Map(o => o.Id);
Map(o => o.DateTime);
}
}
public class PocoTwoReader : PocoOneBaseReader<PocoTwo>
{
public PocoTwoReader()
{
Map(o => o.IntegerFromPocoTwo);
}
}
Supports sparse columns and custom placement (see also corresponding test):
var exportConfig = new PocoThreeExportConfigurator(sheetName).Config;
const int firstColumnIndex = 2;
// this is index of the header row
const int firstRowIndex = 3;
exportConfig.LeftSheetColumnIndex = firstColumnIndex;
exportConfig.TopSheetRowIndex = firstRowIndex;
// no freezing panes
exportConfig.FreezeColumnIndex = null;
// move third column to the right
var movedColumnConfig = exportConfig.GetAutoColumnConfig(exportConfig.Columns[2].Caption);
movedColumnConfig.Index = exportConfig.Columns.Count + 2;
// allow it to grow more at the end of the table
movedColumnConfig.MaximumWidth = 300;
// ...
var columnReadingMap = exportConfig.Columns
.Select(c => new KeyValuePair<string, int>(c.Caption, firstColumnIndex + c.Index))
.ToList();
const int startDataRowIndex = firstRowIndex + 1;
var reader = new ExcelTableReader(workbook.Workbook.Worksheets[1], startDataRowIndex, null, columnReadingMap);
var readPocos = new PocoThreeReader().Read(reader);
Product | Versions 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.1
- AutoMapper (>= 10.1.1)
- AutoMapper.Extensions.ExpressionMapping (>= 2.0.0)
- EPPlus (>= 4.5.3)
- ExcelDataReader (>= 3.4.0)
- ExcelDataReader.DataSet (>= 3.4.0)
- log4net (>= 2.0.8)
-
.NETStandard 2.0
- AutoMapper (>= 10.1.1)
- AutoMapper.Extensions.ExpressionMapping (>= 2.0.0)
- EPPlus (>= 4.5.3)
- ExcelDataReader (>= 3.4.0)
- ExcelDataReader.DataSet (>= 3.4.0)
- log4net (>= 2.0.8)
- Microsoft.Extensions.Configuration.Json (>= 2.2.0)
- System.Collections.NonGeneric (>= 4.3.0)
- System.ComponentModel.TypeConverter (>= 4.3.0)
- System.Data.Common (>= 4.3.0)
- System.Data.SqlClient (>= 4.4.0)
- System.Drawing.Common (>= 4.5.1)
- System.Reflection (>= 4.3.0)
- System.Security.Claims (>= 4.3.0)
- System.Security.Cryptography.Pkcs (>= 4.5.1)
- System.Text.Encoding.CodePages (>= 4.5.0)
- System.Threading.Thread (>= 4.3.0)
- System.Threading.ThreadPool (>= 4.3.0)
- System.Xml.XmlDocument (>= 4.3.0)
- System.Xml.XPath.XmlDocument (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add convenience method PocoExportConfigurator<>.AddColumnCompiled to incrementally/fluently add POCO columns with lambda value extractor (compiled).