在 EPPlus 上使用自定义 TableStyles 导出数据透视表

Export PivotTable with custom TableStyles on EPPlus

我正在使用 EPPlus 导出数据透视表 table。我能够正确导出所有数据,现在我正尝试在 excel 上为我的枢轴 table 设置自定义样式,但我无法为其设置不同的样式。

为了做一些测试,我使用 GitHub Project 上可用的 'Sample12.cs' EPPlus 的代码:

    public static string RunSample12(string sqlServerName, DirectoryInfo outputDir)
    {
        var list = new List<SalesDTO>();
        if (sqlServerName == "")
        {
            list = GetRandomData();
        }
        else
        {
            list = GetDataFromSQL(sqlServerName);
        }

        string file = outputDir.FullName + @"\sample12.xlsx";
        if (File.Exists(file)) File.Delete(file);
        FileInfo newFile = new FileInfo(file);

        using (ExcelPackage pck = new ExcelPackage(newFile))
        {
            // get the handle to the existing worksheet
            var wsData = pck.Workbook.Worksheets.Add("SalesData");

            var dataRange = wsData.Cells["A1"].LoadFromCollection
                (
                from s in list 
                orderby s.LastName, s.FirstName 
                select s, 
               true, OfficeOpenXml.Table.TableStyles.None);                

            wsData.Cells[2, 6, dataRange.End.Row, 6].Style.Numberformat.Format = "mm-dd-yy";
            wsData.Cells[2, 7, dataRange.End.Row, 11].Style.Numberformat.Format = "#,##0";

            dataRange.AutoFitColumns();

            var wsPivot = pck.Workbook.Worksheets.Add("PivotSimple");
            var pivotTable1 = wsPivot.PivotTables.Add(wsPivot.Cells["A1"], dataRange, "PerEmploee");

            pivotTable1.RowFields.Add(pivotTable1.Fields[4]);
            var dataField = pivotTable1.DataFields.Add(pivotTable1.Fields[6]);
            dataField.Format="#,##0";
            pivotTable1.DataOnRows = true;

            var chart = wsPivot.Drawings.AddChart("PivotChart", eChartType.Pie, pivotTable1);
            chart.SetPosition(1, 0, 4, 0);
            chart.SetSize(600, 400);

            var wsPivot2 = pck.Workbook.Worksheets.Add("PivotDateGrp");     
            var pivotTable2 = wsPivot2.PivotTables.Add(wsPivot2.Cells["A3"], dataRange, "PerEmploeeAndQuarter");

            pivotTable2.RowFields.Add(pivotTable2.Fields["Name"]);


            //Add a rowfield
            var rowField = pivotTable2.RowFields.Add(pivotTable2.Fields["OrderDate"]);
            //This is a date field so we want to group by Years and quaters. This will create one additional field for years.
            rowField.AddDateGrouping(eDateGroupBy.Years | eDateGroupBy.Quarters);
            //Get the Quaters field and change the texts
            var quaterField = pivotTable2.Fields.GetDateGroupField(eDateGroupBy.Quarters);
            quaterField.Items[0].Text = "<"; //Values below min date, but we use auto so its not used
            quaterField.Items[1].Text = "Q1";
            quaterField.Items[2].Text = "Q2";
            quaterField.Items[3].Text = "Q3";
            quaterField.Items[4].Text = "Q4";
            quaterField.Items[5].Text = ">"; //Values above max date, but we use auto so its not used

            //Add a pagefield
            var pageField = pivotTable2.PageFields.Add(pivotTable2.Fields["Title"]);

            //Add the data fields and format them
            dataField = pivotTable2.DataFields.Add(pivotTable2.Fields["SubTotal"]);
            dataField.Format = "#,##0";
            dataField = pivotTable2.DataFields.Add(pivotTable2.Fields["Tax"]);
            dataField.Format = "#,##0";
            dataField = pivotTable2.DataFields.Add(pivotTable2.Fields["Freight"]);
            dataField.Format = "#,##0";

            //We want the datafields to appear in columns
            pivotTable2.DataOnRows = false;

            pck.Save();
        }
        return file;
    }

我正在使用 'LoadFromCollection' 方法加载我的数据透视表,它接收 3 个参数(最后一个是我遇到问题的那个):

public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle)

所以,我尝试将 'OfficeOpenXml.Table.TableStyles.None' 和 'OfficeOpenXml.Table.TableStyles.Custom' 都设置为 dataRange 变量,但是 PivotTables 的 none 改变了,它们都保持 Medium9 样式,无论我尝试在 TableStyle 上设置什么样式。

PS。当我设置其他 TableStyles

时,普通 tables 的样式发生了变化

有谁知道是否无法使用 EPPlus 更改数据透视表的样式并且默认是 Medium9 主题?如果可能的话,我做错了什么?

你做错的是改变 LoadFromCollection 中的 TableStyles 参数不应该改变你的任何枢轴 tables,它应该改变正常的 table代表你的 collection.

您看到 LoadFromCollection 的输出是正常的 table 而不是主元。

要更改第一个枢轴的样式 table,您需要做的就是:

 pivotTable1.TableStyle = TableStyles.Dark2;

在这里您可以将 Dark2 更改为您想要的任何可用样式。