我正在开发一个项目,我需要从现有的Excel文件创建数据透视表。我能够加载文件并创建数据透视表,但是当我想为数据透视表设置过滤器时,我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/CTPageFields
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTPivotTableDefinitionImpl.addNewPageFields(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFPivotTable.addReportFilter(XSSFPivotTable.java:410)
at com.mycompany.testmaven.PivotTable.main(PivotTable.java:36)
Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageFields
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
我不确定为什么会这样。以下是我的代码:
package com.mycompany.testmaven;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.AreaReference;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.*;
import org.apache.poi.ss.usermodel.DataConsolidateFunction;
import org.apache.poi.xssf.usermodel.XSSFPivotTable;
public class PivotTable {
public static void main(String[] args) throws Exception{
/* Read the input file that contains the data to pivot */
FileInputStream input_document = new FileInputStream(new File("C:\\Users\\User\\Desktop\\NameOfFile.xlsx"));
/* Create a POI XSSFWorkbook Object from the input file */
XSSFWorkbook my_xlsx_workbook = new XSSFWorkbook(input_document);
/* Read Data to be Pivoted - we have only one worksheet */
XSSFSheet sheet = my_xlsx_workbook.getSheetAt(0);
/* Get the reference for Pivot Data */
AreaReference a=new AreaReference("A1:O136448");
/* Find out where the Pivot Table needs to be placed */
CellReference b=new CellReference("S5");
/* Create Pivot Table */
XSSFPivotTable pivotTable = sheet.createPivotTable(a,b);
/* Add filters */
pivotTable.addDataColumn(8, true);
pivotTable.addReportFilter(8);
//pivotTable.addRowLabel(1);
//pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2);
/* Write Pivot Table to File */
FileOutputStream output_file = new FileOutputStream(new File("C:\\Users\\User\\Desktop\\POI_XLS_Pivot_Example.xlsx"));
my_xlsx_workbook.write(output_file);
input_document.close();
}
}
非常感谢任何帮助。我使用的是Java 8,Apache Maven 3.3.9和Apache POI 3.15。