Java 列和行标签使用相同索引时Apache POI透视表错误
我试图创建一个数据透视表来进行队列分析Java 列和行标签使用相同索引时Apache POI透视表错误,java,excel,apache-poi,pivot,pivot-table,Java,Excel,Apache Poi,Pivot,Pivot Table,我试图创建一个数据透视表来进行队列分析 pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1); pivotTable.addRowLabel(1); 这是在打开文件时给我一个错误,该文件已损坏。是否仍要打开该文件,当我说“是”并打开它时,结果看起来很好,唯一的问题是错误 我做了一个变通办法,用不同的名称复制了一个列数据 例如: 假设第1列是电子邮件,添加了一个重复的第36列,名称为dup email,并按如下所示执行,效果良好
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);
pivotTable.addRowLabel(1);
这是在打开文件时给我一个错误,该文件已损坏。是否仍要打开该文件,当我说“是”并打开它时,结果看起来很好,唯一的问题是错误
我做了一个变通办法,用不同的名称复制了一个列数据
例如:
假设第1列是电子邮件,添加了一个重复的第36列,名称为dup email,并按如下所示执行,效果良好
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);
pivotTable.addRowLabel(35);
首先,当我将列和行标签都指定为1时,为什么它失败了
如果您使用apache poi设置数据透视表.addRowLabel(1),则apache poi将数据透视字段1设置为axisRow,但如果您还希望
数据透视表.addColumnLabel(DataConsolidateFunction.COUNT,1)
,则需要将数据透视字段1设置为dataField,我们将非常感谢您的帮助。所以我们需要纠正这一点
例如:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;
class PivotTableTest5 {
private static void setCellData(Sheet sheet) {
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Name");
cell = row.createCell(1);
cell.setCellValue("City");
for (int r = 1; r < 15; r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue("Name " + ((r-1) % 4 + 1));
cell = row.createCell(1);
cell.setCellValue("City " + (int)((new java.util.Random().nextDouble() * 3)+1) );
}
}
public static void main(String[] args) {
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
//Create some data to build the pivot table on
setCellData(sheet);
XSSFPivotTable pivotTable = sheet.createPivotTable(
new AreaReference(new CellReference("A1"), new CellReference("B15")), new CellReference("H5"));
//Count the second column. This needs to be second column a data field.
pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 1);
//Use second column as row label
pivotTable.addRowLabel(1);
//Apache poi sets pivot field 1 (second column) only to be axisRow but it needs to be dataField too.
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(1).setDataField(true);
FileOutputStream fileOut = new FileOutputStream("PivotTableTest5.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入java.io.*;
类PivotTableTest5{
专用静态void setCellData(表格){
Row Row=sheet.createRow(0);
Cell Cell=row.createCell(0);
cell.setCellValue(“名称”);
cell=row.createCell(1);
cell.setCellValue(“城市”);
对于(int r=1;r<15;r++){
行=表。创建行(r);
cell=row.createCell(0);
cell.setCellValue(“名称”+((r-1)%4+1));
cell=row.createCell(1);
setCellValue(“City”+(int)((new java.util.Random().nextDouble()*3)+1));
}
}
公共静态void main(字符串[]args){
试一试{
XSSF工作簿wb=新XSSF工作簿();
XSSFSheet sheet=wb.createSheet();
//创建一些数据以构建数据透视表
setCellData(表格);
XSSFPivotTable数据透视表=sheet.createPivotTable(
新区域参考(新小区参考(“A1”)、新小区参考(“B15”)、新小区参考(“H5”);
//计算第二列。这必须是数据字段的第二列。
数据透视表.addColumnLabel(DataConsolidateFunction.COUNT,1);
//使用第二列作为行标签
数据透视表.addRowLabel(1);
//ApachePOI仅将pivot字段1(第二列)设置为axisRow,但也需要设置为dataField。
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(1).setDataField(true);
FileOutputStream fileOut=新的FileOutputStream(“PivotTableTest5.xlsx”);
wb.写入(文件输出);
fileOut.close();
wb.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
非常感谢,这仍然是POI 4.0.1中的一个问题。