为什么某些数据透视表标签显示错误的年份?

Why are some of the PivotTable labels showing the wrong year?

我有一些数据的旧视图,如下所示 - 请注意,9 月到 12 月是 2015 年,而不是 2016 年:

不过,此数据的数据透视表版本显示 所有 年份为 2016 年:

...因此也对它们进行了错误排序,因为 year-confusion.

,Sept-Dec 在 一月和二月之后

数据透视表是根据另一个 sheet 的源数据生成的,该数据透视表与 "legacy" sheet 具有相同的数据(Sept-Dec = 2015 年,1 月和 2 月 = 2016):

日期值从源数据添加到数据透视表,如下所示:

var pch = _xlBook.PivotCaches();
int pivotDataRowsUsed = xlBook.Worksheets["PivotData"].UsedRange.Rows.Count;
int pivotDataColsUsed = 
    xlBook.Worksheets["PivotData"].UsedRange.Columns.Count;
string lastColWrittenAsAlpha 
    ReportRunnerConstsAndUtils.GetExcelColumnName(pivotDataColsUsed);
string endRange = string.Format("{0}{1}", lastColWrittenAsAlpha
    pivotDataRowsUsed);

Range sourceData = _xlBook.Worksheets["PivotData"].Range[string.Format("A1:{0}", endRange)];

PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A6"], "PivotTable");
. . .
var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
monthField.NumberFormat = "mmm yyyy";
monthField.DataRange.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);

pvt.CompactLayoutColumnHeader = "Months";

上面看到的 Sept、Oct 和 Nov 行都是“15-”;所有的一月和二月都是“16-”

为什么日期标签在所有情况下都显示“16”,而不是有时显示“15”,我怎样才能让它们正确显示?

更新

更糟糕的是当我 运行 典型的 13 个月的报告,因此有两个相同的月份(但来自两个不同的年份)- 它显示它们是同一年,即使数据 不同:

更新 2

越来越好奇 - 像这样使用 NumberFormat "dd mmm":

//monthField.NumberFormat = "mmm yyyy"; with this, they all show one year, even when the range spans two years
//monthField.NumberFormat = "dd mmm yyyy"; this shows a combination, such as "16 Jan 2016" and "15 Sep 2016"
monthField.NumberFormat = "dd mmm";

...确实给了我不同的年份,但它们的顺序错了(16 在 15 之前):

更新 3

更奇怪的是,当我生成一份 13 个月的报告时,因此有两个相同的月份(但年份不同);在这种情况下,2016 年的月份是有序的,然后是 2015 年的月份,和以前一样,除了双倍的月份,其中 2015 年正确但奇怪(基于月份标签的其余部分的行为)排在第一位:

更新 4

我已经尝试了以下所有组合,但都以某种方式失败了:

向数据透视表使用的源 sheet 提供数据 - 格式为 "YY-MMM"(如“15-Sep”)或 "YYYY-MMM"(如“2015-Sep”) :

monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(MonthYear);

-或:

string _prependedCentury = "20" + MonthYear;
monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(_prependedCentury); //MonthYear);

然后应用各种 NumberFormats:

//monthField.NumberFormat = "MMM yy";
monthField.NumberFormat = "MMM yyyy";
//monthField.NumberFormat = "yyyy MMM";
//monthField.NumberFormat = "yyyy-MMM";
//monthField.NumberFormat = "dd mmm"; 

一定有某种数据格式和 NumberFormat 的组合 属性 可以正确地以正确的顺序显示值,但我还没有想到。

更新 5

如果源数据的格式为 YYYYMM(例如“201509”和“201510”)并且数据透视表 table 使用的 NumberFormat 为 "MMM yy",则两列(2015 年 9 月和2015 年 10 月)显示为 "Sep 51".

这有效:

已将源数据 YYYYMM 值转换为 MM/1/YYYY:

// These come in as "YYYYMM" values (such as "201509"); need them to be 9/1/2016 and suchlike
private string ConvertToMMDDYYYY(string monthYear)
{
    string YYYY = monthYear.Substring(0, 4);
    string MM = monthYear.Substring(4, 2);
    return string.Format("{0}/1/{1}", MM, YYYY);
}

以这种方式在数据透视表上使用它们:

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