【问题标题】:Why does the logical IFS() function keep getting rendered in lowercase?为什么逻辑 IFS() 函数总是以小写形式呈现?
【发布时间】:2019-06-14 22:14:47
【问题描述】:

我正在从 java 代码动态生成 XLSX 文件,并设置公式。除了 IFS() 函数外,所有工作都正常,而似乎总是使用小写的“ifs()”进行渲染,因此当打开生成的文件时,libreoffice 无法将其识别为函数。所有其他公式,例如普通的旧“IF”工作得很好

我已经尝试调试 POI ooxml 源代码,最后我能说的是单元格被正确设置为大写。我已经尝试更新到最新版本,预先格式化单元格内容......到目前为止还没有运气。此代码在 poi 4.0.1 上运行,我正在使用 libreoffice 6.1.3.2 打开文件(以防这可能是 libreoffice 问题?)。我无权访问 EXCEL 2016+ 来检查它如何处理生成的文件。

public void testIFS(){
    try {
        String IFSformula = "IFS(1,\"yes\",0,\"no\")";
        String IFformula = "IF(1,\"yes\",\"no\")";
        String outputFileName = "IFStest.xlsx";
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet poiSheet = workbook.createSheet("ifstest");
        XSSFRow row = poiSheet.createRow(0);
        XSSFCell cell0 = row.createCell(0);
        cell0.setCellFormula(IFSformula);
        XSSFCell cell1 = row.createCell(1);
        cell1.setCellFormula(IFformula);
        workbook.write(new FileOutputStream(outputFileName));
    } catch (IOException ex) {
        Logger.getLogger(IFSTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

现在,单元格 0 以 =ifs(1,"yes",0,"no") 结尾 - 这是错误的(结果是 #NAME),但单元格 1 工作得很好并且有一个单元格公式 =IF( 1,"yes","no")(结果为“yes”)。如果我手动将“ifs”更改为“IFS”,则公式可以正常工作并且还显示“是”。

【问题讨论】:

  • 无法在本地模拟它,但以防万一这有帮助 - 您可以在 IF(): IF((1)*(1)*(1),"yes","no") 内执行多个逻辑条件
  • 您是否检查过您的 XML 将文件标识为 Office 2016 .xlsx(支持 IFS),而不是 Office 2007 .xlsx(不支持 IFS
  • IFS 不是 Excel 2013 中可识别的函数
  • @Glitch_Doctor - 我知道,但这仍然只允许 2 个结果,真假,我特别需要 X 个可能的结果。

标签: excel excel-formula apache-poi libreoffice-calc


【解决方案1】:

最新的LibreOffice Calc 支持IFS。但如果它保存*.xlsx 文件,那么它使用_xlfn 前缀存储IFS 公式。通常_xlfn 前缀表示The Excel workbook contains a function that is not supported in the version of Excel that you are currently running.。所以似乎LibreOffice Calc 尝试在Excel 2016 兼容模式下保存。 IFS function 仅从 Office 365 向上。而且由于它使用该前缀进行存储,因此在阅读 *.xlsx 时似乎也需要该前缀。

甚至 Office 365 Excel_xlfn.IFS 存储到 *.xlsx 文件中,而不仅仅是 IFS(今天测试,2019 年 1 月 21 日)。所以LibreOffice Calc 期望这个前缀也是正确的。

以下适用于我,使用apache poi 4.0.1 创建*.xlsx 并使用LibreOffice Calc(版本:6.0.7.3 构建ID:1:6.0.7-0ubuntu0.18.04.2)以及使用@987654342 @ 用于打开 *.xlsx 然后。

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

class TestIFS {

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

  try (XSSFWorkbook workbook = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream("TestIFS.xlsx")) { 
   String formulaIFS = "_xlfn.IFS(1=0,\"first\",0=0,\"second\")";
   String formulaIF = "IF(1=0,\"yes\",\"no\")";
   Sheet sheet = workbook.createSheet("IFStest");
   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);
   cell.setCellFormula(formulaIFS);
   cell = row.createCell(1);
   cell.setCellFormula(formulaIF);
   workbook.write(out); 
  }
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多