简体   繁体   English

如何按行和列字段值对Aspose中的数据透视表排序?

[英]How to sort Pivot Table in Aspose by row and column field values?

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. 我尝试在下面的代码中执行此操作,但它不会产生正确/可读的Excel文件。 Alternatively, if I use the commented code, it produces the correct Excel file but does not sort correctly as expected. 或者,如果我使用注释的代码,它会生成正确的Excel文件,但不能按预期正确排序。

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)

Since your requirement is to customize sorting based on some specific values/area in the Pivot Table report, so you cannot achieve it directly via PivotTable options, you may confirm this in MS Excel manually. 由于您的要求是根据“数据透视表”报告中的某些特定值/区域自定义排序,因此您无法直接通过“数据透视表”选项实现排序,因此可以在MS Excel中手动确认。 I am afraid, you have to first create your PivotTable via Aspose.Cells APIs, then you got to refresh PivotTable with its data (ie, use PivoTable.refreshData() and PivotTable.calculateData(), etc.), so Aspose.Cells should paste/render the PivotTable report into the worksheet cells. 恐怕,您必须首先通过Aspose.Cells API创建数据透视表,然后必须使用其数据刷新数据透视表(即,使用PivoTable.refreshData()和PivotTable.calculateData()等),这样Aspose.Cells应该将数据透视表报表粘贴/渲染到工作表单元格中。 Now use general data sorting feature (see the document for your reference: http://www.aspose.com/docs/display/cellsjava/Data+Sorting ) to sort your desired area (using top to bottom or left to right options) accordingly via Aspose.Cells APIs. 现在使用常规数据排序功能(请参阅文档以供参考: http : //www.aspose.com/docs/display/cellsjava/Data+Sorting )对所需区域进行排序(使用从上到下或从左到右的选项)相应地通过Aspose.Cells API。 It is not so simple because you first need to find out which area or part of area (row or column) to be sorted out. 这不是那么简单,因为您首先需要找出要分类的区域或区域的一部分(行或列)。 Here, you may use Find/Search options provided by Aspose.Cells API. 在这里,您可以使用Aspose.Cells API提供的“查找/搜索”选项。

If you still think such a custom sorting can be done directly via PivotTable options in MS Excel, kindly do create your desired PivotTable manually in MS Excel, save the Excel file and provide us in your thread , we will check and help you soon there. 如果您仍然认为可以通过MS Excel中的数据透视表选项直接进行这种自定义排序,请在MS Excel中手动创建所需的数据透视表,保存Excel文件并在您的线程中提供给我们,我们将在此进行检查并为您提供帮助。

I am working as Support developer/ Evangelist at Aspose. 我在Aspose担任技术支持/开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM