Skip to content

Commit

Permalink
2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Jan 8, 2024
1 parent f69380d commit 08f165b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 113 deletions.
80 changes: 42 additions & 38 deletions ArrayToExcel/ArrayToExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,68 @@ namespace ArrayToExcel
public class ArrayToExcel
{
public static MemoryStream CreateExcel<T>(IEnumerable<T> items, Action<SchemaBuilder<T>>? schema = null)
{
var ms = new MemoryStream();
CreateExcel(ms, items, schema);
ms.Position = 0;
return ms;
}

public static void CreateExcel<T>(Stream stream, IEnumerable<T> items, Action<SchemaBuilder<T>>? schema = null)
{
var builder = new SchemaBuilder<T>(items);
schema?.Invoke(builder);
return CreateExcel(new[] { builder.Schema }.Concat(builder.Childs));
CreateExcel(stream, new[] { builder.Schema }.Concat(builder.Childs));
}

static MemoryStream CreateExcel(IEnumerable<SheetSchema> sheetSchemas)
static void CreateExcel(Stream stream, IEnumerable<SheetSchema> sheetSchemas)
{
var ms = new MemoryStream();
using (var document = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook))
{
var workbookpart = document.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
using var document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);

var sheets = workbookpart.Workbook.AppendChild(new Sheets());
var workbookpart = document.AddWorkbookPart();
workbookpart.Workbook = new Workbook();

AddStyles(workbookpart);
var sheets = workbookpart.Workbook.AppendChild(new Sheets());

var sheetNames = new HashSet<string>();
var sheetId = 0u;
AddStyles(workbookpart);

foreach (var sheetSchema in sheetSchemas)
{
sheetId++;
var sheetNames = new HashSet<string>();
var sheetId = 0u;

var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
foreach (var sheetSchema in sheetSchemas)
{
sheetId++;

var sheet = new Sheet()
{
Id = workbookpart.GetIdOfPart(worksheetPart),
SheetId = sheetId,
Name = NormSheetName(sheetSchema.SheetName, sheetId, sheetNames),
};
var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();

sheetNames.Add(sheet.Name.Value ?? string.Empty);
sheets.Append(sheet);
var sheet = new Sheet()
{
Id = workbookpart.GetIdOfPart(worksheetPart),
SheetId = sheetId,
Name = NormSheetName(sheetSchema.SheetName, sheetId, sheetNames),
};

var cols = worksheetPart.Worksheet.AppendChild(new Columns());
var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
sheetNames.Add(sheet.Name.Value ?? string.Empty);
sheets.Append(sheet);

if (sheetSchema.Columns.Count == 0)
{
cols.Append(new Column() { Min = 1, Max = 1, BestFit = true });
continue;
}
var cols = worksheetPart.Worksheet.AppendChild(new Columns());
var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

cols.Append(sheetSchema.Columns.Select((x, i) => new Column() { Min = (uint)(i + 1), Max = (uint)(i + 1), Width = x.Width, CustomWidth = true, BestFit = true }));
if (sheetSchema.Columns.Count == 0)
{
cols.Append(new Column() { Min = 1, Max = 1, BestFit = true });
continue;
}

sheetData.Append(GetRows(sheetSchema.Items, sheetSchema.Columns));
cols.Append(sheetSchema.Columns.Select((x, i) => new Column() { Min = (uint)(i + 1), Max = (uint)(i + 1), Width = x.Width, CustomWidth = true, BestFit = true }));

worksheetPart.Worksheet.Append(new AutoFilter() { Reference = $"A1:{GetColReference(sheetSchema.Columns.Count - 1)}{sheetData.ChildElements.Count}" });
}
sheetData.Append(GetRows(sheetSchema.Items, sheetSchema.Columns));

workbookpart.Workbook.Save();
worksheetPart.Worksheet.Append(new AutoFilter() { Reference = $"A1:{GetColReference(sheetSchema.Columns.Count - 1)}{sheetData.ChildElements.Count}" });
}
ms.Position = 0;
return ms;

workbookpart.Workbook.Save();
}

static string NormSheetName(string? value, uint sheetId, HashSet<string> existNames)
Expand Down
6 changes: 3 additions & 3 deletions ArrayToExcel/ArrayToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\ArrayToExcel.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>2.2.4</AssemblyVersion>
<FileVersion>2.2.4</FileVersion>
<Version>2.2.4</Version>
<AssemblyVersion>2.3.0</AssemblyVersion>
<FileVersion>2.3.0</FileVersion>
<Version>2.3.0</Version>
<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2024</Copyright>
Expand Down
33 changes: 33 additions & 0 deletions ArrayToExcel/Extensions.DataSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Data;
using System.IO;
using System.Linq;

namespace ArrayToExcel
{
public static partial class Extensions
{
public static void ToExcel(this DataSet dataSet, Stream stream, Action<DataTable, SchemaBuilder<DataRow>>? schema = null)
{
var tables = dataSet.Tables.AsEnumerable().ToList();
ToExcel(tables.First(), stream, builder =>
{
foreach (var table in tables.Skip(1))
builder.AddSheet(table, s => schema?.Invoke(table, s));
schema?.Invoke(tables.First(), builder);
});
}

public static byte[] ToExcel(this DataSet dataSet, Action<DataTable, SchemaBuilder<DataRow>>? schema = null)
=> ToExcelStream(dataSet, schema).ToArray();

public static MemoryStream ToExcelStream(this DataSet dataSet, Action<DataTable, SchemaBuilder<DataRow>>? schema = null)
{
var ms = new MemoryStream();
ToExcel(dataSet, ms, schema);
ms.Position = 0;
return ms;
}
}
}
46 changes: 46 additions & 0 deletions ArrayToExcel/Extensions.DataTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace ArrayToExcel
{
public static partial class Extensions
{
public static void ToExcel(this DataTable dataTable, Stream stream, Action<SchemaBuilder<DataRow>>? schema = null)
=> ArrayToExcel.CreateExcel(stream, dataTable.Rows.AsEnumerable(), b => dataTable.SchemaBuilder(b, schema));

public static byte[] ToExcel(this DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
=> ToExcelStream(dataTable, schema).ToArray();

public static MemoryStream ToExcelStream(this DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
=> ArrayToExcel.CreateExcel(dataTable.Rows.AsEnumerable(), b => dataTable.SchemaBuilder(b, schema));

public static SchemaBuilder<T> AddSheet<T>(this SchemaBuilder<T> builder, DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
=> builder.AddSheet(dataTable.Rows.AsEnumerable(), b => dataTable.SchemaBuilder(b, schema));


private static void SchemaBuilder(this DataTable dataTable, SchemaBuilder<DataRow> builder, Action<SchemaBuilder<DataRow>>? schema)
{
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;
}
}
}
73 changes: 6 additions & 67 deletions ArrayToExcel/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,19 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;

namespace ArrayToExcel
{
public static class Extensions
public static partial class Extensions
{
public static byte[] ToExcel<T>(this IEnumerable<T> items, Action<SchemaBuilder<T>>? schema = null)
{
using var ms = ToExcelStream(items, schema);
return ms.ToArray();
}

public static byte[] ToExcel(this DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
{
using var ms = ToExcelStream(dataTable, schema);
return ms.ToArray();
}
public static void ToExcel<T>(this IEnumerable<T> items, Stream stream, Action<SchemaBuilder<T>>? schema = null)
=> ArrayToExcel.CreateExcel(stream, items, schema);

public static byte[] ToExcel(this DataSet dataSet, Action<DataTable, SchemaBuilder<DataRow>>? schema = null)
{
using var ms = ToExcelStream(dataSet, schema);
return ms.ToArray();
}
public static byte[] ToExcel<T>(this IEnumerable<T> items, Action<SchemaBuilder<T>>? schema = null)
=> ArrayToExcel.CreateExcel(items, schema).ToArray();

public static MemoryStream ToExcelStream<T>(this IEnumerable<T> items, Action<SchemaBuilder<T>>? schema = null)
{
return ArrayToExcel.CreateExcel(items, schema);
}

public static MemoryStream ToExcelStream(this DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
{
return ArrayToExcel.CreateExcel(dataTable.Rows.AsEnumerable(), b => dataTable.SchemaBuilder(b, schema));
}

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

public static SchemaBuilder<T> AddSheet<T>(this SchemaBuilder<T> builder, DataTable dataTable, Action<SchemaBuilder<DataRow>>? schema = null)
{
return builder.AddSheet(dataTable.Rows.AsEnumerable(), b => dataTable.SchemaBuilder(b, schema));
}


private static void SchemaBuilder(this DataTable dataTable, SchemaBuilder<DataRow> builder, Action<SchemaBuilder<DataRow>>? schema)
{
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;
}
=> ArrayToExcel.CreateExcel(items, schema);

private static IEnumerable<DataTable> AsEnumerable(this DataTableCollection items)
{
foreach (DataTable item in items)
yield return item;
}
}
}
6 changes: 1 addition & 5 deletions Examples/Example.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,8 @@ static void TestTypes()
Percent = new Percent(1d/x),
});

using var excel = items.ToExcelStream();
using var file = File.Create($@"..\{nameof(TestTypes)}.xlsx");
excel.CopyTo(file);
items.ToExcel(file);
}



}
}

0 comments on commit 08f165b

Please sign in to comment.