Home » Java » How to get the row number after inserting a pivot in a sheet using Apache POI, java

How to get the row number after inserting a pivot in a sheet using Apache POI, java

Posted by: admin July 15, 2018 Leave a comment

Questions:

I’m working on a project where I’ve to create multiple pivots in a single sheet. after creating one pivot whenever I tried to get the last row using sheet.getLastRowNum(), It’s always returning 0. For this very reason, I can’t create more than one pivot in a single sheet as it’s overlapping with other pivots. My code is like,

String[] filter= null;
if(!filters.isEmpty()){
    filter = filters.split(",");
}
String[] column = columns.split(",");
String[] row = rows.split(",");
String[] value = values.split(",");
XSSFWorkbook workbook = (XSSFWorkbook) workbookObj;
XSSFSheet sheet = workbook.getSheetAt(0);
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
int firstCol = sheet.getRow(0).getFirstCellNum();
int lastCol = sheet.getRow(0).getLastCellNum();
CellReference topLeft = new CellReference("Employee Details",firstRow, firstCol, false, false);
CellReference botRight = new CellReference("Employee Details",lastRow, lastCol - 1, false, false);
AreaReference aref = new AreaReference(topLeft,botRight);
XSSFSheet sheet1 = workbook.getSheet(sheetName);
int pivotlastRow = sheet1.getLastRowNum();   //always returning 0
CellReference pos = new CellReference(pivotlastRow + 4, 0);
XSSFPivotTable pivotTable = sheet1.createPivotTable(aref,pos);
if(filter!=null){
    for(int i=0;i<filter.length;i++){
        pivotTable.addReportFilter(Integer.parseInt(filter[i]));
    }
}
for(int j=0;j<column.length;j++){
  addColLabel(pivotTable,Integer.parseInt(column[j]));
}
for(int k=0;k<row.length;k++){
   pivotTable.addRowLabel(Integer.parseInt(row[k]));
}
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, Integer.parseInt(values));

Is there any method or any other way to get after creating each pivot without exporting? Thanks in advance.

Answers: