Skip to content

Commit

Permalink
Merge pull request #87 from SyncfusionExamples/ExcelPivotTable
Browse files Browse the repository at this point in the history
Pivot Table in C#
  • Loading branch information
Mohan2401 authored May 6, 2024
2 parents c54ac5b + 78a75b0 commit 60e0c62
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Pivot Table/PivotTable/PivotTable.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34414.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PivotTable", "PivotTable\PivotTable.csproj", "{85B03C6F-4178-4C36-A9A2-87215CF528A9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{85B03C6F-4178-4C36-A9A2-87215CF528A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85B03C6F-4178-4C36-A9A2-87215CF528A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85B03C6F-4178-4C36-A9A2-87215CF528A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85B03C6F-4178-4C36-A9A2-87215CF528A9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {250B4B78-2430-43BD-A2F0-CA1F9939397E}
EndGlobalSection
EndGlobal
Binary file not shown.
14 changes: 14 additions & 0 deletions Pivot Table/PivotTable/PivotTable/PivotTable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="25.1.42" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions Pivot Table/PivotTable/PivotTable/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

using Syncfusion.XlsIO;

namespace PivotTable
{
class Program
{
public static void Main()
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
FileStream fileStream = new FileStream("../../../Data/SalesReport.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet worksheet = workbook.Worksheets[0];

IWorksheet pivotSheet = workbook.Worksheets.Create("PivotSheet");

//Create Pivot cache with the given data range
IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:H50"]);

//Create "PivotTable1" with the cache at the specified range
IPivotTable pivotTable = pivotSheet.PivotTables.Add("PivotTable1", pivotSheet["A1"], cache);

//Add Pivot table row fields
pivotTable.Fields[3].Axis = PivotAxisTypes.Row;
pivotTable.Fields[4].Axis = PivotAxisTypes.Row;

//Add Pivot table column fields
pivotTable.Fields[2].Axis = PivotAxisTypes.Column;

//Add data fields
IPivotField field = pivotTable.Fields[5];
pivotTable.DataFields.Add(field, "Units", PivotSubtotalTypes.Sum);

field = pivotTable.Fields[6];
pivotTable.DataFields.Add(field, "Unit Cost", PivotSubtotalTypes.Sum);

//Pivot table style
pivotTable.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium14;

pivotSheet.Activate();

string fileName = "PivotTable.xlsx";
//Saving the workbook as stream
FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(stream);
stream.Dispose();
}
}
}
}

0 comments on commit 60e0c62

Please sign in to comment.