1

I have a requirement to create a excel sheet in JAVA as below :

enter image description here

I am not able to create a row label with multiple columns side by side ( menus and submenus filter ).

Instead of displaying the submenu in different column, its coming under the menu column.

Below is the piece of code which i have written :

XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0); 
    AreaReference a=new AreaReference("A1:G7");
    CellReference b=new CellReference("I5");
    XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);
    pivotTable.addReportFilter(0);
    pivotTable.addReportFilter(1);
    pivotTable.addRowLabel(2);
    pivotTable.addRowLabel(3);
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum");   

But its getting wrongly displayed as below :

enter image description here

Can someone please help me ?

4
  • under pivot table options, select "Classic Pivot Table layout" Commented Apr 6, 2016 at 5:35
  • Can you please elaborate bit more please ? I am new to poi API's. Commented Apr 6, 2016 at 5:43
  • I was giving you how to change it in Excel. The macro recorder shows you want pivotTable.InGridDropZones = True and pivotTable.RowAxisLayout xlTabularRow Commented Apr 6, 2016 at 5:55
  • I want to do it from the java code. I cannot change it manually on excel sheet. This sheet is been downloaded by user. Commented Apr 6, 2016 at 5:59

1 Answer 1

3

Since the format is XML it is easy to check what is needed. Unpack the Zip xlsx and look in the /xl/pivotTables/pivotTable1.xml.

Then: https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFPivotTable.html#getCTPivotTableDefinition%28%29 and http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/spreadsheetml/x2006/main/CTPivotTableDefinition.java.

    AreaReference a=new AreaReference(new CellReference("A1"), new CellReference("E7"));
    CellReference b=new CellReference("I5");
    XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);
    pivotTable.addReportFilter(0);
    pivotTable.addReportFilter(1);
    pivotTable.addRowLabel(2);

pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setOutline(false);

    pivotTable.addRowLabel(3);
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum"); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.