2

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

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

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

在此处输入图像描述

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

在 Excel 互操作中,它是这样完成的:

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”的vals

如何将 EPPlus 中 ColumnField 列标题的格式从“未转换”更改为“MMM yy”格式?

更新

对于 VDWWD:

正如您在评论中看到的,有很多与数据透视表相关的东西在 EPPlus 中不起作用或难以使用;与 EPPlus 相比,Excel Interop 是一只熊(而不是泰迪熊或考拉,但更像是一只灰熊),但对于数据透视表,与 Exterop 的炸得酥脆的相比,EPPlus 似乎有点半生不熟.

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();
}
4

2 回答 2

2

您可以使用内置日期格式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;
于 2016-10-28T13:44:54.303 回答
1

您似乎无法在字段本身上设置格式。您必须通过数据透视表对象访问:

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

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

于 2018-10-16T17:34:52.007 回答