问题
I was taught how to convert a date-as-string value that was being converted from what I wanted ("Sep 2015", "Oct 2015" etc.) to what Excel thought it should be ("15-Sep", "15-Oct" etc.) here.
When it was displaying "badly," the columns at least displayed in the right order ("2015-Sep" followed by "2015-Oct"). Now that they are "Sep 15" and "Oct 15", though, they are displaying out of "natural" order and in alphabetical order ("Oct 15" followed by "Sep 15").
This is the too-typical scenario (especially egregiously evident in software development) of the solving of one problem causing another one to rear its ugly rear.
This is how I create the "month" part of the PivotTable:
var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
Before fixing the display format problem:
After fixing the display format problem ("15-Sep" is now "Sep 15", etc., but the months are now out of order):
Can I "have my cake and eat it, too" so to speak? If so, how?
回答1:
From reading the comments, it sounds like you need to convert your C# date to an excel date. In the second comment you mention that you were able to get values "201509" and "201510" onto an excel sheet.
I suggest you separate the year and month using the LEFT()
and RIGHT()
functions, then use the DATE()
function to get the Excel serial number for 9/1/15 and 10/1/15.
Here's a screenshot of the steps I'm think of (Happy Halloween!):
Finally, you can now format the Serial number using your formula monthField.NumberFormat = "MMM yy"
. Excel will realize this is a date and sort it chronologically.
来源:https://stackoverflow.com/questions/40050213/how-can-i-get-the-members-of-a-pivottable-column-field-to-display-in-natural-ord