java - 如何在java中使用aspose word将FieldFormula添加到Cell中

标签 java aspose aspose-cells

是否可以使用 aspose word java API 将公式添加到表中。 我编写了一个代码来生成聚合行,但我搜索了与公式内容控制相关的内容。

我找到了API FieldFormula ,该字段API。我们可以在单元格中添加字段吗?

  private void prepareAggregateRow(Table table, Row headerRow, Map<String, WsAttribute> attrValueMap, List<Attribute> attrDefs) throws Exception{
        Row totalRow = (Row)headerRow.deepClone(true);
        for (Attribute attribute : attrDefs) {
            WsAttribute wsAttribute = attrValueMap.get(attribute.getName());
            Cell cell = totalRow.getCells().get(attribute.getIdx());
            Run run = (Run)cell.getChild(NodeType.RUN, 0,true);
            if(wsAttribute!=null && wsAttribute.getValue()!=null) {
                run.setText(wsAttribute.getValue());
            } else {
                run.setText("");
            }
        }
        table.appendChild(totalRow);
    }

最佳答案

FieldBuilder类从字段代码标记(参数和开关)构建一个字段。以下代码示例将 FieldFormula 插入表的单元格中。希望这对您有帮助。

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Cell cell = table.getFirstRow().getFirstCell();
builder.moveTo(cell.getFirstParagraph());

FieldBuilder fbuilder = new FieldBuilder(FieldType.FIELD_FORMULA);
fbuilder.addArgument("20000").addSwitch("\\# \"#,##0\"").buildAndInsert(builder.getCurrentParagraph());

doc.updateFields();
doc.save(MyDir + "Out.docx");

我作为开发者布道者与 Aspose 合作。

关于java - 如何在java中使用aspose word将FieldFormula添加到Cell中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38870098/

相关文章:

.net - 在新文档中复制项目符号列表编号?

excel - 使用 Aspose Cells 在同一个 Excel 工作表上的多组单选按钮

java - 理解java中的同步

Java分隔符跳过一个单词

java - Jar 位于类路径中但拒绝工作

java - 如何从java中的for循环中减去

c# - x 轴上的自定义标签

java - 无法找出 NoClassDefFoundError : aspose/pdf/Paragraph 的原因