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


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

    • 公式导出 #88

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

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

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

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

    exporter.Append(list1).SeparateByColumn().Append(list2).ExportAppendData(filePath);

    导出形式如下所示:

    header 1header 2 header1
    row 1 col 1 row 1 col 2   row1
    row 2 col 1 row 2 col 2   row2

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

    exporter.Append(list1).SeparateBySheet().Append(list2).ExportAppendData(filePath);

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

    exporter.Append(list1).SeparateByRow().Append(list2).ExportAppendData(filePath);
    header 1header 2 header1
    row 1 col 1 row 1 col 2   row1
    row 2 col 1 row 2 col 2   row2
           
    row 2 col 1 row 2 col 2   row2

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

    exporter.Append(list1).SeparateByRow().AppendHeaders().Append(list2).ExportAppendData(filePath);
    header 1header 2 header1
    row 1 col 1 row 1 col 2   row1
    row 2 col 1 row 2 col 2   row2
           
    header 1 header 2   header1
    row 2 col 1 row 2 col 2   row2
    • 添加对ExpandoObject类型的支持#135

    特别感谢 sgalcheung 添加该特性的导出,具体使用方式如下所示:

    class Program
        {
            static async Task Main(string[] args)
            {
                IExporter exporter = new ExcelExporter();
                // 生成测试数据
                var personList = GenFu.GenFu.ListOf<Person>();
    ​
                // 导出一个只包含"FirstName", "LastName"列的excel
                string fields = "FirstName,LastName"; // 可自定义导出想要的字段
                var expandoObjectList = new List<ExpandoObject>(personList.Count);
                var propertyInfoList = new List<PropertyInfo>();
                var fieldsAfterSplit = fields.Split(',');
                foreach (var field in fieldsAfterSplit)
                {
                    var propertyName = field.Trim();
                    var propertyInfo = typeof(Person).GetProperty(propertyName);
    ​
                    if (propertyInfo == null)
                    {
                        throw new Exception($"Property: {propertyName} 没有找到:{typeof(Person)}");
                    }
    ​
                    propertyInfoList.Add(propertyInfo);
                }
    ​
                foreach (var person in personList)
                {
                    var shapedObj = new ExpandoObject();
    ​
                    foreach (var propertyInfo in propertyInfoList)
                    {
                        var propertyValue = propertyInfo.GetValue(person);
                        ((IDictionary<string, object>)shapedObj).Add(propertyInfo.Name, propertyValue);
                    }
    ​
                    expandoObjectList.Add(shapedObj);
                }
    ​
                string filePath = Path.Combine(Directory.GetCurrentDirectory(), "dynamicExportExcel.xlsx");
                var result = await exporter.ExportAsByteArray<ExpandoObject>(expandoObjectList);
                File.WriteAllBytes(filePath, result);
            }
        }
    ​
    ​
        class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Title { get; set; }
            public int Age { get; set; }
            public int NumberOfKids { get; set; }
        }

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

  • 相关阅读:
    ABAP-FI-Redirection of read accesses from ANEA to FAAV_ANEA failed
    招聘
    五分钟教你在长沙如何找到靠谱的驾校和教练(长沙找驾校)
    数组哪些方法能改变原数组,以及循环改变数组的情况
    FXGL游戏开发-JavaFX游戏框架
    tempermonkey.d.ts | 油猴函数类型
    post导出文件
    mescroll.js 使用
    查看托管应用池用法
    IDEA配置
  • 原文地址:https://www.cnblogs.com/codelove/p/13683359.html
Copyright © 2020-2023  润新知