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

我已经看到了一些这样的数据的遗留观点 – 注意到9月到12月是2015年,而不是2016年:

在这里输入图像说明

尽pipe如此,数据的数据透视表显示所有年份都是在2016年:

在这里输入图像说明

…也因此而产生误解,因为今年1月份和2月份之后的 9月 – 12月会出现混乱。

数据透视表是从另一张具有与“旧式”表(2011年9月 – 12月= 2015年1月和2月= 2016年)相同数据的表中的源数据生成的:

在这里输入图像描述

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

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"; 

上面的9月,10月和11月行都是“15”; 1月和2月都是“16”

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

UPDATE

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

在这里输入图像描述

更新2

Curiouser和curiouser – 使用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"; 

…给了我不同的年份,但是他们被错误地命令了(15岁以前的16岁):

在这里输入图像说明

更新3

即使是诡异的时候,我发出了一个13个月的报告,因此有两个相同的月份(但不同年份); 在这种情况下,2016年的月份和之前一样,2015年的月份与之前一样,除了2015年正确但奇怪的月份(基于其余月份标签的行为)的第一个月:

在这里输入图像说明

更新4

我已经尝试了以下的每一个组合,并且都以这样或那样的方式失败:

向数据透视表使用的数据源提供数据 – 格式为“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属性的组合,才能以正确的顺序正确显示值,但是我还没有find它。

更新5

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

这工作:

将源数据YYYYMM vals转换为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";