Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ApachePOI:如何在数据透视的行标签日期中设置此年过滤器_Java_Excel_Apache Poi - Fatal编程技术网

Java ApachePOI:如何在数据透视的行标签日期中设置此年过滤器

Java ApachePOI:如何在数据透视的行标签日期中设置此年过滤器,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试使用ApachePOI在excel中创建一个pivot。 我的要求是应用日期过滤器(今年),即在透视图的日期行标签中仅显示当前年份的日期 问题:在生成输出excel文件的可复制代码下方,日期列应用了“今年”过滤器,但由于某些原因,它没有显示任何数据 下面是生成输出的代码 import java.io.FileOutputStream; import org.apache.poi.ss.*; import org.apache.poi.ss.usermodel.*; import or

我正在尝试使用ApachePOI在excel中创建一个pivot。 我的要求是应用日期过滤器(今年),即在透视图的日期行标签中仅显示当前年份的日期

问题:在生成输出excel文件的可复制代码下方,日期列应用了“今年”过滤器,但由于某些原因,它没有显示任何数据

下面是生成输出的代码

import java.io.FileOutputStream;

import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;

import java.util.GregorianCalendar;

class CreatePivotTableFilter {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   DataFormat format = workbook.createDataFormat();
   CellStyle dateStyle = workbook.createCellStyle();
   dateStyle.setDataFormat(format.getFormat("M\\/d\\/yy"));

   Sheet sheet = workbook.createSheet();

   String[] headers = new String[]{"Column1", "Column2", "Date", "Count"};
   Row row = sheet.createRow(0);
   Cell cell;
   for (int c = 0; c < headers.length; c++) {
    cell = row.createCell(c); cell.setCellValue(headers[c]);
   }

   Object[][] data = new Object[][]{
    new Object[]{"A", "B1", new GregorianCalendar(2020, 0, 2), 2d},
    new Object[]{"A", "B2", new GregorianCalendar(2020, 0, 1), 4d},
    new Object[]{"B", "B1", new GregorianCalendar(2019, 0, 2), 1d},
    new Object[]{"B", "B2", new GregorianCalendar(2019, 0, 2), 7d},
    new Object[]{"A", "C1", new GregorianCalendar(2019, 0, 1), 5d},
    new Object[]{"A", "C2", new GregorianCalendar(2019, 0, 1), 5d},
    new Object[]{"B", "C1", new GregorianCalendar(2019, 0, 2), 2d},
    new Object[]{"B", "C2", new GregorianCalendar(2019, 0, 2), 8d}
   };
   for (int r = 0; r < data.length; r++) {
    row = sheet.createRow(r+1);
    Object[] rowData = data[r];
    for (int c = 0; c < rowData.length; c++) {
     cell = row.createCell(c);
     if (rowData[c] instanceof String) {
      cell.setCellValue((String)rowData[c]);
     } else if (rowData[c] instanceof GregorianCalendar) {
      cell.setCellValue((GregorianCalendar)rowData[c]);
      cell.setCellStyle(dateStyle);
     } else if (rowData[c] instanceof Double) {
      cell.setCellValue((Double)rowData[c]);
     }
    }
   }

   XSSFPivotTable pivotTable = ((XSSFSheet)sheet).createPivotTable(
    new AreaReference("A1:D9", 
    SpreadsheetVersion.EXCEL2007), 
    new CellReference("F4"));

   pivotTable.addRowLabel(0);
   pivotTable.addRowLabel(1);
   pivotTable.addRowLabel(2);
 
   pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
   pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);

   CTPivotFilters filters = CTPivotFilters.Factory.newInstance();
   CTPivotFilter filter = filters.addNewFilter();
   filter.setId(0);
   filter.setFld(2);
   filter.setType(STPivotFilterType.THIS_YEAR);
   CTFilterColumn filterColumn = filter.addNewAutoFilter().addNewFilterColumn();
   filterColumn.setColId(0);
   CTFilters ctFilters = filterColumn.addNewFilters();
   ctFilter.addNewFilter().setVal("This Year Filter");
    
   //set filters to pivot table definition
   pivotTable.getCTPivotTableDefinition().setFilters(filters);

   workbook.write(fileout);

  }

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.xssf.usermodel.*;
导入org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
导入java.util.GregorianCalendar;
类CreatePivotTableFilter{
公共静态void main(字符串[]args)引发异常{
尝试(工作簿=新XSSFWORKWORK();
FileOutputStream fileout=新的FileOutputStream(“Excel.xlsx”)){
DataFormat=workbook.createDataFormat();
CellStyle dateStyle=workbook.createCellStyle();
dateStyle.setDataFormat(format.getFormat(“M\\/d\\/yy”);
工作表=工作簿.createSheet();
字符串[]头=新字符串[]{“Column1”、“Column2”、“Date”、“Count”};
Row Row=sheet.createRow(0);
细胞;
for(int c=0;c
由代码生成的输出

预期产出


处理低级
ooxml模式
类的最佳方法是使用
Excel
GUI
创建任何需要的内容,然后解压缩生成的
*.xlsx
文件,并查看
XML
GUI
创建的内容

在这种情况下,在
xl/pivotTables/pivotTable1.xml
中,我们发现:

...
<filters>
 <filter fld="2" type="thisYear" id="0">
  <autoFilter>
   <filterColumn colId="0">
    <dynamicFilter type="thisYear"/>
   </filterColumn>
  </autoFilter>
 </filter>
</filters>
...

不幸的是,没有任何
API
文档可以公开使用
ooxml模式。因此,如果我们需要它,我们需要从
maven
下载
ooxmlschemas
源代码。然后我们可以使用
javadoc
创建
API
文档。在那里,我们可以找到
CTFilterColumn
的字段和方法,例如。

感谢您对apache poi问题如此及时和积极。您在社区中的回答在创建非常高级的excel数据透视方面帮助了我很多。
...
   CTPivotFilters filters = CTPivotFilters.Factory.newInstance();
   CTPivotFilter filter = filters.addNewFilter();
   filter.setId(0);
   filter.setFld(2);
   filter.setType(STPivotFilterType.THIS_YEAR);
   CTFilterColumn filterColumn = filter.addNewAutoFilter().addNewFilterColumn();
   filterColumn.setColId(0);
   CTDynamicFilter ctDynamicFilter = filterColumn.addNewDynamicFilter();
   ctDynamicFilter.setType(STDynamicFilterType.THIS_YEAR);
    
   //set filters to pivot table definition
   pivotTable.getCTPivotTableDefinition().setFilters(filters);
...