在EPPlus

时间:2018-02-20 17:57:52

标签: c# excel pivot-table epplus

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

要做一些测试,我正在使用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'方法加载我的pivotTable,它接收3个参数(最后一个是我遇到问题的那个):

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

所以,我尝试将'OfficeOpenXml.Table.TableStyles。'和'OfficeOpenXml.Table.TableStyles。自定义'设置为dataRange变量,但没有数据透视表已更改,无论我尝试在TableStyle上设置何种样式,它们都保持 Medium9 样式。

PS。当我设置其他TableStyles

时,普通表的样式已更改

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

1 个答案:

答案 0 :(得分:1)

您所做的错误是更改TableStyles中的LoadFromCollection参数不应更改任何数据透视表,它应更改代表您的集合的普通表。

您会看到LoadFromCollection的输出是普通表而非数据透视。

要更改第一个数据透视表的样式,您只需:

 pivotTable1.TableStyle = TableStyles.Dark2;

您可以将Dark2更改为您希望的任何可用样式。