‘Row Label’ appears as first column header as well as subtotals appear, while generating excel pivot table using apache poi

I am generating an excel with pivot table using apache poi. Pivot gets generated. However, there are two issues observed

Source table:

Employee ID Employee Name Location Salary
100 John London 5000
101 Chris New York 6000
102 Mary Los Angeles 10000
103 Lilly London 6000
104 Joe Toronto 3000
105 Dan New York 7500

Issue:

Generated excel pivot shown here

Issue Description:

  1. The first column header of the pivot observed to be as ‘Row Label’, instead of the actual pivoted column header (here ‘Employee ID’). How to ensure the actual pivoted column header (‘Employee ID’) appears as first column header
  2. Subtotals appear for each row. How to disable subtotal using apache poi?

Here is my code

public class ExcelPivot {

    public static void main(String[] args) throws XmlException {

        try (FileInputStream fis = new FileInputStream("employee_template.xlsx");
                Workbook wb = new XSSFWorkbook(fis)) {
            List<Employee> employeeList = Arrays.asList(
                    new Employee("100", "John", "London", 5000.00),
                    new Employee("101", "Chris", "New York", 6000.00),
                    new Employee("102", "Mary", "Los Angeles", 10000.00),
                    new Employee("103", "Lilly", "London", 6000.00),
                    new Employee("104", "Joe", "Toronto", 3000.00),
                    new Employee("105", "Dan", "New York", 7500.00)
            );

            fis.close();
            Sheet sheet = wb.getSheet("Employee");

            for (int r = 1; r <= employeeList.size(); r++) {
                Employee obj = employeeList.get(r - 1);
                Row row = sheet.createRow(r);
                Cell cell1 = row.createCell(0);
                cell1.setCellValue(obj.getEmployeeId());
                Cell cell2 = row.createCell(1);
                cell2.setCellValue(obj.getEmployeeName());
                Cell cell3 = row.createCell(2);
                cell3.setCellValue(obj.getLocation());
                Cell cell4 = row.createCell(3);
                cell4.setCellValue(obj.getSalary());
            }

            XSSFSheet pivotSheet = (XSSFSheet) wb.getSheet("Summary");

            AreaReference source = new AreaReference("A1:D7", SpreadsheetVersion.EXCEL2007);
            CellReference position = new CellReference("B10");

            XSSFPivotTable pivotTable = pivotSheet.createPivotTable(source, position, sheet);
            pivotTable.addRowLabel(0);
            pivotTable.addRowLabel(1);
            pivotTable.addColLabel(2);
            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3, "Sum Of Salary");

            pivotTable.getCTPivotTableDefinition().setColGrandTotals(false);

            for (CTPivotField ctPivotField : pivotTable.getCTPivotTableDefinition().getPivotFields()
                    .getPivotFieldList()) {
                ctPivotField.setOutline(false);
                ctPivotField.setSubtotalCaption("");
            }

            System.out.println("Pivot Generated");

            FileOutputStream fileOut = new FileOutputStream("employee_template.xlsx");
            wb.write(fileOut);
            fileOut.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    static class Employee {
        private String employeeId;
        private String employeeName;
        private String location;
        private double salary;

        public Employee(String employeeId, String employeeName, String location, double salary) {
            super();
            this.employeeId = employeeId;
            this.employeeName = employeeName;
            this.location = location;
            this.salary = salary;
        }

        public String getEmployeeId() {
            return employeeId;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public String getLocation() {
            return location;
        }

        public double getSalary() {
            return salary;
        }

    }

}

I couldn’t find any documentation in Apache poi with respect to this. Also, tried various property settings such as
For Issue 1 (Row label issue) I tried this:
//pivotSheet.getRow(2).getCell(1).setCellValue("Fund Id");
For Issue 2 (Subtotal issue) I tried this:
ctPivotField.setSumSubtotal(false); ctPivotField.setDefaultSubtotal(false); ctPivotField.setVarSubtotal(false);

But, nothing seems to be affecting/changing the generated pivot table. Does any of you have any suggestions to fix this issue?

New contributor

Keerthi K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật