@Shivammirje1998,
Please use the PivotTable.IsAutoFormat to control the “AutoFit columns widths on update” option.
The sample code is as follows:
Workbook workbook = new Workbook(filePath + “Sample.xlsx”);
Worksheet sheet = workbook.Worksheets.Add(“NewSheet”);
PivotTableCollection pivotTables = sheet.PivotTables;
// Adding a PivotTable to the worksheet
int index = pivotTables.Add("=Masterfile!$A$1:$D$8", “A1”, “PivotTable3”);
// Accessing the instance of the newly added PivotTable
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 0);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Page, 1);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 3);
//Add the following code to move PivotTable.DataField to column area
if (pivotTable.DataField != null)
{
pivotTable.AddFieldToArea(PivotFieldType.Column, pivotTable.DataField);
}
pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;
pivotTable.DataFields[0].NumberFormat = “0.0,”;
pivotTable.DataFields[1].NumberFormat = “0.0,”;
#region sort pivot table data columns in descending order
PivotField currRowField = pivotTable.RowFields[0];
currRowField.IsAutoSort = true;
currRowField.IsAscendSort = false;
//Sort PivotField in row area via DataField named “Quantity”;
currRowField.AutoSortField = 0;
#endregion
#region add conditional foramts
int formatIndex = pivotTable.PivotFormatConditions.Add();
PivotFormatCondition pfc = pivotTable.PivotFormatConditions[formatIndex];
//Set conditional styles only in the DataField named “Quantity”
pfc.AddDataAreaCondition(pivotTable.DataFields[0]);
pfc.SetConditionalAreas();
FormatConditionCollection fcc = pfc.FormatConditions;
int idx = fcc.AddCondition(FormatConditionType.CellValue);
FormatCondition fc = fcc[idx];
fc.Formula1 = “3000”;
fc.Operator = OperatorType.GreaterOrEqual;
fc.Style.BackgroundColor = Color.Red;
#endregion
//add this line to control the “autofit column width on update” option
pivotTable.IsAutoFormat = false;
CellArea area = pivotTable.RowRange;
int start = area.StartColumn;
sheet.Cells.SetColumnWidthPixel(start, 220);
// Saving the Excel file
workbook.Save(filePath + “sample_out.xlsx”);