You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFPivotTableSorting.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import org.apache.poi.ss.usermodel.DataConsolidateFunction;
  17. import org.apache.poi.ss.usermodel.Row;
  18. import org.apache.poi.ss.util.AreaReference;
  19. import org.apache.poi.ss.util.CellReference;
  20. import org.apache.poi.xssf.XSSFITestDataProvider;
  21. import org.junit.jupiter.api.Test;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotAreaReference;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFieldSortType;
  24. import java.io.IOException;
  25. import static org.junit.jupiter.api.Assertions.assertEquals;
  26. /**
  27. * Test pivot tables created by area reference
  28. */
  29. class TestXSSFPivotTableSorting {
  30. private static final XSSFITestDataProvider _testDataProvider = XSSFITestDataProvider.instance;
  31. @Test
  32. void testNestedSorting() throws IOException {
  33. try (XSSFWorkbook wb = new XSSFWorkbook()) {
  34. XSSFSheet sheet = wb.createSheet();
  35. Row row0 = sheet.createRow(0);
  36. // Create a cell and put a value in it.
  37. row0.createCell(0).setCellValue("Month");
  38. row0.createCell(1).setCellValue("Name");
  39. row0.createCell(2).setCellValue("Product");
  40. row0.createCell(3).setCellValue("Amount");
  41. Row row1 = sheet.createRow(1);
  42. row1.createCell(0).setCellValue("Jan");
  43. row1.createCell(1).setCellValue("John");
  44. row1.createCell(2).setCellValue("Pen");
  45. row1.createCell(3).setCellValue(5);
  46. Row row2 = sheet.createRow(2);
  47. row2.createCell(0).setCellValue("Jan");
  48. row2.createCell(1).setCellValue("Mary");
  49. row2.createCell(2).setCellValue("Paper");
  50. row2.createCell(3).setCellValue(5);
  51. Row row3 = sheet.createRow(3);
  52. row3.createCell(0).setCellValue("Feb");
  53. row3.createCell(1).setCellValue("John");
  54. row3.createCell(2).setCellValue("Clips");
  55. row3.createCell(3).setCellValue(5);
  56. Row row4 = sheet.createRow(4);
  57. row4.createCell(0).setCellValue("Feb");
  58. row4.createCell(1).setCellValue("Mary");
  59. row4.createCell(2).setCellValue("Book");
  60. row4.createCell(3).setCellValue(15);
  61. AreaReference source = wb.getCreationHelper().createAreaReference("A1:D5");
  62. XSSFPivotTable pivotTable = sheet.createPivotTable(source, new CellReference("H1"));
  63. int monthCol = 0;
  64. int nameCol = 1;
  65. int productCol = 2;
  66. int amountCol = 3;
  67. // Names
  68. pivotTable.addRowLabel(nameCol);
  69. pivotTable.addRowLabel(productCol);
  70. pivotTable.addColumnLabel(DataConsolidateFunction.SUM, amountCol);
  71. pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(nameCol).setSortType(STFieldSortType.ASCENDING);
  72. pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(productCol).setSortType(STFieldSortType.DESCENDING);
  73. // add sorting ASC by sum
  74. int advancedSortingColumnInPivot = 1;
  75. CTPivotAreaReference reference =
  76. pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(advancedSortingColumnInPivot)
  77. .addNewAutoSortScope().addNewPivotArea().addNewReferences().addNewReference();
  78. // I have no idea what these constants are for
  79. reference.setField(4294967294L); // if you are curious it's a 2^32 - 2 or just signed -2
  80. reference.addNewX().setV(0);
  81. assertEquals(2, pivotTable.getRowLabelColumns().size());
  82. }
  83. }
  84. }