-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters