Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持传入多个类型 Collection<T> 进行导出 #52

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Magicodes.ExporterAndImporter.Excel
/// </summary>
public class ExcelExporter : IExcelExporter
{

private ExcelPackage _excelPackage;
/// <summary>
/// 导出Excel
/// </summary>
Expand All @@ -50,8 +50,42 @@ public async Task<ExportFileInfo> Export<T>(string fileName, ICollection<T> data
var bytes = await ExportAsByteArray(dataItems);
return bytes.ToExcelExportFileInfo(fileName);
}
/// <summary>
/// append collectioin to context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataItems"></param>
/// <returns></returns>
public ExcelExporter Append<T>(ICollection<T> dataItems) where T : class
{

var helper = this._excelPackage == null ? new ExportHelper<T>() : new ExportHelper<T>(_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;
}

/// <summary>
/// export excel after append all collectioins
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public ExportFileInfo Export(string fileName)
{
fileName.CheckExcelFileName();
if (this._excelPackage == null)
{
throw new ArgumentNullException("this method can only be called after method Append<T>;");
}
var bytes = _excelPackage.GetAsByteArray();
return bytes.ToExcelExportFileInfo(fileName);
}

/// <summary>
/// 导出Excel
Expand Down
22 changes: 21 additions & 1 deletion src/Magicodes.ExporterAndImporter.Excel/IExcelExporter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,5 +37,24 @@ public interface IExcelExporter : IExporter, IExportFileByTemplate
/// <param name="maxRowNumberOnASheet">一个Sheet最大允许的行数,设置了之后将输出多个Sheet</param>
/// <returns>文件二进制数组</returns>
Task<byte[]> ExportAsByteArray(DataTable dataItems, IExporterHeaderFilter exporterHeaderFilter = null, int maxRowNumberOnASheet = 1000000);


/// <summary>
/// append the collection to context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataItems"></param>
/// <returns></returns>
ExcelExporter Append<T>(ICollection<T> dataItems) where T : class;



/// <summary>
/// export excel after append all collectioins
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
ExportFileInfo Export(string fileName);

}
}
17 changes: 17 additions & 0 deletions src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public ExportHelper()
IsDynamicDatableExport = true;
}
}
/// <summary>
///
/// </summary>
/// <param name="existExcelPackage"></param>

public ExportHelper(ExcelPackage existExcelPackage)
{
if (typeof(DataTable).Equals(typeof(T)))
{
IsDynamicDatableExport = true;
}
if (existExcelPackage != null)
{
this._excelPackage = existExcelPackage;
}
}


/// <summary>
/// 导出设置
Expand Down
23 changes: 23 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExportTestDataWithAttrs>();

var list2 = GenFu.GenFu.ListOf<ExportTestDataWithSplitSheet>(30);


var result = exporter.Append(list1).Append(list2).Export(filePath);
result.ShouldNotBeNull();

File.Exists(filePath).ShouldBeTrue();
}


[Fact(DisplayName = "通过Dto导出表头")]
public async Task ExportHeaderAsByteArray_Test()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down