如何更改 EPPlus 中 ColumnField 列标题的格式?

How can I change the format of the ColumnField column headings in EPPlus?

我在 EPPlus 中创建了一个列字段,如下所示:

// Column field[s]
var monthYrColField = pivotTable.Fields["MonthYr"];
pivotTable.ColumnFields.Add(monthYrColField);

...显示如下(“201509”和“201510”列):

我希望这些值显示为 "Sep 15" 和 "Oct 15"

在 Excel Interop 中,它是这样完成的:

var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
monthField.NumberFormat = "MMM yy";

...但在 EPPlus 中,相应的变量 (monthYrColField) 没有 "NumberFormat"(或 "Style")成员。

我试过这个:

pivotTableWorksheet.Column(2).Style.Numberformat.Format = "MMM yy";

...但是,虽然它没有抱怨或造成严重破坏,但也没有更改“201509”和“201510”的值

如何将 EPPlus 中 ColumnField 列标题的格式从 "untransformed" 更改为 "MMM yy" 格式?

更新

对于 VDWWD:

正如您在评论中看到的那样,有很多与数据透视表相关的东西在 EPPlus 中不起作用或很难起作用; Excel 与 EPPlus 相比,Interop 是一只熊(而不是泰迪熊或考拉,更像是灰熊),但对于数据透视表,EPPlus 似乎 half-baked 与 Exterop 的 fried-to-a-crispness.

private void PopulatePivotTableSheet()
{
    string NORTHWEST_CORNER_OF_PIVOT_TABLE = "A6";
    AddPrePivotTableDataToPivotTableSheet();
    var dataRange = pivotDataWorksheet.Cells[pivotDataWorksheet.Dimension.Address];
    dataRange.AutoFitColumns();
    var pivotTable = pivotTableWorksheet.PivotTables.Add(
                        pivotTableWorksheet.Cells[NORTHWEST_CORNER_OF_PIVOT_TABLE], 
                        dataRange, 
                        "PivotTable");
    pivotTable.MultipleFieldFilters = true;
    pivotTable.GridDropZones = false;
    pivotTable.Outline = false;
    pivotTable.OutlineData = false;
    pivotTable.ShowError = true;
    pivotTable.ErrorCaption = "[error]";
    pivotTable.ShowHeaders = true;
    pivotTable.UseAutoFormatting = true;
    pivotTable.ApplyWidthHeightFormats = true;
    pivotTable.ShowDrill = true;

    // Row field[s]
    var descRowField = pivotTable.Fields["Description"];
    pivotTable.RowFields.Add(descRowField);

    // Column field[s]
    var monthYrColField = pivotTable.Fields["MonthYr"];
    pivotTable.ColumnFields.Add(monthYrColField);

    // Data field[s]
    var totQtyField = pivotTable.Fields["TotalQty"];
    pivotTable.DataFields.Add(totQtyField);

    var totPriceField = pivotTable.Fields["TotalPrice"];
    pivotTable.DataFields.Add(totPriceField);

    // Don't know how to calc these vals here, so had to put them on the data sheet
    var avgPriceField = pivotTable.Fields["AvgPrice"];
    pivotTable.DataFields.Add(avgPriceField);

    var prcntgOfTotalField = pivotTable.Fields["PrcntgOfTotal"];
    pivotTable.DataFields.Add(prcntgOfTotalField);

    // TODO: Get the sorting (by sales, descending) working:
    // These two lines don't seem that they would do so, but they do result in the items 
    // being sorted by (grand) total purchases descending
    //var fld = ((PivotField)pvt.PivotFields("Description"));
    //fld.AutoSort(2, "Total Purchases");
    //int dataCnt = pivotTable.ra //DataBodyRange.Columns.Count + 1;

    FormatPivotTable();
}

private void FormatPivotTable()
{
    int HEADER_ROW = 7;

    if (DateTimeFormatInfo.CurrentInfo != null)
        pivotTableWorksheet.Column(2).Style.Numberformat.Format = 
            DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
    // Pivot Table Header Row - bold and increase height
    using (var headerRowFirstCell = pivotTableWorksheet.Cells[HEADER_ROW, 1])
    {
        headerRowFirstCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
        headerRowFirstCell.Style.Font.Bold = true;
        headerRowFirstCell.Style.Font.Size = 12;
        pivotTableWorksheet.Row(HEADER_ROW).Height = 25;
    }

    ColorizeContractItemBlocks(contractItemDescs);
    // TODO: Why is the hiding not working?
    HideItemsWithFewerThan1PercentOfSales();
}

您可以使用内置日期格式YearMonthPattern。这将给出 september 2016 作为格式。

pivotTableWorksheet.Column(2).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;

如果你真的想要MMM yy作为模式,你需要覆盖文化格式:

Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL")
{
    DateTimeFormat = { YearMonthPattern = "MMM yy" }
};
pivotTableWorksheet.Column(2).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;

您似乎无法在字段本身上设置格式。您必须通过枢轴 table 对象访问:

pivotTable.DataFields[0].Format = "MMM yy";

应用到基础工作表的任何格式似乎都被完全忽略了。