簡體   English   中英

如何在數據透視表 EPPlus 中應用條件格式

[英]How to apply conditional formatting in pivot table EPPlus

我有一個 ASP.NET Mvc 應用程序,它使用 EPPlus 生成 Excel 報告。 生成的工作簿中的工作表之一包含一個數據透視表。 我想在數據透視表中應用條件格式來為那些值等於 1 的單元格的背景顏色着色。

以下代碼用於創建數據透視表。

var dataRange = _objSheet.Cells[_objSheet.Dimension.Address.ToString()];
_objSheet = outputExcel.Workbook.Worksheets.Add("Pivot");
var ptTable = _objSheet.PivotTables.Add(_objSheet.Cells["A1"], dataRange, "PivotTable");
ptTable.GridDropZones = true;
ptTable.ApplyPatternFormats = true;
ptTable.Compact = false;
ptTable.CompactData = false;
ptTable.Indent = 0;
ptTable.RowGrandTotals = false;
ptTable.ColumnGrandTotals = false;
ptTable.ShowMemberPropertyTips = false;
ptTable.DataOnRows = false;
ptTable.UseAutoFormatting = false;

如何將條件格式應用於數據透視表的范圍?

它可以完成,但需要對 XML 進行修改,因為 epplus 目前似乎不支持ConditionalFormatting上的Pivot屬性:

https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.conditionalformatting.pivot?view=openxml-2.8.1

因此,如果您有一個數據透視表並添加了格式,則可以設置該標志,以便 Excel 將其與工作表中的數據透視表相關聯。 例如:

[TestMethod]
public void Pivot_Table_Conditional_Format()
{
    //https://stackoverflow.com/questions/59359688/how-to-apply-conditional-formatting-in-pivot-table-epplus
    //Throw in some data
    var dt = new DataTable("tblData");
    dt.Columns.AddRange(new[]
    {
        new DataColumn("Group", typeof (string)),
        new DataColumn("MValue", typeof (int)),
        new DataColumn("Month", typeof (int)),
        new DataColumn("String", typeof (object))
    });

    var rnd = new Random();

    for (var i = 0; i < 100; i++)
    {
        var row = dt.NewRow();
        //This adds some randomness to the number of groups that will be created
        row[0] = $"Group {rnd.Next(1, 100)}";
        row[1] = i * rnd.Next(1, 100);

        //This adds randomness to the columns so not guaranteed to be all 12
        row[2] = rnd.Next(1, 12);
        row[3] = Path.GetRandomFileName();

        dt.Rows.Add(row);
    }

    //Create a test file
    var fi = new FileInfo(@"c:\temp\Pivot_Table_Conditional_Format.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (var pck = new ExcelPackage(fi))
    {
        var wsData = pck.Workbook.Worksheets.Add("Data");
        wsData.Cells.LoadFromDataTable(dt, true);

        var wsPivot = pck.Workbook.Worksheets.Add("Pivot");

        var pivotTable1 = wsPivot.PivotTables.Add(
            wsPivot.Cells["A1"]
            , wsData.Cells[1, 1, wsData.Dimension.End.Row, wsData.Dimension.End.Column]
            , "DataPivot"
        );

        pivotTable1.DataFields.Add(pivotTable1.Fields["MValue"]);

        //Grouping will be by the "Group" column
        pivotTable1.RowFields.Add(pivotTable1.Fields["Group"]);

        //Columns will be months
        pivotTable1.ColumnFields.Add(pivotTable1.Fields["Month"]);

        //Set conditional formatting but have to determine the range in the pivot table
        var groups = dt
            .Rows
            .Cast<DataRow>()
            .Select(row => row["Group"])
            .Distinct()
            .ToList();

        var columns = dt
            .Rows
            .Cast<DataRow>()
            .Select(row => row["Month"])
            .Distinct()
            .ToList();

        var colOffset = pivotTable1.FirstDataCol;
        var groupOffset = pivotTable1.FirstDataRow + pivotTable1.FirstHeaderRow;

        var range = new ExcelAddress
        (
            pivotTable1.Address.Start.Row + groupOffset
            , pivotTable1.Address.Start.Column + colOffset
            , groups.Count + groupOffset
            , columns.Count + colOffset
        );

        var cond = wsPivot.ConditionalFormatting.AddGreaterThanOrEqual(range);
        cond.Formula = "100";
        cond.Style.Font.Color.Color = Color.Black;
        cond.Style.Fill.PatternType = ExcelFillStyle.Solid;
        cond.Style.Fill.BackgroundColor.Color = Color.Yellow;

        //Only way to set the pivot table as the target is with XML Hack
        var parent = cond.Node.ParentNode;
        var doc = parent.OwnerDocument;

        //Need an attribute "pivot" with a value of "1" (true)
        //https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.conditionalformatting.pivot?view=openxml-2.8.1
        var att = doc.CreateAttribute("pivot", doc.NamespaceURI);
        att.Value = "1";
        parent.Attributes.Append(att);

        pck.Save();
    }
}

這給出了:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM