Delete Pivot Table from a Worksheet
Contents
[
Hide
]
Aspose.Cells provides a feature to delete or remove a Pivot Table from a Worksheet. You can delete the pivot table using the pivot table object or pivot table position. Please use the Worksheet.getPivotTables().remove() method to delete the pivot table using the pivot table object and Worksheet.getPivotTables().removeAt() method to delete a pivot table object using its position inside the pivot table collection.
Example
The following sample code deletes two pivot tables from the worksheet. First, it removes the pivot table using Worksheet.getPivotTables().remove() method and then it removes the pivot table using Worksheet.getPivotTables().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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DeletePivotTableFromWorksheet.class); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the first pivot table object | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Remove pivot table using pivot table object | |
worksheet.getPivotTables().remove(pivotTable); | |
// Remove pivot table using pivot table position | |
worksheet.getPivotTables().removeAt(0); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |