[英]How can I place my data fields in COLUMNS in my Excel 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";
...列中的值對應於從原始數據中提取的值(“ 2015年10月1日”,“ 2015年11月1日”等):
但是,我希望標簽不是“ Oct 15”,“ Nov 15”等,而是那種文本表示形式(“ 10/1/2015”,“ 11/1/2015”等)。
我該怎么做?
在將數據寫入數據表之前(從“ 2015年10月1日”到“十月15日”等),我是否必須更改數據?還是有辦法中斷Pivot上的列標簽寫入過程?表?
答案鏈接提供的代碼似乎應該起作用; 我可以遍歷它,值是正確的,但是沒有任何變化。 這是我的代碼:
// 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);
}
即使這樣也無濟於事-C7的值仍為“ 10/1/2015”
Cell cell = pivotTableSheet.Cells["C7"];
cell.PutValue("Oct 15");
您可以使用諸如WorksheetFunction.TEXT(monthPortion & "/01/" & yearPortion , "mmm")
公式替換GetMonthAsMMM函數。
我通過更改生成數據透視表的源數據來解決了這個問題,如下所示:
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.