问题
I've got a row field that allows the user to filter rows:
pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField;
...and a column field that allows the user to filter columns:
var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
...but when the PivotTable is generated, it displays "Row Labels" where I want it to say "Description" and "Column Labels" where I want it to say "Month":
How can I change this default/plain vanilla verbiage to my custom label text?
A little more context, showing more code:
PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A6"], "PivotTable");
pvt.MergeLabels = true; // The only thing I noticed this doing was centering the heading labels
pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField;
var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;
monthField.DataRange.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
I have the same problem with the "YYYYMM" column header labels, such as "201509" and "201510"; I want these to instead be "Sep 2015" and "Oct 2015" (etc.). It's true that this is the literal value contained in the source data ("201509" and "201510", etc.) but I'm wondering if there's a way to massage those values here without too much bother.
There must be a way to specify a value for these labels, or at least not show them, but which property on which object is it that controls these?
回答1:
I could be off my rocker, but one way to do this is to simply overwrite the values in those cells. They should stick, even as you modify the pivot table.
_xlPivotTableSheet.Range["A7"].Value2 = "Description";
_xlPivotTableSheet.Range["B6"].Value2 = "Months";
I think the more proper way, however is to change the "Compact Layout" header properties:
pvt.CompactLayoutColumnHeader = "Months";
pvt.CompactLayoutRowHeader = "Description";
As far as your month headers, if you're okay with them not being actual dates and just rendering as text, you can always set the NumberFormat
property of the actual source data to "@"
, which is text.
-- EDIT --
If the source data is actual Date-Dates, you have two options. The best option is to keep it as a date, that way sorting and such will work the way you expect (otherwise "Aug 15" appears to be before "Jul 15"). The way you do this is to change the NumberFormat
of your PivotField
:
monthField.NumberFormat = "mmm yyyy";
otherwise known as:
pvt.PivotFields("MonthYr").NumberFormat = "mmm yyyy";
This should format each of the headers the way you described.
The other way to do it is to render your date as text in a new column (from your source data), but that sounds less ideal if the method above meets your needs. The only reason I can see doing this is if you really don't want it as a date behind the scenes, for whatever reason. The formula for the new column would be something like:
=Text("<source cell>", "mmm yyyy")
来源:https://stackoverflow.com/questions/40031858/how-can-i-change-the-text-of-the-automatically-added-labels-on-these-excel-inter