.NET Core開源導入導出庫 Magicodes.IE 2.3發佈

file

在2.3這一版本的更新中,咱們迎來了衆多的使用者、貢獻者,在這個里程碑中咱們也添加並修復了一些功能。對於新特色的功能我將在下面進行詳細的描述,固然也歡迎更多的人能夠加入進來,再或者也很期待你們來提issues或者PR,您的一個issue或者PR將是咱們前進的動力。git

file

  • 公式導出 #88

公式導出目前已經在Excel模板導出中支持,咱們能夠經過如上代碼片斷的格式進行將公式應用到咱們的Excel導出模板中.github

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

在該版本中咱們支持使用鏈式編程導出咱們的Excel,從而起到追加分欄等做用。編程

在多個DTO導出中咱們能夠經過以下代碼片斷將咱們的Excel分欄導出async

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

導出形式以下所示:測試

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

咱們還能夠經過多個DTO進行多Sheet的導出,以下代碼片斷所示:3d

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

file

固然不單單是這樣,咱們還能夠對行進行追加導出,同時咱們能夠選擇導出額外的Header信息或者不導出excel

exporter.Append(list1).SeparateByRow().Append(list2).ExportAppendData(filePath);
header 1 header 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信息code

exporter.Append(list1).SeparateByRow().AppendHeaders().Append(list2).ExportAppendData(filePath);
header 1 header 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 添加該特性的導出,具體使用方式以下所示:orm

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的支持。關於更多發佈信息你們能夠參閱:https://github.com/dotnetcore/Magicodes.IE/blob/master/RELEASE.mdblog

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

相關文章
相關標籤/搜索