对我来说它给出了#NUM!电流误差apache poi 4.1.0
, not NaN
.
问题在于你给出的猜测。在IRR函数 https://support.office.com/en-us/article/IRR-function-64925EAA-9988-495B-B290-3AD0C163C1BC据称:
Guess选修的。您猜测的数字接近 IRR 的结果。
Microsoft Excel 使用迭代技术来计算 IRR。从猜测开始,IRR 循环计算,直到
结果准确度在 0.00001% 以内。如果 IRR 找不到结果
20 次尝试后有效,#NUM!返回错误值。
在大多数情况下,您不需要为 IRR 计算提供猜测。如果省略猜测,则假定为 0.1 (10
百分)。
如果 IRR 给出#NUM!错误值,或者如果结果与您的预期不接近,请使用不同的猜测值重试。
你的结果IRR
将为 -0.050193141Excel
。但你的猜测是0.1。所以用这个猜测内部apache poi
IRR
尝试 20 次后,函数未找到精确度在 0.00001% 以内的结果。所以#NUM!返回错误值。
Why apache poi
's IRR
功能与中不同Excel
?好吧,因为那部分Excel
不是开源的。所以没有人真正知道它是如何运作的。
Using a guess-0.1 对我有用。
Example:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class ExcelEvaluateIRR {
public static void main(String[] args) throws Exception {
try (Workbook workbook = new XSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
Double[] values = new Double[] {
-1.0601017230994111E8,
19150.63,
44505.08,
22997.34,
33936.39,
27265.92,
2127.66,
2108.63,
886.53,
2482.27,
4305.12,
3421.58,
65644.12,
1020.51,
2659.57,
3191.49,
20284508.4,
1881279.27,
11675415.09,
7557862.28,
921090.46,
622104.32,
289267.36,
183.41,
886.53
};
Sheet sheet = workbook.createSheet();
Row row = null;
Cell cell = null;
for (int r = 0; r < values.length; r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue(values[r]);
}
row = sheet.createRow(values.length);
cell = row.createCell(0);
//cell.setCellFormula("IRR(A1:A" + values.length + ",0.1)"); // will not work
cell.setCellFormula("IRR(A1:A" + values.length + ",-0.1)"); // will work
FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellValue cellValue = formulaEvaluator.evaluate(cell);
System.out.println(cellValue);
workbook.write(fileout);
}
}
}