Skip to content

Commit a3825ec

Browse files
authored
Merge pull request #193 from SyncfusionExamples/959613-ColumnWidthPivotTableRange
959613-FAQ on how to set column width for a pivot table range in an Excel Document
2 parents 7e9e381 + abaf6a4 commit a3825ec

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Column width for pivot table range", "Column width for pivot table range\Column width for pivot table range.csproj", "{4228440B-8FDA-4DE2-9B55-3102212DCABC}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4228440B-8FDA-4DE2-9B55-3102212DCABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4228440B-8FDA-4DE2-9B55-3102212DCABC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4228440B-8FDA-4DE2-9B55-3102212DCABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4228440B-8FDA-4DE2-9B55-3102212DCABC}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Column_width_for_pivot_table_range</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
</Project>

FAQ/Column width for pivot table range/.NET/Column width for pivot table range/Column width for pivot table range/Output/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.XlsIO;
4+
using Syncfusion.XlsIO.Implementation.PivotTables;
5+
6+
namespace Column_Width_For_Pivot_Table_Range
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (ExcelEngine excelEngine = new ExcelEngine())
13+
{
14+
IApplication application = excelEngine.Excel;
15+
application.DefaultVersion = ExcelVersion.Xlsx;
16+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
17+
IWorkbook workbook = application.Workbooks.Open(inputStream);
18+
IWorksheet worksheet = workbook.Worksheets[0];
19+
IWorksheet worksheet1 = workbook.Worksheets[1];
20+
21+
//Create pivot cache with the given data range
22+
IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:H5"]);
23+
24+
//Create pivot table with the cache at the specified range
25+
IPivotTable pivotTable = worksheet1.PivotTables.Add("PivotTable1", worksheet1["A1"], cache);
26+
27+
PivotTableImpl pivotTableImpl = pivotTable as PivotTableImpl;
28+
29+
//Add Pivot table fields
30+
pivotTable.Fields[0].Axis = PivotAxisTypes.Row;
31+
pivotTable.Fields[1].Axis = PivotAxisTypes.Row;
32+
pivotTable.DataFields.Add(pivotTable.Fields["Total"], "Sum", PivotSubtotalTypes.Sum);
33+
34+
//Set column width
35+
worksheet1.Range["A10"].ColumnWidth = 50;
36+
37+
//Disable pivot table autoformat
38+
(pivotTable.Options as PivotTableOptions).IsAutoFormat = false;
39+
40+
#region Save
41+
//Saving the workbook
42+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
43+
workbook.SaveAs(outputStream);
44+
#endregion
45+
46+
//Dispose streams
47+
outputStream.Dispose();
48+
inputStream.Dispose();
49+
}
50+
}
51+
}
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# How to set column width for a pivot table range in an Excel Document?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.XlsIO;
15+
using Syncfusion.XlsIO.Implementation.PivotTables;
16+
```
17+
18+
Step 5: Include the below code snippet in **Program.cs** to set column width for a pivot table range in an Excel Document.
19+
20+
```csharp
21+
using (ExcelEngine excelEngine = new ExcelEngine())
22+
{
23+
IApplication application = excelEngine.Excel;
24+
application.DefaultVersion = ExcelVersion.Xlsx;
25+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
26+
IWorkbook workbook = application.Workbooks.Open(inputStream);
27+
IWorksheet worksheet = workbook.Worksheets[0];
28+
IWorksheet worksheet1 = workbook.Worksheets[1];
29+
30+
//Create pivot cache with the given data range
31+
IPivotCache cache = workbook.PivotCaches.Add(worksheet["A1:H5"]);
32+
33+
//Create pivot table with the cache at the specified range
34+
IPivotTable pivotTable = worksheet1.PivotTables.Add("PivotTable1", worksheet1["A1"], cache);
35+
36+
PivotTableImpl pivotTableImpl = pivotTable as PivotTableImpl;
37+
38+
//Add Pivot table fields
39+
pivotTable.Fields[0].Axis = PivotAxisTypes.Row;
40+
pivotTable.Fields[1].Axis = PivotAxisTypes.Row;
41+
pivotTable.DataFields.Add(pivotTable.Fields["Total"], "Sum", PivotSubtotalTypes.Sum);
42+
43+
//Set column width
44+
worksheet1.Range["A10"].ColumnWidth = 50;
45+
46+
//Disable pivot table autoformat
47+
(pivotTable.Options as PivotTableOptions).IsAutoFormat = false;
48+
49+
#region Save
50+
//Saving the workbook
51+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
52+
workbook.SaveAs(outputStream);
53+
#endregion
54+
55+
//Dispose streams
56+
outputStream.Dispose();
57+
inputStream.Dispose();
58+
}
59+
```

0 commit comments

Comments
 (0)