简体   繁体   English

如何在 PIVOT 表中重复所有标签 - Apache POI

[英]How to Repeat All Labels in PIVOT Table- Apache POI

How to achieve the Repeat All items in Pivot table as per below screen shot and also need to know how we can get that drop down icon across all columns.如何按照下面的屏幕截图实现 Pivot 表中的重复所有项目,还需要知道如何在所有列中获取该下拉图标。 now drop down icon showing only at the first column.现在下拉图标仅显示在第一列。 Is Repeat All Items is achievable in Apache POI?. Apache POI 中是否可以实现重复所有项目?

below is the code.下面是代码。

Output which i am getting:我得到的 Output:

在此处输入图像描述

output which i need: output 我需要:

在此处输入图像描述

 public class TestPivotTable
 {
public static void main(String[] args) throws Exception
{
    Workbook wb = new XSSFWorkbook();
    String[][] data = new String[][]{{"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH"}, 
 {"TOM","DUMMY","VAL","1001683","Description1","27/04/2017","CAT","7,80,936.58"},
        {"TOM","DUMMY","VAL","1001695","Description2","27/04/2017","CAT","136.28"},
        {"HARRY","DUMMY1","VAL1","1001692","Description3","03/05/2017","CAT1","191468.21"},
        {"HARRY","DUMMY1","VAL1","1001698","Description4","04/05/2017","CAT1","10.11"}};

    XSSFSheet sheet = (XSSFSheet) wb.createSheet("data");
    XSSFSheet pivot = (XSSFSheet) wb.createSheet("summary");
    for(String[] dataRow : data){
        XSSFRow row = sheet.createRow(sheet.getPhysicalNumberOfRows());
        for(String dataCell : dataRow){
            XSSFCell cell = row.createCell(row.getPhysicalNumberOfCells());
            cell.setCellValue(dataCell);
        }
    }
    XSSFTable table = sheet.createTable();    
    CTTable cttable = table.getCTTable();
    table.setDisplayName("table");
    cttable.setRef("A1:D4");
    cttable.setId(1);

    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);

    int g = 1;
    for (String colName : data[0]){
        CTTableColumn column = columns.addNewTableColumn();
        column.setId(++g);
        column.setName(colName);      
    }
  AreaReference areaReference = new AreaReference("A1:H"+ (sheet.getLastRowNum() + 
  1),SpreadsheetVersion.EXCEL2007);
  FileOutputStream fileOut=null;
  try {
    XSSFPivotTable pivotTable =  pivot.createPivotTable(areaReference, new CellReference("A1"), 
  sheet);

    pivotTable.getCTPivotTableDefinition().setRowHeaderCaption("AAA");

    List<Integer> iterList = new ArrayList<Integer>();

    iterList.add(0);
    iterList.add(1);
    iterList.add(2);
    iterList.add(3);
    iterList.add(4);
    iterList.add(5);
    iterList.add(6);

    for (Integer j : iterList) {

        pivotTable.addRowLabel(j);
        TreeSet<String> uniqueItems = new java.util.TreeSet<String>();
        for (int r = areaReference.getFirstCell().getRow()+1; r < 
   areaReference.getLastCell().getRow()+1; r++) {
            uniqueItems.add(sheet.getRow(r).getCell(j).toString());
        }
        System.out.println(uniqueItems);
        CTPivotField ctPivotField = 
   pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(j);
        int i = 0;
        for (String item : uniqueItems) {
            ctPivotField.getItems().getItemArray(i).unsetT();
            ctPivotField.getItems().getItemArray(i).setX((long)i);


pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().
 getCacheFieldArray(j)
            .getSharedItems().addNewS().setV(item);
            i++;
        }
       // ctPivotField.setAutoShow(false);
        ctPivotField.setDefaultSubtotal(false);
         ctPivotField.setOutline(false);
       // ctPivotField.setCompact(false);
       // ctPivotField.setSubtotalTop(true);
        if (ctPivotField.getDefaultSubtotal()) i++; 
        for (int k = ctPivotField.getItems().getItemList().size()-1; k >= i; k--) {
            ctPivotField.getItems().removeItem(k);
        }
        ctPivotField.getItems().setCount(i);

    }

    System.out.println("----end---");

    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 7, "SUM");

    fileOut = new FileOutputStream("newoutputfile.xlsx");
    wb.write(fileOut);
}catch(Exception e) {
    System.out.println("Exception While Creating Pivot Table"+e);
}finally {
    fileOut.close();
    wb.close();
}

}

} }

The "Repeat All Items Label" setting for pivot tables was implemented after the Office Open XML was published. pivot 表的“重复所有项目标签”设置是在Office Open XML发布后实施的。 That's why it is not part of it.这就是为什么它不是它的一部分。 It uses special extended XML from name space x14 .它使用来自命名空间x14的特殊扩展XML But apache poi only provides Office Open XML .但是apache poi只提供Office Open XML

So if we want using extended XML from name space x14 , we need doing that on low XML level.因此,如果我们想使用命名空间x14中的扩展XML ,我们需要在低XML级别上执行此操作。

First we need setting updated pivot table version to 6 instead of 3.首先,我们需要将更新的 pivot 表版本设置为 6 而不是 3。

Then we need set <x14:pivotField fillDownLabels="1"/> for each CTPivotField .然后我们需要为每个CTPivotField设置<x14:pivotField fillDownLabels="1"/> This is the "Repeat All Items Label" setting.这是“重复所有项目标签”设置。

Complete example which extends your showed code:扩展您显示的代码的完整示例:

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.*;
import java.io.*;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.List;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

public class TestPivotTable {
 public static void main(String[] args) throws Exception {
  Workbook wb = new XSSFWorkbook();
  Object[][] data = new Object[][]{
   {"AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH"}, 
   {"TOM","DUMMY","VAL","1001683","Description1","27/04/2017","CAT",780936.58},
   {"TOM","DUMMY","VAL","1001695","Description2","27/04/2017","CAT",136.28},
   {"HARRY","DUMMY1","VAL1","1001692","Description3","03/05/2017","CAT1",191468.21},
   {"HARRY","DUMMY1","VAL1","1001698","Description4","04/05/2017","CAT1",10.11}
  };

  XSSFSheet sheet = (XSSFSheet) wb.createSheet("data");
  XSSFSheet pivot = (XSSFSheet) wb.createSheet("summary");
  for(Object[] dataRow : data){
   XSSFRow row = sheet.createRow(sheet.getPhysicalNumberOfRows());
   for(Object dataCell : dataRow){
    XSSFCell cell = row.createCell(row.getPhysicalNumberOfCells());
    if (dataCell instanceof String) {
     cell.setCellValue((String)dataCell);
    } else if (dataCell instanceof Double) {
     cell.setCellValue((Double)dataCell);
    }
   }
  }

  AreaReference areaReference = new AreaReference("A1:H"+ (sheet.getLastRowNum() + 1), SpreadsheetVersion.EXCEL2007);
  XSSFPivotTable pivotTable =  pivot.createPivotTable(areaReference, new CellReference("A1"), sheet);
  pivotTable.getCTPivotTableDefinition().setRowHeaderCaption("AAA");

  List<Integer> iterList = new ArrayList<Integer>();
  iterList.add(0);
  iterList.add(1);
  iterList.add(2);
  iterList.add(3);
  iterList.add(4);
  iterList.add(5);
  iterList.add(6);

  //set updated pivot table version to 6
  //necessary to make extended pivot table setting available
  pivotTable.getCTPivotTableDefinition().setUpdatedVersion((short)6); 

  for (Integer j : iterList) {

   //create row label - apache poi creates as much fields for each as rows are in the pivot table data range
   pivotTable.addRowLabel(j);

   //determine unique labels in column j
   TreeSet<String> uniqueItems = new java.util.TreeSet<String>();
   for (int r = areaReference.getFirstCell().getRow()+1; r < areaReference.getLastCell().getRow()+1; r++) {
    uniqueItems.add(sheet.getRow(r).getCell(j).getStringCellValue());
   }
   System.out.println(uniqueItems);

   //build pivot table and cache
   CTPivotField ctPivotField = pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(j);
   int i = 0;
   for (String item : uniqueItems) {
    //take the items as numbered items: <item x="0"/><item x="1"/>
    ctPivotField.getItems().getItemArray(i).unsetT();
    ctPivotField.getItems().getItemArray(i).setX((long)i);
    //build a cache definition which has shared elements for those items 
    //<sharedItems><s v="TOM"/><s v="HARRY"/></sharedItems>
    pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldArray(j)
     .getSharedItems().addNewS().setV(item);
    i++;
   }

   //ctPivotField.setAutoShow(false);
   ctPivotField.setDefaultSubtotal(false);
   //ctPivotField.setSubtotalTop(false);
   //ctPivotField.setSubtotalCaption(null);
   //ctPivotField.setCompact(false);
   ctPivotField.setOutline(false);

   //remove further items
   if (ctPivotField.getDefaultSubtotal()) i++; //let one default item be if there shall be subtotals
   for (int k = ctPivotField.getItems().getItemList().size()-1; k >= i; k--) {
    ctPivotField.getItems().removeItem(k);
   }
   ctPivotField.getItems().setCount(i);

   //set <x14:pivotField fillDownLabels="1"/> for each CTPivotField
   //this is the "Repeat All Items Label" setting
   CTExtensionList extList = ctPivotField.addNewExtLst();
   CTExtension ext = extList.addNewExt();
   String extXML = 
      "<x14:pivotField"
    + " xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\""
    + " fillDownLabels=\"1\"/>";
   org.apache.xmlbeans.XmlObject xlmObject = org.apache.xmlbeans.XmlObject.Factory.parse(extXML);
   ext.set(xlmObject);
   ext.setUri("{2946ED86-A175-432a-8AC1-64E0C546D7DE}");  

  }

  System.out.println("----end---");

  pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 7, "SUM");

  FileOutputStream fileOut = new FileOutputStream("newoutputfile.xlsx");
  wb.write(fileOut);
  fileOut.close();
  wb.close();
 }
}

Note: I have removed the XSSFTable creation from your code since it is not necessary and is done wrong and leads to a corrupted Excel file if done as you have done it.注意:我已经从您的代码中删除了XSSFTable创建,因为它不是必需的,并且做错了,如果按照您的操作完成,会导致Excel文件损坏。

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

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