@juanc
To create a Pivot Table from dynamic data in Excel using Aspose.Cells, you can follow these steps:
Step 1: Import the Aspose.Cells Library
Make sure you have the Aspose.Cells library added to your project. You can download it from the Aspose releases.
Step 2: Load the Workbook
First, load the workbook that contains your dynamic data. You can do this with the following code:
Workbook workbook = new Workbook("path_to_your_excel_file.xlsx");
Step 3: Create a Pivot Table
After loading the workbook, you can create a Pivot Table. You need to specify the source data range and the location where you want to place the Pivot Table. Here’s an example:
Worksheet dataSheet = workbook.getWorksheets().get(0); // Assuming your data is in the first sheet
PivotTableCollection pivotTables = workbook.getWorksheets().get(1).getPivotTables(); // Assuming you want to create it in the second sheet
int index = pivotTables.add("=Data!A1:C30", "E3", "PivotTable1"); // Adjust the range and location as needed
PivotTable pivotTable = pivotTables.get(index);
Step 4: Configure the Pivot Table
You can configure the Pivot Table by adding fields to the row, column, and data areas. Here’s how you can do it:
pivotTable.addFieldToArea(PivotFieldType.ROW, 0); // Add the first field to the row area
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1); // Add the second field to the column area
pivotTable.addFieldToArea(PivotFieldType.DATA, 2); // Add the third field to the data area
Step 5: Refresh the Pivot Table
If your source data changes, you can refresh the Pivot Table to reflect the updates:
pivotTable.refreshData();
Step 6: Save the Workbook
Finally, save the workbook to persist your changes:
workbook.save("path_to_save_your_file.xlsx");
Conclusion
By following these steps, you can create a dynamic Pivot Table in Excel using Aspose.Cells. This allows you to summarize and analyze data effectively, even when the structure of your data changes from one export to another.
For more detailed examples and additional features, you can refer to the Aspose documentation on creating dynamic Pivot Tables here and here.
If you have any further questions or need assistance, feel free to ask!