WeihanLi.Npoi the support ShadowProperty

WeihanLi.Npoi support ShadowPropertythe

Intro

There was one in EF ShadowProperty(shadow properties / shadow property) concept, you can define a property model is not defined in .NET by FluentAPI way, only by EF Lane Change Trackerto operate this property.

When exporting Excel Sometimes we may want to export the column is not defined Fortunately, we are in the model, and some may just want to add one attribute value among export an attribute in a nested property, or I just simply want to define more than one, but this time the model may be written somewhere else to die, is not convenient to change.

So WeihanLi.Npoiis supported from version 1.6.0 ShadowProperty, the EF Lane ShadowPropertyintroduced to excel in export, at present, ShadowPropertyis not writable, read only, then return the default value of a type not supported ChangeTracker, does not support the change.

Examples of Use

:( look at a simple example using the example of this comes from the Netizen Issue: https://github.com/WeihanLi/WeihanLi.Npoi/issues/51 )

using System;
using System.Collections.Generic;
using System.IO;
using WeihanLi.Npoi;

namespace NpoiTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var settings = ExcelHelper.SettingFor<TestEntity>();
            settings.Property(x => x.Name)
                .HasColumnIndex(0);
            // settings.Property(x => x.UserFields)
            //     .HasOutputFormatter((entity, value) => $"{value[0].Value},{value[2].Value}")
            //     .HasColumnTitle("姓名,工号")
            //     .HasColumnIndex(1);
            settings.Property(x=>x.UserFields).Ignored();
            settings.Property("工号")
                .HasOutputFormatter((entity,val)=> $"{entity.UserFields[2].Value}")
                 ;
            settings.Property("部门")
                .HasOutputFormatter((entity,val)=> $"{entity.UserFields[1].Value}")
                 ;

            var data = new List<TestEntity>()
            {
                new TestEntity()
                {
                    Name = "xiaoming",
                    TotalScore = 100,
                    UserFields = new UserField[]
                    {
                        new UserField()
                        {
                            Name = "姓名",
                            Value = "xaioming",
                        },
                        new UserField()
                        {
                            Name = "部门",
                            Value = "1212"
                        },
                        new UserField()
                        {
                            Name = "工号",
                            Value = "121213131"
                        },
                    }
                }
            };
            data.ToExcelFile($@"{Directory.GetCurrentDirectory()}\output.xls");
            Console.WriteLine("complete.");
        }

        private class TestEntity
        {
            public string Name { get; set; }

            public UserField[] UserFields { get; set; }

            public int TotalScore { get; set; }
        }

        private class UserField
        {
            public string Fid { get; set; }
            public string Name { get; set; }
            public string Value { get; set; }
        }
    }
}

Export results are as follows:

You can see, we have added two not defined in the original Model in the exported Excel, by means of which we can be more flexible customization of content to be exported

More

Come and experience it, feedback is welcome, welcome issue

Reference

Guess you like

Origin www.cnblogs.com/weihanli/p/12033144.html