用 C++ 从工作表中删除数据透视表
Contents
[
Hide
]
Aspose.Cells提供了一个功能,用于删除或移除工作表中的数据透视表。您可以使用数据透视表对象或数据透视表位置来删除数据透视表。请使用Worksheet.PivotTables.Remove()方法使用数据透视表对象来删除数据透视表,使用其位置在数据透视表集合内Worksheet.PivotTables.RemoveAt()方法删除数据透视表对象。
以下示例代码从工作表中删除两个数据透视表。首先,它使用 Worksheet.PivotTables.Remove() 方法移除数据透视表,然后使用 Worksheet.PivotTables.RemoveAt() 方法继续移除。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"source.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"output_out.xlsx";
// Create workbook object from source Excel file
Workbook workbook(inputFilePath);
// 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);
// OR you can remove pivot table using pivot table position by uncommenting below line
// worksheet.GetPivotTables().RemoveAt(0);
// Save the workbook
workbook.Save(outputFilePath);
std::cout << "Pivot table removed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}