-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from SyncfusionExamples/ExcelPivotTable
Pivot Table in C#
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |