Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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中使用公式保存excel?_Java_Excel_Apache Poi - Fatal编程技术网

Java 如何在ApachePOI中使用公式保存excel?

Java 如何在ApachePOI中使用公式保存excel?,java,excel,apache-poi,Java,Excel,Apache Poi,如果存在带公式的单元格并保存文件,则手动关闭文件后,将要求excel保存更改?如何从程序中完全保存文件,因为如果不保存,我无法通过读取文件获得正确的值 当前文件保存: File file = new File("Test.xlsx"); FileOutputStream out; try { out = new FileOutputStream(file); workbook.wr

如果存在带公式的单元格并保存文件,则手动关闭文件后,将要求excel保存更改?如何从程序中完全保存文件,因为如果不保存,我无法通过读取文件获得正确的值

当前文件保存:

        File file = new File("Test.xlsx");
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            workbook.write(out);
            workbook.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
公式更新:

        XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
        workbook.setForceFormulaRecalculation(true);
公式:

XSSFCell irrCell = row1.createCell(0);
irrCell.setCellFormula("IFERROR(IRR(A2:C2),0)");
文件读取:

        try {
            workbook = new XSSFWorkbook(file);
            System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0).getNumericCellValue());
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
单元格值:-20000000,-1000,2399000


程序公式单元格值:0,文件公式单元格值:-0653687014

这是Microsoft不遵循其自身规则的情况之一。说明:

Microsoft Excel使用迭代技术计算内部收益率。 从guess开始,IRR循环计算,直到 结果准确度在0.00001%以内。如果IRR找不到结果 试了20次后效果就好了#NUM!返回错误值

在大多数情况下,您不需要提供内部收益率的猜测 计算。如果省略猜测,则假定其为0.1(10 百分比)

这正是
apachepoi
IRR
函数实现的。这就是为什么它会得到
#NUM数据样本的错误。但是微软似乎做了一些不同的事情

使用公式
IRR(A2:C2,-0.7)
,该公式具有
-0.7
的给定猜测,并且
apache poi
能够评估
IRR
公式

为了解决这个问题,我们可以做
IRR
函数描述还告诉我们的事情:

如果IRR给出#NUM!错误值,或者如果结果不接近 如您所料,请使用不同的“猜测”值重试

因此,尝试从-1.0到1.0,以0.1的步长进行猜测,直到得到一个值而不是“NUM!”错误

例如:

import java.io.FileInputStream;
import java.io.FileOutputStream;

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

class ExcelEvaluateIRRFunction {
 
 public static void main(String[] args) throws Exception {
  
  Workbook workbook = WorkbookFactory.create(new FileInputStream("./Excel.xlsx")); String filePath = "./ExcelNew.xlsx";
  FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

  Sheet sheet = workbook.getSheetAt(0);
  Row row = sheet.getRow(1); if (row == null) row = sheet.createRow(1);
  row.createCell(0).setCellValue(-20000000d);
  row.createCell(1).setCellValue(-1000d);
  row.createCell(2).setCellValue(2399000d);

  row = sheet.getRow(0); if (row == null) row = sheet.createRow(0);
  Cell cell = row.createCell(0);

  CellValue evaluatedValue = null;
  for (int i = -10; i < 10; i++) {
   double guess = i/10d;
   System.out.println(guess);
   cell.setCellFormula("IRR(A2:C2," + guess + ")");
   evaluator.clearAllCachedResultValues();
   evaluatedValue = evaluator.evaluate(cell);
   System.out.println(evaluatedValue);
   if (evaluatedValue.getCellType() != CellType.ERROR) break;
  }

  evaluator.evaluateFormulaCell(cell);
  System.out.println(cell.getNumericCellValue());

  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close() ;
  workbook.close();
 }
}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
类ExcelEvaluateIRRFunction{
公共静态void main(字符串[]args)引发异常{
工作簿=WorkbookFactory.create(新文件输入流(“./Excel.xlsx”);字符串filePath=“./ExcelNew.xlsx”;
FormulaEvaluator evaluator=工作簿.getCreationHelper().createFormulaEvaluator();
工作表=工作簿。getSheetAt(0);
Row Row=sheet.getRow(1);if(Row==null)Row=sheet.createRow(1);
row.createCell(0.setCellValue(-200000d);
row.createCell(1.setCellValue(-1000d);
row.createCell(2).setCellValue(239900d);
row=sheet.getRow(0);if(row==null)row=sheet.createRow(0);
Cell Cell=row.createCell(0);
CellValue evaluatedValue=null;
对于(int i=-10;i<10;i++){
双猜测=i/10d;
System.out.println(猜测);
setCellFormula(“IRR(A2:C2,“+guess+”));
evaluator.clearAllCachedResultValues();
evaluatedValue=evaluator.evaluate(单元格);
系统输出打印项次(评估值);
如果(evaluatedValue.getCellType()!=CellType.ERROR)中断;
}
评估人。评估公式(单元格);
System.out.println(cell.getNumericCellValue());
FileOutputStream out=新的FileOutputStream(filePath);
练习册。写(出);
out.close();
workbook.close();
}
}
有关相同问题的另一个示例,请参见