Javascript 如何重复PIVOT表中的所有标签-ApachePOI

Javascript 如何重复PIVOT表中的所有标签-ApachePOI,javascript,java,apache-poi,pivot-table,Javascript,Java,Apache Poi,Pivot Table,如何按照下面的屏幕截图实现数据透视表中的重复所有项目,还需要知道如何在所有列中获得下拉图标。现在下拉图标仅显示在第一列。 在Apache POI中是否可以重复所有项目 下面是代码 我得到的输出: 我需要的输出: 公共类TestPivotTable { 公共静态void main(字符串[]args)引发异常 { 工作簿wb=新XSSFWorkbook(); 字符串[][]数据=新字符串[][{{{“AAA”、“BBB”、“CCC”、“DDD”、“EEE”、“FFF”、“GGG”、“HHH”}

如何按照下面的屏幕截图实现数据透视表中的重复所有项目,还需要知道如何在所有列中获得下拉图标。现在下拉图标仅显示在第一列。 在Apache POI中是否可以重复所有项目

下面是代码

我得到的输出:

我需要的输出:

公共类TestPivotTable
{
公共静态void main(字符串[]args)引发异常
{
工作簿wb=新XSSFWorkbook();
字符串[][]数据=新字符串[][{{{“AAA”、“BBB”、“CCC”、“DDD”、“EEE”、“FFF”、“GGG”、“HHH”},
{“汤姆”、“傻瓜”、“瓦尔”、“1001683”、“描述1”、“2017年4月27日”、“猫”、“780936.58”},
{“汤姆”、“傻瓜”、“瓦尔”、“1001695”、“描述2”、“2017年4月27日”、“猫”、“136.28”},
{“哈利”、“傻瓜”、“瓦尔”、“1001692”、“描述3”、“2017年5月3日”、“CAT1”、“191468.21”},
{“哈利”、“傻瓜”、“瓦尔”、“1001698”、“描述4”、“2017年5月4日”、“CAT1”、“10.11”};
XSSFSheet sheet=(XSSFSheet)wb.createSheet(“数据”);
XSSFSheet pivot=(XSSFSheet)wb.createSheet(“摘要”);
for(字符串[]数据行:数据){
XSSFRow row=sheet.createRow(sheet.getPhysicalNumberOfRows());
for(字符串数据单元:dataRow){
XSSFCell cell=row.createCell(row.getPhysicalNumberOfCells());
cell.setCellValue(数据单元);
}
}
XSSFTable table=sheet.createTable();
CTTable=table.getCTTable();
table.setDisplayName(“table”);
cttable.setRef(“A1:D4”);
cttable.setId(1);
CTTableColumns=cttable.addNewTableColumns();
列。设置计数(3);
int g=1;
for(字符串colName:data[0]){
CTTableColumn=columns.addNewTableColumn();
column.setId(++g);
column.setName(colName);
}
AreaReference AreaReference=新的AreaReference(“A1:H”+(sheet.getLastRowNum()+
1) ,电子表格版本,EXCEL2007);
FileOutputStream fileOut=null;
试一试{
XSSFPivotTable pivotTable=pivot.createPivotTable(区域引用,新单元格引用(“A1”),
板材);
数据透视表.getCTPivotTableDefinition().setRowHeaderCaption(“AAA”);
List iterList=new ArrayList();
iterList.add(0);
iterList.add(1);
增加(2);
增加(3);
增加(4);
增加(5);
增加(6);
for(整数j:iterList){
数据透视表.addRowLabel(j);
TreeSet uniqueItems=new java.util.TreeSet();
对于(int r=areaReference.getFirstCell().getRow()+1;r<
areaReference.getLastCell().getRow()+1;r++){
add(sheet.getRow(r.getCell(j.toString());
}
系统输出打印项次(唯一项);
CTPivotField CTPivotField=
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(j);
int i=0;
用于(字符串项:唯一项){
ctPivotField.getItems().getItemArray(i).unset();
ctPivotField.getItems().getItemArray(i).setX((长)i);
数据透视表.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields()。
getCacheFieldArray(j)
.getSharedItems().addNewS().setV(项目);
i++;
}
//ctPivotField.setAutoShow(false);
ctPivotField.setDefaultSubtotal(false);
ctPivotField.setOutline(false);
//ctPivotField.setCompact(false);
//ctPivotField.setSubtotalTop(真);
if(ctPivotField.getDefaultSubtotal())i++;
对于(int k=ctPivotField.getItems().getItemList().size()-1;k>=i;k--){
ctPivotField.getItems().removeItem(k);
}
ctPivotField.getItems().setCount(i);
}
System.out.println(“----结束----”;
数据透视表.addColumnLabel(DataConsolidateFunction.SUM,7,“SUM”);
fileOut=newfileoutputstream(“newoutputfile.xlsx”);
wb.写入(文件输出);
}捕获(例外e){
System.out.println(“创建透视表时出现异常”+e);
}最后{
fileOut.close();
wb.close();
}
}
}在发布了
Office Open XML
之后,为透视表执行了“重复所有项目标签”设置。这就是为什么它不是它的一部分。它使用来自名称空间
x14
的特殊扩展
XML
。但是
apachepoi
只提供
officeopenxml

因此,如果我们想从名称空间
x14
使用扩展的
XML
,我们需要在较低的
XML
级别上这样做

首先,我们需要将更新的透视表版本设置为6,而不是3

然后我们需要为每个
CTPivotField
设置
。这是“重复所有项目标签”设置

扩展所示代码的完整示例:

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();
 }
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.ss.*;
导入java.io.*;
导入java.util.ArrayList;
导入java.util.TreeSet;
导入java.util.List;
导入org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
公共类测试数据透视表{
公共静态void main(字符串[]args)引发异常{
工作簿wb=新XSSFWorkbook();
对象[][]数据=新对象[][]{
{“AAA”、“BBB”、“CCC”、“DDD”、“EEE”、“FFF”、“GGG”、“HHH”},
{“汤姆”,“傻瓜”,“瓦尔”,“1001683”,“描述1”,“2017年4月27日”,“猫”,780936.58},
{“汤姆”,“傻瓜”,“瓦尔”,“1001695”,“描述2”,“2017年4月27日”,“猫”,136.28},
{“哈利”、“傻瓜”、“瓦尔”、“1001692”、“描述3”、“2017年5月3日”、“CAT1”,191468.21},
{“哈利”、“傻瓜”、“瓦尔”、“1001698”、“描述4”、“2017年5月4日”、“第1类”,10.11}
};
XSSFSheet sheet=(XSSFSheet)wb.createSheet(“数据”);
XSSFSheet pivot=(XSSFSheet)wb.createSheet(“摘要”);
对于(对象[]数据行:数据){
XSSFRow row=sheet.createRow(sheet.getPhysicalNumberOfRows());
用于(对象数据单元:dataRow){
XSSFCell cell=row.createCell(row.getPhysicalNumberOfCells());
if(字符串的数据单元实例){
setCellValue((字符串)dataCell);
}else if(Doub的数据单元实例)
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();
 }
}