【发布时间】:2020-03-09 22:08:22
【问题描述】:
如何根据下面的屏幕截图实现数据透视表中的重复所有项目,还需要知道如何在所有列中获取该下拉图标。现在下拉图标仅显示在第一列。 在 Apache POI 中是否可以实现重复所有项目?
下面是代码。
我得到的输出:
我需要的输出:
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();
}
}
}
【问题讨论】:
-
大家好,我们可以从 apache POI 中实现“重复所有项目标签”吗?
标签: javascript java apache-poi pivot-table