Hi Clay,
int sheetIndex = wb.Worksheets.Add();
PivotTableCollection pts = wb.Worksheets[sheetIndex].PivotTables;
int ptIndex = pts.Add("=PivotData!$A$1:$J$415", “A5”, “testPt”);
PivotTable pt = pts[ptIndex];
pt.AddFieldToArea(PivotFieldType.Row, “Description”);
pt.AddFieldToArea(PivotFieldType.Column, “MonthYr”);
pt.AddFieldToArea(PivotFieldType.Data, “TotalQty”);
pt.DataFields[0].DisplayName = “Total Packages”;
pt.AddFieldToArea(PivotFieldType.Data, “TotalPrice”);
pt.DataFields[1].DisplayName = “Total Purchases”;
pt.AddCalculatedField(“Average Price”, “=TotalPrice/TotalQty”, true);
pt.AddCalculatedField(“PercentOfTotal”, “=TotalPrice”, true);
pt.DataFields[3].DisplayName = “Percentage of Total”;
pt.DataFields[3].DataDisplayFormat = PivotFieldDataDisplayFormat.PercentageOfColumn;
pt.RowFields[0].IsAutoSubtotals = false;
pt.RowFields[0].ShowInOutlineForm = true;
pt.RowFields[0].ShowCompact = true;
PivotField field = pt.RowFields[0];
field.IsAutoSort = true;
field.IsAscendSort = false;
field.AutoSortField = 1;
pt.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;
pt.RefreshDataFlag = true;
pt.RefreshData();
pt.CalculateData();
pt.RefreshDataFlag = false;
Style columnStyle = new CellsFactory().CreateStyle();
columnStyle.Custom = “mmm yy”;
CellArea columnRange = pt.ColumnRange;
for (int c = columnRange.StartColumn; c < columnRange.EndColumn; c++)
{
pt.Format(columnRange.StartRow + 1, c, columnStyle);
}
wb.Save(dir + “out.xlsx”);
Thanks - awesome work!
Hi,
Hi,
Still doesn’t work; even when I tried to “brute force” a value in a particular cell, it didn’t work - it still says “10/1/15” in C7 even with the following code:
// made no difference
//pivotTable.RefreshDataFlag = true;
//pivotTable.RefreshData();
//pivotTable.CalculateData();
//pivotTable.RefreshDataFlag = false;
//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);
//}
Cell cell = pivotTableSheet.Cells[“C7”];
cell.PutValue(“Oct 15”);
Hi,
I am attaching an Excel spreadsheet that shows this problem, and the code for generating the spreadsheet, too.
I bit the bullet and changed the source data (this works):
Hi,
- CELLSNET-44957 - Unable to change the value of cell C7 which is inside the PivotTable
I was able to work around it with this:
{
. . .
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”;
}
Hi,