Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库

Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库

在2.3这一版本的更新中,我们迎来了众多的使用者、贡献者,在这个里程碑中我们也添加并修复了一些功能。对于新特点的功能我将在下面进行详细的描述,当然也欢迎更多的人可以加入进来,再或者也很期待大家来提issues或者PR,您的一个issue或者PR将是我们前进的动力。

Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库
  • 公式导出 #88

(https://github.com/dotnetcore/Magicodes.IE/issues/88)

公式导出目前已经在Excel模板导出中支持,我们可以通过如上代码片段的格式进行将公式应用到我们的Excel导出模板中.

<code>{{Formula::AVERAGE?params=G4:G6}}/<code><code>{{Formula::SUM?params=G4:G6&G4}}/<code>
  • 增加分栏、分sheet、追加rows导出 #74

(https://github.com/dotnetcore/Magicodes.IE/issues/74)

在该版本中我们支持使用链式编程导出我们的Excel,从而起到追加分栏等作用。

在多个DTO导出中我们可以通过如下代码片段将我们的Excel分栏导出

<code>

exporter

.Append

(

list1

)

.SeparateByColumn

.Append

(

list2

)

.ExportAppendData

(

filePath

);/<code>

导出形式如下所示:




header 1header 2header1row 1 col 1row 1 col 2row1row 2 col 1row 2 col 2row2

我们还可以通过多个DTO进行多Sheet的导出,如下代码片段所示:

<code>

exporter

.Append

(

list1

)

.SeparateBySheet

.Append

(

list2

)

.ExportAppendData

(

filePath

);/<code>
Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库

当然不仅仅是这样,我们还可以对行进行追加导出,同时我们可以选择导出额外的Header信息或者不导出

<code>

exporter

.Append

(

list1

)

.SeparateByRow

.Append

(

list2

)

.ExportAppendData

(

filePath

);/<code>








header 1header 2header1row 1 col 1row 1 col 2row1row 2 col 1row 2 col 2row2row 2 col 1row 2 col 2row2

或者我们可以这样加入Header信息

<code>

exporter

.Append

(

list1

)

.SeparateByRow

.AppendHeaders

.Append

(

list2

)

.ExportAppendData

(

filePath

);/<code>









header 1header 2header1row 1 col 1row 1 col 2row1row 2 col 1row 2 col 2row2header 1header 2header1row 2 col 1row 2 col 2row2
  • 添加对ExpandoObject类型的支持#135

(https://github.com/dotnetcore/Magicodes.IE/issues/135)

特别感谢 sgalcheung(
https://github.com/sgalcheung)
添加该特性的导出,具体使用方式如下所示:

<code>class Program/<code><code> {/<code><code> static async Task Main(

string

[] args)/<code><code> {/<code><code> IExporter exporter = new ExcelExporter;/<code><code> // 生成测试数据/<code><code> var personList = GenFu.GenFu.ListOf;/<code>
<code> // 导出一个只包含

"FirstName"

,

"LastName"

列的excel/<code><code>

string

fields =

"FirstName,LastName"

; // 可自定义导出想要的字段/<code><code> var expandoObjectList = new List(personList.Count);/<code><code> var propertyInfoList = new List;/<code><code> var fieldsAfterSplit = fields.Split(

','

);/<code><code>

foreach

(var field

in

fieldsAfterSplit)/<code><code> {/<code><code> var propertyName = field.Trim;/<code><code> var propertyInfo = typeof(Person).GetProperty(propertyName);/<code>
<code>

if

(propertyInfo == )/<code><code> {/<code><code> throw new Exception($

"Property: {propertyName} 没有找到:{typeof(Person)}"

);/<code><code> }/<code>
<code> propertyInfoList.Add(propertyInfo);/<code><code> }/<code>
<code>

foreach

(var person

in

personList)/<code><code> {/<code><code> var shapedObj = new ExpandoObject;/<code>
<code>

foreach

(var propertyInfo

in

propertyInfoList)/<code><code> {/<code><code> var propertyValue = propertyInfo.GetValue(person);/<code><code> ((IDictionary<

string

, object>)shapedObj).Add(propertyInfo.Name, propertyValue);/<code><code> }/<code>
<code> expandoObjectList.Add(shapedObj);/<code><code> }/<code>
<code>

string

filePath = Path.Combine(Directory.GetCurrentDirectory,

"dynamicExportExcel.xlsx"

);/<code><code> var result = await exporter.ExportAsByteArray(expandoObjectList);/<code><code> File.WriteAllBytes(filePath, result);/<code><code> }/<code><code> }/<code>

<code> class Person/<code><code> {/<code><code> public

string

FirstName { get; set; }/<code><code> public

string

LastName { get; set; }/<code><code> public

string

Title { get; set; }/<code><code> public int Age { get; set; }/<code><code> public int NumberOfKids { get; set; }/<code><code> }/<code>

感谢大家对Magicodes.IE的支持。关于更多发布信息大家可以参阅:

  • https://github.com/dotnetcore/Magicodes.IE/blob/master/RELEASE.md

  • https://github.com/dotnetcore/Magicodes.IE

转载是一种动力 分享是一种美德

Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库

如果喜欢作者的文章,请关注【麦扣聊技术】订阅号以便第一时间获得最新内容。本文版权归作者和湖南心莱信息科技有限公司共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文作者:HueiFeng

文档官网:docs.xin-lai.com

编程交流群<85318032>

产品交流群<897857351>

Magicodes.IE 2.3重磅发布——.NET Core开源导入导出库


分享到:


相關文章: