Skip to content

Commit

Permalink
2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Nov 7, 2021
1 parent b2fe3bc commit 5e04e32
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 13 deletions.
10 changes: 5 additions & 5 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.0.1</AssemblyVersion>
<FileVersion>2.0.1</FileVersion>
<Version>2.0.1</Version>
<AssemblyVersion>2.0.2</AssemblyVersion>
<FileVersion>2.0.2</FileVersion>
<Version>2.0.2</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2021</Copyright>
<PackageId>ArrayToExcel</PackageId>
<Product>ArrayToExcel</Product>
<Title>ArrayToExcel</Title>
<Description>Create Excel from Array</Description>
<PackageTags>array excel xlsx array2excel arraytoexcel convert map mapping dotnet</PackageTags>
<PackageTags>excel xlsx array2excel arraytoexcel array list dataset datatable convert map mapping dotnet</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/mustaddon/ArrayToExcel</PackageProjectUrl>
<RepositoryUrl>https://github.com/mustaddon/ArrayToExcel</RepositoryUrl>
Expand All @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.10.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.14.0" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions ArrayToExcel/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace ArrayToExcel
{
Expand All @@ -10,5 +12,56 @@ public static byte[] ToExcel<T>(this IEnumerable<T> items, Action<SchemaBuilder<
return ArrayToExcel.CreateExcel(items, schema);
}

public static byte[] ToExcel(this DataSet dataSet, Action<SchemaBuilder<DataRow>>? schema = null)
{
var tables = dataSet.Tables.AsEnumerable().ToList();
return ToExcel(tables.First(), builder =>
{
foreach (var table in tables.Skip(1))
builder.AddSheet(table);
schema?.Invoke(builder);
});
}

public static byte[] ToExcel(this DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
{
return ArrayToExcel.CreateExcel(dataTable.Rows.AsEnumerable(), builder =>
{
if (!string.IsNullOrWhiteSpace(dataTable.TableName))
builder.SheetName(dataTable.TableName);
foreach (DataColumn col in dataTable.Columns)
builder.AddColumn(col.ColumnName, x => x[col]);
schema?.Invoke(builder);
});
}

public static SchemaBuilder<T> AddSheet<T>(this SchemaBuilder<T> builder, DataTable dataTable, Action<SheetSchemaBuilder<DataRow>>? schema = null)
{
return builder.AddSheet(dataTable.Rows.AsEnumerable(), builder =>
{
if (!string.IsNullOrWhiteSpace(dataTable.TableName))
builder.SheetName(dataTable.TableName);
foreach (DataColumn col in dataTable.Columns)
builder.AddColumn(col.ColumnName, x => x[col]);
schema?.Invoke(builder);
});
}

private static IEnumerable<DataRow> AsEnumerable(this DataRowCollection items)
{
foreach (DataRow item in items)
yield return item;
}

private static IEnumerable<DataTable> AsEnumerable(this DataTableCollection items)
{
foreach (DataTable item in items)
yield return item;
}
}
}
82 changes: 74 additions & 8 deletions Examples/Example.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ArrayToExcel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
Expand All @@ -17,7 +19,11 @@ static void Main(string[] args)
Example4();
Example5();
Example6();
Example7();

TestDictionary();
TestExpandoObject();
TestHashtable();
TestDataTable();
TestTypes();
}

Expand Down Expand Up @@ -84,8 +90,31 @@ static void Example5()
File.WriteAllBytes($@"..\..\..\..\{nameof(Example5)}.xlsx".ToLower(), excel);
}

// list of dictionaries
// DataSet
static void Example6()
{
var dataSet = new DataSet();

foreach (var i in Enumerable.Range(1, 3))
{
var table = new DataTable($"Table{i}");
dataSet.Tables.Add(table);

table.Columns.Add($"Column {i}-1", typeof(string));
table.Columns.Add($"Column {i}-2", typeof(int));
table.Columns.Add($"Column {i}-3", typeof(DateTime));

foreach (var x in Enumerable.Range(1, 10 * i))
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
}

var excel = dataSet.ToExcel();

File.WriteAllBytes($@"..\..\..\..\{nameof(Example6)}.xlsx".ToLower(), excel);
}

// list of dictionaries
static void TestDictionary()
{
var items = Enumerable.Range(1, 100).Select(x => new Dictionary<object, object>
{
Expand All @@ -94,13 +123,14 @@ static void Example6()
{ "Column #3", DateTime.Now.AddDays(-x) },
});

var excel = items.ToExcel();
var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet

File.WriteAllBytes($@"..\..\..\..\{nameof(Example6)}.xlsx".ToLower(), excel);
File.WriteAllBytes($@"..\{nameof(TestDictionary)}.xlsx".ToLower(), excel);
}

// list of expandos
static void Example7()
static void TestExpandoObject()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
Expand All @@ -112,11 +142,48 @@ static void Example7()
return item;
});

var excel = items.ToExcel();
var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet

File.WriteAllBytes($@"..\..\..\..\{nameof(Example7)}.xlsx".ToLower(), excel);
File.WriteAllBytes($@"..\{nameof(TestExpandoObject)}.xlsx", excel);
}

// list of hashtables
static void TestHashtable()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new Hashtable();
item.Add("Column #1", $"Text #{x}");
item.Add("Column #2", x * 1000);
item.Add("Column #3", DateTime.Now.AddDays(-x));
return item;
});

var excel = items.ToExcel(s => s
.AddSheet(items.Skip(10))); // extra sheet

File.WriteAllBytes($@"..\{nameof(TestHashtable)}.xlsx", excel);
}

// DataTable
static void TestDataTable()
{
var table = new DataTable("Table1");

table.Columns.Add("Column #1", typeof(string));
table.Columns.Add("Column #2", typeof(int));
table.Columns.Add("Column #3", typeof(DateTime));

foreach (var x in Enumerable.Range(1, 100))
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));

var excel = table.ToExcel();

File.WriteAllBytes($@"..\{nameof(TestDataTable)}.xlsx", excel);
}

// different types
static void TestTypes()
{
var items = Enumerable.Range(1, 100).Select(x => new
Expand All @@ -141,6 +208,5 @@ static void TestTypes()




}
}
Binary file modified Examples/example1.xlsx
Binary file not shown.
Binary file modified Examples/example2.xlsx
Binary file not shown.
Binary file modified Examples/example3.xlsx
Binary file not shown.
Binary file modified Examples/example4.xlsx
Binary file not shown.
Binary file modified Examples/example5.xlsx
Binary file not shown.
Binary file added Examples/example6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Examples/example6.xlsx
Binary file not shown.
Binary file removed Examples/example7.xlsx
Binary file not shown.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,29 @@ Result:
![](https://raw.githubusercontent.com/mustaddon/ArrayToExcel/master/Examples/example5.png)


### Example 6: Create from DataSet
```C#
var dataSet = new DataSet();

foreach (var i in Enumerable.Range(1, 3))
{
var table = new DataTable($"Table{i}");
dataSet.Tables.Add(table);

table.Columns.Add($"Column {i}-1", typeof(string));
table.Columns.Add($"Column {i}-2", typeof(int));
table.Columns.Add($"Column {i}-3", typeof(DateTime));

foreach (var x in Enumerable.Range(1, 10 * i))
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
}

var excel = dataSet.ToExcel();
```
Result:
[example6.xlsx](https://github.com/mustaddon/ArrayToExcel/raw/master/Examples/example6.xlsx)

![](https://raw.githubusercontent.com/mustaddon/ArrayToExcel/master/Examples/example6.png)


[Example.ConsoleApp](https://github.com/mustaddon/ArrayToExcel/tree/master/Examples/Example.ConsoleApp/)

0 comments on commit 5e04e32

Please sign in to comment.