- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 POI 3.12-beta1:
<!-- Apache POI (for Excel) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
我正在尝试创建一个计算数据透视表列,其定义为:= 'Ended'/'Generated' * 100
。
我继续在 Excel 中手动编辑工作表以使其正常工作,当我将 *.xlsx
文件反转到 ZIP 目录并查看它时,我在\xl\pivotCache\pivotCacheDefinition1.xml
:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pivotCacheDefinition xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1" refreshOnLoad="1" refreshedBy="vyasrav" refreshedDate="42110.580247453705" createdVersion="3" refreshedVersion="3" minRefreshableVersion="3" recordCount="352">
<cacheSource type="worksheet">
<worksheetSource ref="A1:O353" sheet="Data"/>
</cacheSource>
<cacheFields count="16">
<!-- OMITTED -->
<cacheField name="Avg Pct Processed" numFmtId="0" formula="'Ended' / 'Generated' * 100" databaseField="0"/>
</cacheFields>
</pivotCacheDefinition>
所以我回到我的 java 程序并添加了以下代码以自动生成它,但它没有注册数据列“15”并且我收到了 IndexOutOfBounds 错误。
// Add pivot (pivot table):
Sheet pivotSheet = workbook.createSheet("Pivot");
LOGGER.trace("Created sheet: '" + String.valueOf(pivotSheet) + "'.");
XSSFPivotTable pivotTable = ((XSSFSheet)pivotSheet).createPivotTable(new AreaReference(tableRange), new CellReference("A1"), dataSheet);
CTPivotTableDefinition ctPivotTableDefinition = pivotTable.getCTPivotTableDefinition();
CTPivotTableStyle ctPivotTableStyle = ctPivotTableDefinition.getPivotTableStyleInfo();
ctPivotTableStyle.setName("PivotStyleMedium4");
// Row Labels:
pivotTable.addRowLabel(...); // ...
...
// Add column 15 (this is a calculated column):
CTCacheFields ctCacheFields = pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields();
CTCacheField ctCacheField = ctCacheFields.addNewCacheField();
ctCacheField.setName("Avg Pct Processed");
ctCacheField.setFormula("'Ended' / 'Generated' * 100");
// Column Labels:
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 8, "Sum of Generated");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 12, "Sum of Ended");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 13, "Sum of Unended");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 15, "Average of Processed Percent");
...
出现在上面粗体行的 IndexOutOfBoundsException 的 StackTrace 是:
Exception in thread "main" java.lang.IndexOutOfBoundsException at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTPivotFieldsImpl.setPivotFieldArray(Unknown Source) at org.apache.poi.xssf.usermodel.XSSFPivotTable.addDataColumn(XSSFPivotTable.java:372) at org.apache.poi.xssf.usermodel.XSSFPivotTable.addColumnLabel(XSSFPivotTable.java:296) at com...
Does anyone know how can I use POI to generate this column?
EDIT:
I tried using both:
CTPivotTableDefinition ctPivotTableDefinition = pivotTable.getCTPivotTableDefinition();
和
CTCacheField ctCacheField = ctCacheFields.insertNewCacheField(15);
在这两种情况下,执行此行时我都会遇到相同的异常:
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 15, "Average of Processed Percent");
请注意,我确实尝试注释掉添加新列标签的行,当我这样做时,如果我在 Excel 2010 中打开工作簿,它启动时会收到以下错误消息:
Removed Feature: PivotTable report from /xl/pivotTables/pivotTable1.xml part (PivotTable view)Removed Records: Workbook properties from /xl/workbook.xml part (Workbook)
谢谢!
最佳答案
我通过以下方式解决了您的问题:
//... get or create pivotTable
//Use first column as row label
pivotTable.addRowLabel(0);
// 1. Add Formula to cache
addFormulaToCache(pivotTable);
// 2. Add PivotField for Formula column
addPivotFieldForNewColumn(pivotTable);
// 3. Add all column labels before our function..
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
//Set the third column as filter
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);
// 4. Add formula column
addFormulaColumn(pivotTable);
下面是方法的实现:
private static void addFormulaToCache(XSSFPivotTable pivotTable) {
CTCacheFields ctCacheFields = pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields();
CTCacheField ctCacheField = ctCacheFields.addNewCacheField();
ctCacheField.setName("Field1"); // Any field name
ctCacheField.setFormula("'Ended' / 'Generated' * 100");
ctCacheField.setDatabaseField(false);
ctCacheField.setNumFmtId(0);
ctCacheFields.setCount(ctCacheFields.sizeOfCacheFieldArray()); //!!! update count of fields directly
}
private static void addPivotFieldForNewColumn(XSSFPivotTable pivotTable) {
CTPivotField pivotField = pivotTable.getCTPivotTableDefinition().getPivotFields().addNewPivotField();
pivotField.setDataField(true);
pivotField.setDragToCol(false);
pivotField.setDragToPage(false);
pivotField.setDragToRow(false);
pivotField.setShowAll(false);
pivotField.setDefaultSubtotal(false);
}
private static void addFormulaColumn(XSSFPivotTable pivotTable) {
CTDataFields dataFields;
if(pivotTable.getCTPivotTableDefinition().getDataFields() != null) {
dataFields = pivotTable.getCTPivotTableDefinition().getDataFields();
} else {
// can be null if we have not added any column labels yet
dataFields = pivotTable.getCTPivotTableDefinition().addNewDataFields();
}
CTDataField dataField = dataFields.addNewDataField();
dataField.setName("Avg Pct Processed");
// set index of cached field with formula - it is the last one!!!
dataField.setFld(pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCount()-1);
dataField.setBaseItem(0);
dataField.setBaseField(0);
}
关于java - XSSF (POI) - 将 "formula"列添加到数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29684662/
我正在尝试使用神经网络进行预测。 创建一些X: x f y ~ x1 + x2 ## fit model using `f` model model Call: neuralnet(formula
我要字段idEmpresa使用 idDepartamento 成为@Formula 字段(这是另一个@Formula 字段)在 WHERE陈述。 @Entity public class CfgUsu
我正在使用Google Apps脚本从概念数据库中查询数据,并在我的Google日历中创建日历事件。。我遇到了一些奇怪的行为,我搞不懂。。我的概念数据库为每个事件都有一个条目。每个事件都有开始和结束日
例如:将公式左侧的7个单元格相加。。值得注意的是,当在其左侧或范围内的某个位置插入新列时,该公式应该起作用。不扩展范围,不跳过新插入的列。
例如:将公式左侧的7个单元格相加。。值得注意的是,当在其左侧或范围内的某个位置插入新列时,该公式应该起作用。不扩展范围,不跳过新插入的列。
例如:将公式左侧的7个单元格相加。。值得注意的是,当在其左侧或范围内的某个位置插入新列时,该公式应该起作用。不扩展范围,不跳过新插入的列。
示例:对公式左侧的7个单元格求和。。值得注意的是,当在其左侧或范围内的某个位置插入新列时,该公式应该起作用。不扩展范围,不跳过新插入的列。
我需要帮助才能在 MS Excel 工作表中执行以下功能。工作表示例如下 A B C D E 1
好的, 我有一个射弹,其位置定义如下: a.x = initialX + initialDX * time; a.y = initialY + initialDY * time + 0.5 * gra
我正在尝试根据另一个点的经度计算一个点的经度。它们具有相同的纬度,并且它们之间的距离是已知的。我尝试使用余弦的特殊定律公式。 # 'lat' short for 'latitude', 'lng' s
我使用haversine公式来计算点之间的距离。这个公式的结果是米还是公里? http://en.wikipedia.org/wiki/Haversine_formula 任何人都可以帮助我吗?
取消使用鱼眼镜头或广角镜头制作的照片的最简单方法是什么?我正在寻找一个参数很少的像素投影公式。相机和镜头参数将不可知,因此用户必须直观地更改参数。谢谢 最佳答案 有好纸here这为镜头畸变提供了一些看
当使用来自 Formula package 的 Formula() 或 as.Formula() 时,我收到一条警告消息。它似乎不会影响功能,但我无法理解它的来源。 我正在使用 Formula 包更新
我正在使用这个公式来显示本月剩余的天数。是否可以在工作日内执行此操作? =EOMONTH(TODAY(),0)-TODAY() 最佳答案 分析工具包还提供函数 NETWORKDAYS 来查找两个日期之
我正在向字段添加公式: @Formula(value = "(select count(*) from approvalGroup as a where a.isAccounting=true)")
我想根据标准在下面的 C 列中使用 IF 公式发表评论: A B C 10 56% Leader 20 10% Laggard 55 45% Mover 90 90% Cashco
有没有更优雅(更简单)的方式将变量放入.formula?我不想使用 .formulaR1C1 我有这个代码: Range("C8").Select Selection.End(xlDown).Sele
我在 excel 中解析字符串,我需要通过最后一个数字返回所有内容。例如: Input: A00XX Output: A00 在我的情况下,我知道最后一个数字将在索引 3 和 5 之间,所以我用暴力强
我有一个包含两张纸的 LibreOffice Calc 文件。表 2 只有一列 A 有很多数字。在工作表 1 中,每行的 A 列也包含一个数字。我想从工作表 1 中删除所有在 A 列中具有值的行,这些
Excel的范围下拉(或拖拽)操作的公式是什么? 输入2,5,7,9然后向下拖动,显示11.5 13.8 16.1 18.4 ....步骤2.3 输入5,10,20然后向下拖动,显示26.66667
我是一名优秀的程序员,十分优秀!