Setting a PivotTables style the the inbuilt ones in Excel2007/2010

Does anyone have any example code on how to set a PivotTable to one of the inbuilt styles in Excel2007/2010?

I’ve created my PivotTable, have it on a seperate Worksheet, no problems whatsoever.

However I just want to be able to set the PivotTable to the same style as, for example, “Pivot Style Medium 2” (it’s a nice blue one), but just can’t get this to work at all!

Any examples/help really would be apprecaited.


Hi,

Please see the code below. It reads a PivotTable from existing file and then format it as Pivot Style Medium 2.

Please see the source and output workbooks as well as screenshot.

C#


string path = @“F:\Shak-Data-RW\Downloads\pivotTable.xlsx”;

Workbook workbook = new Workbook(path);

Worksheet worksheet = workbook.Worksheets[1];

PivotTable pvtTable = worksheet.PivotTables[0];

pvtTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleMedium2;

workbook.Save(path + “.out.xlsx”, SaveFormat.Xlsx);

Screenshot:

That works, although the code was identical to what I was doing, I wasn’t actually setting the SaveFormat to xlsx.

Thank you for highlighting the issue.

Hi,

It’s great, the problem was sorted out easily. You are right. For xls [Excel 2003] format, you need to use PivotTable.AutoFormatType and PivotTableAutoFormatType enumeration