为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 apache poi 在 docx 中计算 =SUM(Above) 函数

如何使用 apache poi 在 docx 中计算 =SUM(Above) 函数

牛魔王的故事 2023-04-26 17:12:53
我正在尝试使用 apache poi 来处理 docx 格式文件,但我一直坚持使用表格中的公式。例如看图片:我确实尝试将文本设置为“=SUM(ABOVE)”,但这种方式不起作用。我想我可能需要在这里设置自定义 xml 数据,但我不确定如何进行。我尝试了以下代码:              XWPFTable table = document.createTable();              //create first row              XWPFTableRow tableRowOne = table.getRow(0);              table.getRow(0).createCell();              table.getRow(0).getCell(0).setText("10");              table.getRow(0).createCell();              table.getRow(0).getCell(1).setText("=SUM(ABOVE)");
查看完整描述

1 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

在这种情况下,我正在做的事情如下:首先,Word使用Word GUI. 然后查看Word已创建的内容,了解需要使用apache poi.


具体来说:创建最简单的表,其中Word有一个字段{=SUM(ABOVE)}。将其另存为*.docx. 现在解压缩*.docx(Office Open XML 文件*.docx只是ZIP存档)。看看/word/document.xml那个档案。在那里你会发现类似的东西:


<w:tc>

 <w:p>

  <w:fldSimple w:instr="=SUM(ABOVE)"/>

 ...

 </w:p>

</w:tc>

这XML适用于具有段落的表格单元格,fldSimple段落中有一个元素,其中instr属性包含公式。


现在我们知道,我们需要表格单元格XWPFTableCell和XWPFParagraph其中的 。然后我们需要 fldSimple在此段落中设置一个元素,其中instr属性包含公式。


这很简单


paragraphInCell.getCTP().addNewFldSimple().setInstr("=SUM(ABOVE)");

但是当然有些东西必须告诉Word在文档打开时需要计算公式。最简单的解决方案是将字段设置为“脏”。这导致需要在打开文档时更新字段Word。它还会导致有关更新需求的确认消息对话框。


完整示例使用apache poi 4.1.0:


import java.io.FileOutputStream;


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


import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;


public class CreateWordTableSumAbove {


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


  XWPFDocument document= new XWPFDocument();


  XWPFParagraph paragraph = document.createParagraph();

  XWPFRun run=paragraph.createRun();  

  run.setText("The table:");


  //create the table

  XWPFTable table = document.createTable(4,3);

  table.setWidth("100%");

  for (int row = 0; row < 3; row++) {

   for (int col = 0; col < 3; col++) {

    if (col < 2) table.getRow(row).getCell(col).setText("row " + row + ", col " + col);

    else table.getRow(row).getCell(col).setText("" + ((row + 1) * 1234));

   }

  }


  //set Sum row

  table.getRow(3).getCell(0).setText("Sum:");


  //get paragraph from cell where the sum field shall be contained

  XWPFParagraph paragraphInCell = null;

  if (table.getRow(3).getCell(2).getParagraphs().size() == 0) paragraphInCell = table.getRow(3).getCell(2).addParagraph();

  else paragraphInCell = table.getRow(3).getCell(2).getParagraphs().get(0);


  //set sum field in

  CTSimpleField sumAbove = paragraphInCell.getCTP().addNewFldSimple();

  sumAbove.setInstr("=SUM(ABOVE)");

  //set sum field dirty, so it must be calculated while opening the document

  sumAbove.setDirty(STOnOff.TRUE);


  paragraph = document.createParagraph();


  FileOutputStream out = new FileOutputStream("create_table.docx"); 

  document.write(out);

  out.close();

  document.close();

 }

}

这一切只有在使用打开文档时才能正常工作Microsoft Word。LibreOffice Writer无法将此类公式字段存储为Office Open XML( *.docx) 格式,也无法Office Open XML正确读取此类公式字段。


查看完整回答
反对 回复 2023-04-26
  • 1 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信