How to set custom format on data fields of a Pivot Table?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files. Using this library, you can set custom formats on data fields of a pivot table.
Steps to set custom formats on data fields of a pivot table programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application.
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
Install the NuGet package.
Step 3: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using Syncfusion.XlsIO.Implementation.PivotTables;
VB.NET
Imports Syncfusion.XlsIO Imports Syncfusion.XlsIO.Implementation.PivotTables
Step 4: Add the following code snippet to set a custom format on data fields of a pivot table using C# and VB.NET.
C#
// Create an instance of ExcelEngine
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Instantiate the Excel application object
IApplication application = excelEngine.Excel;
// Open a workbook with a pivot table
IWorkbook workbook = application.Workbooks.Open("PivotTable.xlsx");
// Access the worksheet that contains the pivot table
IWorksheet worksheet = workbook.Worksheets[1];
// Access the pivot table
IPivotTable pivotTable = worksheet.PivotTables[0];
PivotTableImpl pivotTableImpl = pivotTable as PivotTableImpl;
// Set the custom number format for a data field
pivotTableImpl.DataFields[0].Field.NumberFormat = "###.00";
// Save the workbook
workbook.SaveAs("Output.xlsx");
}
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Open a workbook with a pivot table Dim workbook As IWorkbook = application.Workbooks.Open("PivotTable.xlsx") 'Access the worksheet that contains the pivot table Dim worksheet As IWorksheet = workbook.Worksheets(1) 'Access the pivot table Dim pivotTable As IPivotTable = worksheet.PivotTables(0) Dim pivotTableImpl As PivotTableImpl = TryCast(pivotTable, PivotTableImpl) 'Set the custom number format for a data field pivotTableImpl.DataFields(0).Field.NumberFormat = "###.00" 'Save the workbook workbook.SaveAs("Output.xlsx") End Using
A complete working example to set a custom format on data fields of a pivot table can be downloaded from SetCustomFormatOnPivotDataFields.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
An online sample link to generate an Excel file.
See Also:
Create Pivot Table in C#, VB.NET
Sort Excel pivot table data by row and column fields in C# using XlsIO
Create a pivot table in an Excel workbook with data exported using template markers
Format Excel cell with Accounting number format in C#, VB.NET
How to get the formatting Date value based on the culture
Format Excel chart data label in C#, VB.NET
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a in your projects. Refer to this link to learn about generating and registering a Syncfusion in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to a set a custom format on data fields of a Pivot Table.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!