如何覆盖 Pivot Table (Aspose Cells) 上的列标签文本?
How can I override the column label text on a Pivot Table (Aspose Cells)?
我使用的源数据包含“10/1/2015”、“11/1/2015”等值
可以理解,当我将这些值添加为 Column PivotFieldType 时:
pivotTable.AddFieldToArea(PivotFieldType.Column, MONTHYR_COLUMN);
pivotTable.ColumnHeaderCaption = "Months";
...列中的值对应于从原始数据中提取的值(“10/1/2015”、“11/1/2015”等):
但是,我希望标签为 "Oct 15"、"Nov 15",而不是文本表示(“10/1/2015”、“11/1/2015”等)等等
我怎样才能做到这一点?
我是否必须在将数据写入数据 sheet 之前更改数据(从“10/1/2015”到 "Oct 15",等等)或者有什么方法可以中断Pivot 上的列标签写入过程 Table?
更新
似乎答案 link 提供的代码应该可以工作;我可以遍历它,值是正确的,但没有任何变化。这是我的代码:
// 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);
}
更新 2
即使这样也没有任何作用 - C7 的值仍然是“10/1/2015”
Cell cell = pivotTableSheet.Cells["C7"];
cell.PutValue("Oct 15");
我通过更改生成 Pivot Table 的源数据解决了这个问题,如下所示:
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";
}
您可以用 WorksheetFunction.TEXT(monthPortion & "/01/" & yearPortion , "mmm")
等公式替换 GetMonthAsMMM 函数
我使用的源数据包含“10/1/2015”、“11/1/2015”等值
可以理解,当我将这些值添加为 Column PivotFieldType 时:
pivotTable.AddFieldToArea(PivotFieldType.Column, MONTHYR_COLUMN);
pivotTable.ColumnHeaderCaption = "Months";
...列中的值对应于从原始数据中提取的值(“10/1/2015”、“11/1/2015”等):
但是,我希望标签为 "Oct 15"、"Nov 15",而不是文本表示(“10/1/2015”、“11/1/2015”等)等等
我怎样才能做到这一点?
我是否必须在将数据写入数据 sheet 之前更改数据(从“10/1/2015”到 "Oct 15",等等)或者有什么方法可以中断Pivot 上的列标签写入过程 Table?
更新
似乎答案 link 提供的代码应该可以工作;我可以遍历它,值是正确的,但没有任何变化。这是我的代码:
// 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);
}
更新 2
即使这样也没有任何作用 - C7 的值仍然是“10/1/2015”
Cell cell = pivotTableSheet.Cells["C7"];
cell.PutValue("Oct 15");
我通过更改生成 Pivot Table 的源数据解决了这个问题,如下所示:
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";
}
您可以用 WorksheetFunction.TEXT(monthPortion & "/01/" & yearPortion , "mmm")