EPPlus 中多个表之间的关系?

Relationships between multiple tables in EPPplus?

我希望创建一个依赖于数据模型的数据透视表,该数据模型取自我的工作簿中已定义的三个表。不幸的是我找不到如何做到这一点。

到目前为止我做了什么:

  1. 我编写的代码用于创建一个包含三个 table 的示例 Excel 文件,然后创建一个简单的 table 数据透视表位于底部其中 post。从该代码中的硬编码数据可以看出,这三个 table 定义了两个人、两只宠物以及这些宠物的所有权。一只宠物为两人所有,另一只宠物为一人所有。然后在单个 table PeoplePets.

    上创建枢轴 SimplePivotTbl
  2. 接下来,我在 Excel 2016 年打开了 test.xlsx 并创建了明显的多 table 数据透视表 ComplexPivotTbl 来计算每个所有者的数量宠物。生成的 Excel 文件可以下载 here

我需要的:执行第 2 步的代码。换句话说,就是我用 Excel“手工”完成的代码数据透视表。

使用三个 table 创建新工作簿的代码:

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Table.PivotTable;

namespace EPPlusTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            const string outputFile = @"C:\temp\test.xlsx";
            if (System.IO.File.Exists(outputFile))
            {
                System.IO.File.Delete(outputFile);
            }
            var xls = new ExcelPackage(new FileInfo(outputFile));
            ExcelWorksheet ws = xls.Workbook.Worksheets.Add("Tables");

            // first table
            ws.Cells[1, 1].Value = "PersonId";
            ws.Cells[1, 2].Value = "PersonName";
            ws.Cells[1, 3].Value = "Age";

            ws.Cells[2, 1].Value = 1;
            ws.Cells[2, 2].Value = "John Doe";
            ws.Cells[2, 3].Value = 35;

            ws.Cells[3, 1].Value = 2;
            ws.Cells[3, 2].Value = "Jane Smith";
            ws.Cells[3, 3].Value = 40;
            ws.Tables.Add(ws.Cells[1, 1, 3, 3], "People");

            // second table
            ws.Cells[1, 5].Value = "PetId";
            ws.Cells[1, 6].Value = "PetName";
            ws.Cells[1, 7].Value = "Species";

            ws.Cells[2, 5].Value = 10;
            ws.Cells[2, 6].Value = "Spot";
            ws.Cells[2, 7].Value = "Dog";

            ws.Cells[3, 5].Value = 11;
            ws.Cells[3, 6].Value = "Fluffy";
            ws.Cells[3, 7].Value = "Cat";
            ws.Tables.Add(ws.Cells[1, 5, 3, 7], "Pets");

            // third table
            ws.Cells[1, 10].Value = "PersonId";
            ws.Cells[1, 11].Value = "PetId";

            ws.Cells[2, 10].Value = 1;
            ws.Cells[2, 11].Value = 10;

            ws.Cells[3, 10].Value = 2;
            ws.Cells[3, 11].Value = 10;
            
            ws.Cells[4, 10].Value = 1;
            ws.Cells[4, 11].Value = 11;
            ws.Tables.Add(ws.Cells[1, 10, 4, 11], "PeoplePets");

            ws.Cells.AutoFitColumns();

            // simple pivottable

            var ptws = xls.Workbook.Worksheets.Add("SimplePivotTable");
            var pt = ptws.PivotTables.Add(ptws.Cells[1, 1], (ExcelRangeBase)ws.Tables["PeoplePets"].Address, "SimplePivotTbl");
            var fld = pt.RowFields.Add(pt.Fields["PersonId"]);
            fld.Sort = eSortType.Ascending;
            fld = pt.ColumnFields.Add(pt.Fields["PetId"]);
            fld.Sort = eSortType.Ascending;
            var dfield = pt.DataFields.Add(pt.Fields["PetId"]);
            dfield.Function = DataFieldFunctions.Count;
            ptws.Cells.AutoFitColumns();

            xls.Save();
        }
    }
}

我在 EPPlus github 上以 an issue 的身份发布了此内容,得到的回复是:

No, there is not support for data models at this point. I'll add this as an enhancement for future versions.