具有IEnumerable <T>的NReco数据透视表

时间:2019-09-25 12:41:26

标签: c# pivot-table nreco

我有一个名为 SmtpUser 的对象,该对象基本上是已发送电子邮件的用户

public partial class SmtpUser
    {
        public DateTime NDate { get; set; }

        public string NFrom { get; set; }

        public string NTo { get; set; }

        public string NBytes { get; set; }

        public long NCount { get; set; }
    }

我有一个对象SmtpUser s ,它是一个列表

我在SmtpUsers中具有这些值:

SmtpUsers.AddRange(new List<SmtpUser>() {
            new SmtpUser() { NDate = "11/09/2019 16:22:00".ToDateTime() , NFrom = "jdoe@domain.com", NTo = "pmia@domain2.com", NBytes = "1", NCount = 1 },
                new SmtpUser() { NDate = "12/09/2019 16:22:00".ToDateTime() , NFrom = "fpolo@domain.com", NTo = "kholy@domain2.com", NBytes = "1", NCount = 2 },
                new SmtpUser() { NDate = "13/09/2019 16:22:00".ToDateTime(), NFrom = "fpolo@domain.com", NTo = "wfred@domain2.com", NBytes = "2", NCount = 1 },
                new SmtpUser() { NDate = "12/09/2019 16:22:00".ToDateTime(), NFrom = "ljan@domain.com", NTo = "heagle@domain2.com", NBytes = "1", NCount = 3 },
                new SmtpUser() { NDate = "14/09/2019 16:22:00".ToDateTime(), NFrom = "mfranck@domain.com", NTo = "pmia@domain2.com", NBytes = "1", NCount = 1 },
                new SmtpUser() { NDate = "14/09/2019 16:22:00".ToDateTime(), NFrom = "jdoe@domain.com", NTo = "pmia@domain2.com", NBytes = "1", NCount = 8 }
            });

现在,我在 groupedSmtpUsers

中按DateOf对SmtpUsers进行了分组
var groupedSmtpUsers =
                from groupedSmtpUser in SmtpUsers
                group groupedSmtpUser by new
                {
                    From = groupedSmtpUser.NFrom.ToLower()
                } into smtpList
                select new
                {
                    smtpList.Key.From,
                    Mails = smtpList.GroupBy(m => m.NDate.ToShortDateString()).Select(
                        p => new { DateOf = p.Key, MailsSentOnThisDate = p.ToList() }
                        ).ToList()
                };

Here's a GIF which shows groupedSmtpUsers

我想要创建一个数据透视表(使用NReco库或其他任何可以说的内容) 并将其导出到Excel。

在我的Excel中,我希望能够看到DateOf(行)和From(列) 对于每个DateOf,我需要能够看到每个MailsS​​entOnThisDate及其子对象属性(NFrom,NDate ....)

我尝试过的事情:

var pvtData = new PivotData(new[] { "From", "Mails" }, new CountAggregatorFactory());
            pvtData.ProcessData(groupedSmtpUsers, new ObjectMember().GetValue);

            var pkg = new ExcelPackage();
            pkg.Compression = CompressionLevel.Default;
            var wsPvt = pkg.Workbook.Worksheets.Add("Pivot Table");
            var wsData = pkg.Workbook.Worksheets.Add("Source Data");

            var pvtTbl = new PivotTable(
                new[] { "From" },
                new[] { "Mails" },
                pvtData);

            var excelPvtTblWr = new ExcelPivotTableWriter(wsPvt, wsData);
            excelPvtTblWr.Write(pvtTbl);
            using (var excelFs = new FileStream("result.xlsx", FileMode.Create, FileAccess.Write))
            {
                pkg.SaveAs(excelFs);
            }

这是结果。xlsx...

Click here to see

我该怎么做?

0 个答案:

没有答案