Delete Pivot Table from a Worksheet
Contents
[
Hide
]
Aspose.Cells provides a feature to delete or remove Pivot Table from a Worksheet. You can delete the pivot table using pivot table object or pivot table position. Please use the Worksheet.PivotTables.Remove() method to delete the pivot table using pivot table object and Worksheet.PivotTables.RemoveAt() method to delete pivot table object using its position inside the pivot table collection.
The following sample code deletes two pivot tables from the worksheet. First it removes pivot table using Worksheet.PivotTables.Remove() method and then it removes pivot table using Worksheet.PivotTables.RemoveAt() method
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access the first pivot table object | |
PivotTable pivotTable = worksheet.PivotTables[0]; | |
// Remove pivot table using pivot table object | |
worksheet.PivotTables.Remove(pivotTable); | |
// OR you can remove pivot table using pivot table position by uncommenting below line | |
//worksheet.PivotTables.RemoveAt(0); | |
// Save the workbook | |
workbook.Save(dataDir + "output_out.xlsx"); |