diff --git a/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs b/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs
index 164e6b07..9bf5fb14 100644
--- a/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs
+++ b/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs
@@ -37,7 +37,7 @@ namespace Magicodes.ExporterAndImporter.Excel
///
public class ExcelExporter : IExcelExporter
{
-
+ private ExcelPackage _excelPackage;
///
/// 导出Excel
///
@@ -50,8 +50,42 @@ public async Task Export(string fileName, ICollection data
var bytes = await ExportAsByteArray(dataItems);
return bytes.ToExcelExportFileInfo(fileName);
}
+ ///
+ /// append collectioin to context
+ ///
+ ///
+ ///
+ ///
+ public ExcelExporter Append(ICollection dataItems) where T : class
+ {
+
+ var helper = this._excelPackage == null ? new ExportHelper() : new ExportHelper(_excelPackage);
+ var sheetName = helper.ExcelExporterSettings?.Name ?? "导出结果";
+
+ if (this._excelPackage?.Workbook.Worksheets.Any(x => x.Name == sheetName) ?? false)
+ {
+ throw new ArgumentNullException($"已经存在名字为{sheetName }的sheet");
+ }
+ this._excelPackage = helper.Export(dataItems);
-
+ return this;
+ }
+
+ ///
+ /// export excel after append all collectioins
+ ///
+ ///
+ ///
+ public ExportFileInfo Export(string fileName)
+ {
+ fileName.CheckExcelFileName();
+ if (this._excelPackage == null)
+ {
+ throw new ArgumentNullException("this method can only be called after method Append;");
+ }
+ var bytes = _excelPackage.GetAsByteArray();
+ return bytes.ToExcelExportFileInfo(fileName);
+ }
///
/// 导出Excel
diff --git a/src/Magicodes.ExporterAndImporter.Excel/IExcelExporter.cs b/src/Magicodes.ExporterAndImporter.Excel/IExcelExporter.cs
index aecf58e5..72315892 100644
--- a/src/Magicodes.ExporterAndImporter.Excel/IExcelExporter.cs
+++ b/src/Magicodes.ExporterAndImporter.Excel/IExcelExporter.cs
@@ -1,4 +1,5 @@
-using Magicodes.ExporterAndImporter.Core;
+using System.Collections.Generic;
+using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Filters;
using Magicodes.ExporterAndImporter.Core.Models;
using System.Data;
@@ -36,5 +37,24 @@ public interface IExcelExporter : IExporter, IExportFileByTemplate
/// 一个Sheet最大允许的行数,设置了之后将输出多个Sheet
/// 文件二进制数组
Task ExportAsByteArray(DataTable dataItems, IExporterHeaderFilter exporterHeaderFilter = null, int maxRowNumberOnASheet = 1000000);
+
+
+ ///
+ /// append the collection to context
+ ///
+ ///
+ ///
+ ///
+ ExcelExporter Append(ICollection dataItems) where T : class;
+
+
+
+ ///
+ /// export excel after append all collectioins
+ ///
+ ///
+ ///
+ ExportFileInfo Export(string fileName);
+
}
}
diff --git a/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs b/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
index ce5347ca..1e80a2d7 100644
--- a/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
+++ b/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
@@ -38,6 +38,23 @@ public ExportHelper()
IsDynamicDatableExport = true;
}
}
+ ///
+ ///
+ ///
+ ///
+
+ public ExportHelper(ExcelPackage existExcelPackage)
+ {
+ if (typeof(DataTable).Equals(typeof(T)))
+ {
+ IsDynamicDatableExport = true;
+ }
+ if (existExcelPackage != null)
+ {
+ this._excelPackage = existExcelPackage;
+ }
+ }
+
///
/// 导出设置
diff --git a/src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs b/src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
index 846ec79c..83269442 100644
--- a/src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
+++ b/src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
@@ -292,6 +292,29 @@ public async Task ExportAsByteArray_Test()
File.Exists(filePath).ShouldBeTrue();
}
+
+ [Fact(DisplayName = "多个sheet导出")]
+ public async Task ExportMutiCollection_Test()
+ {
+ var exporter = new ExcelExporter();
+
+ var filePath = GetTestFilePath($"{nameof(ExportMutiCollection_Test)}.xlsx");
+
+ DeleteFile(filePath);
+
+
+ var list1 = GenFu.GenFu.ListOf();
+
+ var list2 = GenFu.GenFu.ListOf(30);
+
+
+ var result = exporter.Append(list1).Append(list2).Export(filePath);
+ result.ShouldNotBeNull();
+
+ File.Exists(filePath).ShouldBeTrue();
+ }
+
+
[Fact(DisplayName = "通过Dto导出表头")]
public async Task ExportHeaderAsByteArray_Test()
{
diff --git a/src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExportTestDataWithSplitSheet.cs b/src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExportTestDataWithSplitSheet.cs
index cced7229..ece14271 100644
--- a/src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExportTestDataWithSplitSheet.cs
+++ b/src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExportTestDataWithSplitSheet.cs
@@ -17,7 +17,7 @@
namespace Magicodes.ExporterAndImporter.Tests.Models.Export
{
- [ExcelExporter(Name = "测试", TableStyle = "Light10", AutoFitAllColumn = true, MaxRowNumberOnASheet = 100)]
+ [ExcelExporter(Name = "测试2", TableStyle = "Light10", AutoFitAllColumn = true, MaxRowNumberOnASheet = 100)]
public class ExportTestDataWithSplitSheet
{
[ExporterHeader(DisplayName = "加粗文本", IsBold = true)]