Java 如何在pivot表中使用ApachePOI在as列标签中添加多个列

Java 如何在pivot表中使用ApachePOI在as列标签中添加多个列,java,apache-poi,Java,Apache Poi,我正在创建透视表,以前我将一个行值作为列,但现在当我在透视表中添加了一个列标签时,它被删除了 我的代码给出了类似的o/p 所需o/p 这是我的密码 public class ApacheCreatePivotTable { public static void main(String[] args) throws Exception { XSSFWorkbook wb = new XSSFWorkbook()

我正在创建透视表,以前我将一个行值作为列,但现在当我在透视表中添加了一个列标签时,它被删除了

我的代码给出了类似的o/p

所需o/p

这是我的密码

          public class ApacheCreatePivotTable
    {
        public static void main(String[] args) throws Exception
        {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet =  wb.createSheet();
            //Create some data to build the pivot table on
            setCellData(sheet);
            int endclonum = wb.getSheetAt(0).getRow(0).getLastCellNum()-1;
            int endrownum = wb.getSheetAt(0).getLastRowNum();
            AreaReference source = new AreaReference(new CellReference(0,0),new CellReference(endrownum,endclonum));
            XSSFSheet sheet2 = wb.createSheet("pivot");
            CellReference position = new CellReference("A1"); //convertColStringToIndex
            XSSFPivotTable pivotTable = sheet2.createPivotTable(source, position, sheet);
            pivotTable.addRowLabel(0);
            pivotTable.addRowLabel(1);
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setDefaultSubtotal(false);

            pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);
            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);
            FileOutputStream fileOut = new FileOutputStream("output.xlsx");
            wb.write(fileOut);
            fileOut.close();
            wb.close();
        }

        public static void setCellData(XSSFSheet sheet)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Row row1 = sheet.createRow(0);
            // Create a cell and put a value in it.
            Cell cell11 = row1.createCell(0);
            cell11.setCellValue("Names");
            Cell cell12 = row1.createCell(1);
            cell12.setCellValue("falility");
            Cell cell13 = row1.createCell(2);
            cell13.setCellValue("date");
            Cell cell14 = row1.createCell(3);
            cell14.setCellValue("cost");
            Cell cell15 = row1.createCell(4);
            cell15.setCellValue("growth");


            Row row2 = sheet.createRow(1);
            Cell cell21 = row2.createCell(0);
            cell21.setCellValue("tom");
            Cell cell22 = row2.createCell(1);
            cell22.setCellValue("Nal stop");
            Cell cell23 = row2.createCell(2);
            Calendar cal = Calendar.getInstance();
            cal.set(2017,07,18);
            cell23.setCellValue(sdf.format(cal.getTime()));
            Cell cell24 = row2.createCell(3);
            cell24.setCellValue(10);
            Cell cell25 = row2.createCell(4);
            cell25.setCellValue(.18);


            Row row3 = sheet.createRow(2);
            Cell cell31 = row3.createCell(0);
            cell31.setCellValue("Ram");
            Cell cell32 = row3.createCell(1);
            cell32.setCellValue("Vadgao");
            Cell cell33 = row3.createCell(2);
            cal.set(2017,07,19);
            cell33.setCellValue(sdf.format(cal.getTime()));
            Cell cell34 = row3.createCell(3);
            cell34.setCellValue(12);
            Cell cell35 = row3.createCell(4);
            cell35.setCellValue(.12);

            Row row4 = sheet.createRow(3);
            Cell cell41 = row4.createCell(0);
            cell41.setCellValue("Terk");
            Cell cell42 = row4.createCell(1);
            cell42.setCellValue("Deccan");
            Cell cell43 = row4.createCell(2);
            cal.set(2017,07,20);
            cell43.setCellValue(sdf.format(cal.getTime()));
            Cell cell44 = row4.createCell(3);
            cell44.setCellValue(11);
            Cell cell45 = row4.createCell(4);
            cell45.setCellValue(.35);

            Row row5 = sheet.createRow(4);
            Cell cell51 = row5.createCell(0);
            cell51.setCellValue("tom");
            Cell cell52 = row5.createCell(1);
            cell52.setCellValue("baner");
            Cell cell53 = row5.createCell(2);
            cal.set(2017,07,18);
            cell53.setCellValue(sdf.format(cal.getTime()));
            Cell cell54 = row5.createCell(3);
            cell54.setCellValue(20);
            Cell cell55 = row5.createCell(4);
            cell55.setCellValue(.50);
        }
}

请帮助我,问题中提供的代码没有为Microsoft Excel创建正确的文件
Excel
不会打开该文件,但会抛出错误内容的错误,并提供修复文件的选项。只有在这些修复之后,才会显示透视表

Openoffice
Libreoffice
对此更宽容。因此,如果只需要为
Openoffice
Libreoffice
创建一个适当的文件,那么只需要对问题中提供的代码添加以下内容(我的添加内容没有缩进):

但是
*.xlsx
不是
Openoffice
Libreoffice
的默认文件格式,而是
Microsoft Excel
的默认文件格式。因此
*.xlsx
应该是
Microsoft Excel
的合适文件

要支持Microsoft Excel,需要正确的pivot缓存定义。为此,首先需要正确的透视字段项(我添加的内容没有缩进):

import java.io.*;
导入org.apache.poi.ss.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.xssf.usermodel.*;
导入java.util.Calendar;
导入java.text.simpleDataFormat;
公共类ApacheCreatePivotTable2
{
公共静态void main(字符串[]args)引发异常
{
XSSF工作簿wb=新XSSF工作簿();
XSSFSheet sheet=wb.createSheet();
//创建一些数据以构建数据透视表
setCellData(表格);
int endclonum=wb.getSheetAt(0.getRow(0.getLastCellNum()-1;
int endrownum=wb.getSheetAt(0.getLastRowNum();
AreaReference源=新的AreaReference(新的CellReference(0,0),新的CellReference(endrownum,endclonum));
XSSFSheet sheet2=wb.createSheet(“pivot”);
CellReference位置=新的CellReference(“A1”);//convertColStringToIndex
XSSFPivotTable pivotTable=sheet2.createPivotTable(源、位置、表);
数据透视表.addRowLabel(0);
数据透视表.addRowLabel(1);
//设置表格布局而不是树布局
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setOutline(false);
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setDefaultSubtotal(false);
数据透视表.addColumnLabel(DataConsolidateFunction.AVERAGE,3);
数据透视表.addColumnLabel(DataConsolidateFunction.SUM,3);
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
org.openxmlformats.schemas.spreadsheetml.x2006.main.statxis.AXIS_COL);
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2.addNewItems();
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);
//需要在位置0处插入新的ColField
数据透视表.getCTPivotTableDefinition().getColFields().insertNewField(0).setX(2);
数据透视表.getCTPivotTableDefinition().getColFields().setCount(2);
//下一段代码是为Microsoft Excel创建正确文件所必需的
对于(int i=0;i<3;i++){
//将前3项作为编号项目:
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0.getItems().getItemArray(i.Unset());
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().getItemArray(i).setX((长)i);
}
对于(int i=4;i>2;i--){
//删除其他项目
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0.getItems().removeItem(i);
}
//设置新项目计数
数据透视表.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0.getItems().setCount(3);
//构建缓存定义,其中包含这些项的共享元素
//
数据透视表.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0.getSharedItems().addNewS().setV(“tom”);
数据透视表.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0.getSharedItems().addNewS().setV(“Ram”);
数据透视表.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0.getSharedItems().addNewS().setV(“Terk”);
FileOutputStream fileOut=新的FileOutputStream(“output.xlsx”);
wb.写入(文件输出);
fileOut.close();
wb.close();
}
公共静态无效setCellData(XSSFSheet)
{
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
行row1=sheet.createRow(0);
//创建一个单元格并在其中输入一个值。
Cell cell11=row1.createCell(0);
cell11.setCellValue(“名称”);
Cell cell12=row1.createCell(1);
单元格12.设置单元格值(“错误”);
Cell cell13=row1.createCell(2);
cell13.设置CellValue(“日期”);
Cell cell14=row1.createCell(3);
cell14.设置CellValue(“成本”);
Cell cell15=row1.createCell(4);
cell15.setCellValue(“增长”);
row2行=sheet.createRow(1);
Cell cell21=row2.createCell(0);
cell21.setCellValue(“tom”);
Cell cell22=row2.createCell(1);
cell22.设置CellValue(“Nal停止”);
Cell cell23=row2.createCell(2);
Calendar cal=Calendar.getInstance();
...
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);

//new ColField needs to be inserted at position 0
pivotTable.getCTPivotTableDefinition().getColFields().insertNewField(0).setX(2);
pivotTable.getCTPivotTableDefinition().getColFields().setCount(2);
...
import java.io.*;

import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;

import java.util.Calendar;
import java.text.SimpleDateFormat;

  public class ApacheCreatePivotTable2
    {
        public static void main(String[] args) throws Exception
        {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet =  wb.createSheet();
            //Create some data to build the pivot table on
            setCellData(sheet);
            int endclonum = wb.getSheetAt(0).getRow(0).getLastCellNum()-1;
            int endrownum = wb.getSheetAt(0).getLastRowNum();
            AreaReference source = new AreaReference(new CellReference(0,0),new CellReference(endrownum,endclonum));
            XSSFSheet sheet2 = wb.createSheet("pivot");
            CellReference position = new CellReference("A1"); //convertColStringToIndex
            XSSFPivotTable pivotTable = sheet2.createPivotTable(source, position, sheet);
            pivotTable.addRowLabel(0);
            pivotTable.addRowLabel(1);

//set tabular layout instead of tree layout
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setOutline(false);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).setDefaultSubtotal(false);

            pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);
            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
                    org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);

//new ColField needs to be inserted at position 0
pivotTable.getCTPivotTableDefinition().getColFields().insertNewField(0).setX(2);
pivotTable.getCTPivotTableDefinition().getColFields().setCount(2);


//next code is necessary to create a proper file for Microsoft Excel

for (int i = 0; i < 3; i++) {
 //take the first 3 items as numbered items: <item x="0"/><item x="1"/>
 pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().getItemArray(i).unsetT();
 pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().getItemArray(i).setX((long)i);
}

for (int i = 4; i > 2; i--) {
 //remove further items
 pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().removeItem(i);
}

//set new items count
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().setCount(3);

//build a cache definition which has shared elements for those items 
//<sharedItems><s v="tom"/><s v="Ram"/><s v="Terk"/></sharedItems>
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0).getSharedItems().addNewS().setV("tom");
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0).getSharedItems().addNewS().setV("Ram");
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldList().get(0).getSharedItems().addNewS().setV("Terk");

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

        public static void setCellData(XSSFSheet sheet)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Row row1 = sheet.createRow(0);
            // Create a cell and put a value in it.
            Cell cell11 = row1.createCell(0);
            cell11.setCellValue("Names");
            Cell cell12 = row1.createCell(1);
            cell12.setCellValue("falility");
            Cell cell13 = row1.createCell(2);
            cell13.setCellValue("date");
            Cell cell14 = row1.createCell(3);
            cell14.setCellValue("cost");
            Cell cell15 = row1.createCell(4);
            cell15.setCellValue("growth");


            Row row2 = sheet.createRow(1);
            Cell cell21 = row2.createCell(0);
            cell21.setCellValue("tom");
            Cell cell22 = row2.createCell(1);
            cell22.setCellValue("Nal stop");
            Cell cell23 = row2.createCell(2);
            Calendar cal = Calendar.getInstance();
            cal.set(2017,07,18);
            cell23.setCellValue(sdf.format(cal.getTime()));
            Cell cell24 = row2.createCell(3);
            cell24.setCellValue(10);
            Cell cell25 = row2.createCell(4);
            cell25.setCellValue(.18);


            Row row3 = sheet.createRow(2);
            Cell cell31 = row3.createCell(0);
            cell31.setCellValue("Ram");
            Cell cell32 = row3.createCell(1);
            cell32.setCellValue("Vadgao");
            Cell cell33 = row3.createCell(2);
            cal.set(2017,07,19);
            cell33.setCellValue(sdf.format(cal.getTime()));
            Cell cell34 = row3.createCell(3);
            cell34.setCellValue(12);
            Cell cell35 = row3.createCell(4);
            cell35.setCellValue(.12);

            Row row4 = sheet.createRow(3);
            Cell cell41 = row4.createCell(0);
            cell41.setCellValue("Terk");
            Cell cell42 = row4.createCell(1);
            cell42.setCellValue("Deccan");
            Cell cell43 = row4.createCell(2);
            cal.set(2017,07,20);
            cell43.setCellValue(sdf.format(cal.getTime()));
            Cell cell44 = row4.createCell(3);
            cell44.setCellValue(11);
            Cell cell45 = row4.createCell(4);
            cell45.setCellValue(.35);

            Row row5 = sheet.createRow(4);
            Cell cell51 = row5.createCell(0);
            cell51.setCellValue("tom");
            Cell cell52 = row5.createCell(1);
            cell52.setCellValue("baner");
            Cell cell53 = row5.createCell(2);
            cal.set(2017,07,18);
            cell53.setCellValue(sdf.format(cal.getTime()));
            Cell cell54 = row5.createCell(3);
            cell54.setCellValue(20);
            Cell cell55 = row5.createCell(4);
            cell55.setCellValue(.50);
        }
    }