如何从java折叠透视表中的所有字段?
我正在用JAVA中的ApachePOI创建一个透视表,它会生成一个透视表,如下所示,默认情况下所有行都会展开如何从java折叠透视表中的所有字段?,java,excel,apache-poi,pivot-table,Java,Excel,Apache Poi,Pivot Table,我正在用JAVA中的ApachePOI创建一个透视表,它会生成一个透视表,如下所示,默认情况下所有行都会展开 如何生成数据透视表将从java代码中按如下方式折叠所有行。提前感谢。用于生成数据透视表的代码 AreaReference a=new AreaReference("A1:G5667", null); CellReference b=new CellReference("A1"); XSSFSheet pivot_sh
如何生成数据透视表将从java代码中按如下方式折叠所有行。
提前感谢。
用于生成数据透视表的代码
AreaReference a=new AreaReference("A1:G5667", null);
CellReference b=new CellReference("A1");
XSSFSheet pivot_sheet=workbook.createSheet("Pivot_table");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(a,b,spreadsheet);
pivotTable.addRowLabel(6);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(2);
pivotTable.addRowLabel(3);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4,"Sum");
您提供的代码不能导致第一个显示的结果,因为它没有设置不同的列标签。它只添加列
E
作为数据合并列
但我还是会尝试回答。所以我假设列G
应该是第一行标签。列A
应为第二行标签,应折叠,列C
应为第三行标签。但是,D列
是包含“2018年4月”、“2018年5月”、“2018年6月”的列,此时应为列标签
问题在于apachepoi
在创建pivot表时没有分析内容。因此,它只需为每个数据透视字段添加与数据范围中的行一样多的“默认”数据透视字段项。它只创建了一个非常基本的pivot缓存。只要我们只使用默认值,这就行了,因为Excel
会在呈现透视表时纠正这一点。但是,如果我们需要默认设置以外的其他设置,例如折叠行标签,那么这将失败
因此,我们需要列A
中的唯一内容,以便对所需的数据透视项进行计数并正确创建数据透视缓存。然后,我们需要将尽可能多的透视字段项从“默认”更改为实际透视字段项,因为列A
中有不同的内容。这些透视字段项必须设置指向透视缓存的属性x
。并且必须正确创建pivot缓存,在列A
中包含单个唯一内容。此外,属性sd
必须设置为false
,这表示此项目的详细信息被隐藏
完整示例:
卓越:
代码:
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.ss.SpreadsheetVersion;
导入java.io.FileOutputStream;
导入java.io.FileInputStream;
导入java.util.List;
导入java.util.Set;
导入java.util.HashSet;
类数据透视表测试{
公共静态void main(字符串[]args)引发异常{
XSSF工作簿=(XSSF工作簿)WorkbookFactory.create(新文件输入流(“PivotExample.xlsx”);
XSSFSheet dataSheet=workbook.getSheet(“数据”);
XSSFSheet pivotSheet=workbook.createSheet(“Pivot”);
AreaReference a=新的AreaReference(“A1:G”+(dataSheet.getLastRowNum()+1),电子表格版本.EXCEL2007);
CellReference b=新的CellReference(“A1”);
XSSFPivotTable数据透视表=数据透视表。创建数据透视表(a、b、数据表);
数据透视表.addRowLabel(6);//列G作为第一行标签
数据透视表.addRowLabel(0);//列A作为第二行标签-应折叠
//我们需要列A中的唯一内容来创建pivot缓存
Set colAValues=new HashSet();
对于(int r=1;r
此代码需要中提到的ooxml-schemas-1.3.jar
。
结果:
恐怕还不可能。请提供如何创建第一个结果的代码。然后我将告诉您需要对代码进行哪些更改才能得到结果2。@AxelRichter我添加了用于生成第一个结果的代码。非常感谢,它的工作方式完全符合我的要求。真棒:)
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.SpreadsheetVersion;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
class ExcelPivotTableTest {
public static void main(String[] args) throws Exception{
XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("PivotExample.xlsx"));
XSSFSheet dataSheet = workbook.getSheet("Data");
XSSFSheet pivotSheet = workbook.createSheet("Pivot");
AreaReference a = new AreaReference("A1:G" + (dataSheet.getLastRowNum() + 1), SpreadsheetVersion.EXCEL2007);
CellReference b = new CellReference("A1");
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(a, b, dataSheet);
pivotTable.addRowLabel(6); //column G as first row label
pivotTable.addRowLabel(0); //column A as second row label - shall be collapsed
//we need unique contents in column A for creating the pivot cache
Set<String> colAValues = new HashSet<String>();
for (int r = 1; r < dataSheet.getLastRowNum() + 1; r++) {
Row row = dataSheet.getRow(r);
if (row != null) {
Cell cell = row.getCell(0);
if (cell != null) {
colAValues.add(cell.toString());
}
}
}
//now go through all pivot items of first pivot field
List<org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItem> itemList =
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().getItemList();
int i = 0;
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItem item = null;
for (String value : colAValues) { //as long as there are different column A values
item = itemList.get(i);
item.unsetT(); //unset the type "default"
item.setX(i++); //set x
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields()
.getCacheFieldArray(0).getSharedItems().addNewS().setV(value); //create pivot cache entry
item.setSd(false); //set sd false = indicates that the details are hidden for this item
}
while (i < itemList.size()) {
item = itemList.get(i++);
item.setSd(false);
}
pivotTable.addRowLabel(2); //column C as third row label
pivotTable.addRowLabel(3); //column D as row label - shall be column label instead
//do changing column D to a col label
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3)
.setAxis(org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL); //AXIS_COL
//remove column D from RowFields
pivotTable.getCTPivotTableDefinition().getRowFields().removeField(3);
pivotTable.getCTPivotTableDefinition().getRowFields().setCount(3);
//create ColFields for column D
pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(3);
pivotTable.getCTPivotTableDefinition().getColFields().setCount(1);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum");
workbook.write(new FileOutputStream("PivotExample_New.xlsx"));
workbook.close();
}
}