Please find the below image demonstrating what exactly I want to do.
I have tried to do so in the below code, but it does not produce correct/readable Excel file. Alternatively, if I use the commented code, it produces the correct Excel file but does not sort correctly as expected.
Can you please help me achieve the objective mentioned in the above image.
String sortByColName = "OrderDate";
String sortByRowName = "CategoryName";
PivotFieldCollection colFields = pivotTable.getColumnFields();
PivotFieldCollection rowFields = pivotTable.getRowFields();
PivotField sortByColField = colFields.get(sortByColName);
sortByColField.setAutoSort(true);
sortByColField.setAscendSort(true);
sortByColField.setAutoSortField(2); /* sortByColField.setAutoSortField(-1); */
sortByColField.setAutoShow(true);
sortByColField.setAscendShow(true);
sortByColField.setAutoShowField(0);
PivotField sortByRowField = rowFields.get(sortByRowName);
sortByRowField.setAutoSort(true);
sortByRowField.setAscendSort(true);
sortByRowField.setAutoSortField(2); /* sortByRowField.setAutoSortField(-1); */
sortByRowField.setAutoShow(true);
sortByRowField.setAscendShow(true);
sortByRowField.setAutoShowField(0)