简体   繁体   English

如何覆盖数据透视表(设置单元格)上的列标签文本?

[英]How can I override the column label text on a Pivot Table (Aspose Cells)?

I am using source data that contains values such as "10/1/2015", "11/1/2015" etc. 我正在使用包含“ 10/1/2015”,“ 11/1/2015”等值的源数据。

Understandably, when I add these values as a Column PivotFieldType: 可以理解,当我将这些值添加为Column PivotFieldType时:

pivotTable.AddFieldToArea(PivotFieldType.Column, MONTHYR_COLUMN);
pivotTable.ColumnHeaderCaption = "Months";

...the values in the columns correspond to those extracted from the raw data ("10/1/2015", "11/1/2015" etc.): ...列中的值对应于从原始数据中提取的值(“ 2015年10月1日”,“ 2015年11月1日”等):

在此处输入图片说明

However, rather than that textual representation ("10/1/2015", "11/1/2015" etc.), I want the labels to be "Oct 15", "Nov 15", etc. 但是,我希望标签不是“ Oct 15”,“ Nov 15”等,而是那种文本表示形式(“ 10/1/2015”,“ 11/1/2015”等)。

How can I accomplish that? 我该怎么做?

Will I have to change the data before writing it to the data sheet (from "10/1/2015" to "Oct 15", etc.) or is there a way I can interrupt the column-label-writing process on the Pivot Table? 在将数据写入数据表之前(从“ 2015年10月1日”到“十月15日”等),我是否必须更改数据?还是有办法中断Pivot上的列标签写入过程?表?

UPDATE 更新

It seems as if the code offered from the answer's link should work; 答案链接提供的代码似乎应该起作用; I can loop through it, and the values are right, but nothing changes. 我可以遍历它,值是正确的,但是没有任何变化。 This is my code: 这是我的代码:

// Get "10/1/2015" to display as "Oct 15"
Style columnStyle = new CellsFactory().CreateStyle();
columnStyle.Custom = "mmm yy";
CellArea columnRange = pivotTable.ColumnRange;
for (int c = columnRange.StartColumn; c < columnRange.EndColumn; c++)
{
    pivotTable.Format(columnRange.StartRow + 1, c, columnStyle);
}

UPDATE 2 更新2

Even this doesn't do anything - the value of C7 is still "10/1/2015" 即使这样也无济于事-C7的值仍为“ 10/1/2015”

Cell cell = pivotTableSheet.Cells["C7"];
cell.PutValue("Oct 15");

您可以使用诸如WorksheetFunction.TEXT(monthPortion & "/01/" & yearPortion , "mmm")公式替换GetMonthAsMMM函数。

I solved the problem by changing the source data from which the Pivot Table is generated, like so: 我通过更改生成数据透视表的源数据来解决了这个问题,如下所示:

private void AddPivotData(String ItemCode, String ItemDescription, String Unit, String MonthYear, int Quantity, Decimal TotalPrice, Boolean IsContractItem, Double PercentageOfTotal, Double MonthlyPercentage)
{
    . . .
    cell = sourceDataSheet.Cells[_lastRowAddedPivotTableData, 3];
    cell.PutValue(ConvertToMMMYY(MonthYear));
    . . .
}

// Comes in formatted YYYYMM (such as "201510"), returned as MMM YY (such as "Oct 15")
private object ConvertToMMMYY(string MonthYear)
{
    string yearPortion = MonthYear.Substring(0, 4);
    string monthPortion = MonthYear.Substring(4, 2);
    string yearSansCentury = yearPortion.Substring(2, 2);
    string monthNumAsStr = GetMonthAsMMM(monthPortion);
    return string.Format("{0}{1}", monthNumAsStr, yearSansCentury);
}

private string GetMonthAsMMM(string monthPortion)
{
    if (monthPortion == "01") return "Jan";
    if (monthPortion == "02") return "Feb";
    if (monthPortion == "03") return "Mar";
    if (monthPortion == "04") return "Apr";
    if (monthPortion == "05") return "May";
    if (monthPortion == "06") return "Jun";
    if (monthPortion == "07") return "Jul";
    if (monthPortion == "08") return "Aug";
    if (monthPortion == "09") return "Sep";
    if (monthPortion == "10") return "Oct";
    if (monthPortion == "11") return "Nov";
    if (monthPortion == "12") return "Dec";
    return "Unrecognized Month Num";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的数据字段放在 Excel 数据透视表(Aspose Cells)的 COLUMNS 中 - How can I place my data fields in COLUMNS in my Excel Pivot Table (Aspose Cells) 有没有一种方法可以冻结Aspose单元中的FreezePanes(在数据透视表上)? - Is there a way to FreezePanes (on a Pivot Table) in Aspose Cells? 如何按行和列字段值对Aspose中的数据透视表排序? - How to sort Pivot Table in Aspose by row and column field values? 如何在计算出的像元(Aspose Cells)中防止“#Div / 0!”? - How can I prevent “#Div/0!” in calculated cells (Aspose Cells)? 如何将电子表格中的列宽设置为特定的像素数(Aspose Cells)? - How can I set the column width in a spreadsheet to a specific pixel count (Aspose Cells)? 如何在电子表格范围(Aspose Cells)周围添加边框? - How can I add borders around a spreadsheet range (Aspose Cells)? 如何替换列中单元格中的文本? - How can I replace the text in cells in a column? 如何将数据透视表中的列除以 Excel 2013 中同一个数据透视表中的另一列 - How can I divide the column in a Pivot table by another column in the same PIVOT table in Excel 2013 如何在Excel 2010数据透视表中创建%方差列? - How can I create a % variance column in Excel 2010 pivot table? 数据透视表分组如何与 DAX 中的计算过滤器交互? 我该如何覆盖它们? - How do pivot table groupings interact with calculated filters in DAX? And how can I override them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM