|
| 1 | +--- |
| 2 | +title: Identify the end column of a PivotTable in Excel | Syncfusion |
| 3 | +description: Code example to identify the end column of a PivotTable in an Excel workbook using the Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to identify the end column of a PivotTable in Excel? |
| 10 | + |
| 11 | +The following code examples demonstrate how to identify the end column of a PivotTable in an Excel workbook using C# (Cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Pivot%20Tables%20End%20Column/.NET/End%20Column/End%20Column/Program.cs,180" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + |
| 20 | + // Open workbook (update path as needed) |
| 21 | + IWorkbook workbook = application.Workbooks.Open("PivotTable.xlsx"); |
| 22 | + |
| 23 | + // Get the first pivot table |
| 24 | + IPivotTable pivotTable = workbook.Worksheets[0].PivotTables[0]; |
| 25 | + |
| 26 | + // Ensure layout is calculated |
| 27 | + pivotTable.Layout(); |
| 28 | + |
| 29 | + // Read EndLocation from the implementation type |
| 30 | + IRange endRange = (pivotTable as Syncfusion.XlsIO.Implementation.PivotTables.PivotTableImpl).EndLocation; |
| 31 | + int lastColumn = endRange.LastColumn; |
| 32 | + |
| 33 | + // Use lastColumn as needed (e.g., log) |
| 34 | + Console.WriteLine("PivotTable last column: " + lastColumn); |
| 35 | +} |
| 36 | +{% endhighlight %} |
| 37 | + |
| 38 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 39 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 40 | +{ |
| 41 | + IApplication application = excelEngine.Excel; |
| 42 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 43 | + |
| 44 | + // Open workbook (update path as needed) |
| 45 | + IWorkbook workbook = application.Workbooks.Open("PivotTable.xlsx"); |
| 46 | + |
| 47 | + // Get the first pivot table |
| 48 | + IPivotTable pivotTable = workbook.Worksheets[0].PivotTables[0]; |
| 49 | + |
| 50 | + // Ensure layout is calculated |
| 51 | + pivotTable.Layout(); |
| 52 | + |
| 53 | + // Read EndLocation from the implementation type |
| 54 | + IRange endRange = (pivotTable as Syncfusion.XlsIO.Implementation.PivotTables.PivotTableImpl).EndLocation; |
| 55 | + int lastColumn = endRange.LastColumn; |
| 56 | + |
| 57 | + // Use lastColumn as needed (e.g., log) |
| 58 | + Console.WriteLine("PivotTable last column: " + lastColumn); |
| 59 | +} |
| 60 | +{% endhighlight %} |
| 61 | + |
| 62 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 63 | +Using excelEngine As New ExcelEngine() |
| 64 | + Dim application As IApplication = excelEngine.Excel |
| 65 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 66 | + |
| 67 | + ' Open workbook (update path as needed) |
| 68 | + Dim workbook As IWorkbook = application.Workbooks.Open("PivotTable.xlsx") |
| 69 | + |
| 70 | + Dim pivotTable As IPivotTable = workbook.Worksheets(0).PivotTables(0) |
| 71 | + |
| 72 | + ' Calculate layout |
| 73 | + pivotTable.Layout() |
| 74 | + |
| 75 | + ' Read EndLocation from implementation and get last column |
| 76 | + Dim endRange As IRange = DirectCast(pivotTable, Syncfusion.XlsIO.Implementation.PivotTables.PivotTableImpl).EndLocation |
| 77 | + Dim lastColumn As Integer = endRange.LastColumn |
| 78 | + |
| 79 | + Console.WriteLine("PivotTable last column: " & lastColumn) |
| 80 | +End Using |
| 81 | +{% endhighlight %} |
| 82 | +{% endtabs %} |
| 83 | + |
| 84 | +A complete working example to identify the end column of a pivot table in C# is present on [this GitHub page](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Pivot%20Tables%20End%20Column/.NET/End%20Column). |
0 commit comments