是否可以使用 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/