当前位置: 首页 > 面试题库 >

如何使用Apache POI处理评论内容

亢保赫
2023-03-14
问题内容

我想在Docx文档中找到注释(以某种方式,通过作者或ID…),然后创建新内容。


问题答案:

正如我在您的问题中链接的答案中所说的那样,到目前为止,XWPFdocument在创建过程中,它只会读取该程序包的一部分。没有写访问权限,也没有创建该程序包部件的可能性。在XWPFDocument.java中提到了这一点-受保护的void
onDocumentRead()
:代码行210:“
// TODO根据XWPFComment类创建,扩展POIXMLDocumentPart”。

因此,我们需要自己做直到现在。我们需要提供扩展POIXMLDocumentPart的类以进行注释并注册此关系,而不是仅与简单POIXMLDocumentPart的关系。这样就可以更改在编写XWPFDocument时提交的更改。

例:

import java.io.*;

import org.apache.poi.*;
import org.apache.poi.openxml4j.opc.*;
import org.apache.xmlbeans.*;

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

import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;

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

import javax.xml.namespace.QName;

import java.math.BigInteger;
import java.util.GregorianCalendar;
import java.util.Locale;

public class WordChangeComments {

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

  XWPFDocument document = new XWPFDocument(new FileInputStream("WordDocumentHavingComments.docx"));

  for (POIXMLDocumentPart.RelationPart rpart : document.getRelationParts()) {
   String relation = rpart.getRelationship().getRelationshipType();
   if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
    POIXMLDocumentPart part = rpart.getDocumentPart(); //this is only POIXMLDocumentPart, not a high level class extending POIXMLDocumentPart
    //provide class extending POIXMLDocumentPart for comments 
    MyXWPFCommentsDocument myXWPFCommentsDocument = new MyXWPFCommentsDocument(part.getPackagePart());
    //and registering this relation instead of only relation to POIXMLDocumentPart
    String rId = document.getRelationId(part);
    document.addRelation(rId, XWPFRelation.COMMENT, myXWPFCommentsDocument);

    //now the comments are available from the new MyXWPFCommentsDocument 
    for (CTComment ctComment : myXWPFCommentsDocument.getComments().getCommentArray()) {

     System.out.print("Comment: Id: " + ctComment.getId());
     System.out.print(", Author: " + ctComment.getAuthor());
     System.out.print(", Date: " + ctComment.getDate());
     System.out.print(", Text: ");
     for (CTP ctp : ctComment.getPArray()) {
      System.out.print(ctp.newCursor().getTextValue());
     }
     System.out.println();

     //and changings can be made which were committed while writing the XWPFDocument
     if (BigInteger.ONE.equals(ctComment.getId())) { //the second comment (Id 0 = first)
      ctComment.setAuthor("New Author");
      ctComment.setInitials("NA");
      ctComment.setDate(new GregorianCalendar(Locale.US));
      CTP newCTP = CTP.Factory.newInstance();
      newCTP.addNewR().addNewT().setStringValue("The new Text for Comment with Id 1.");
      ctComment.setPArray(new CTP[]{newCTP });
     }
    }
   }
  }

  document.write(new FileOutputStream("WordDocumentHavingComments.docx"));

  document.close();
 }

//a wrapper class for the CommentsDocument /word/comments.xml in the *.docx ZIP archive
 private static class MyXWPFCommentsDocument extends POIXMLDocumentPart {

  private CTComments comments;

  private MyXWPFCommentsDocument(PackagePart part) throws Exception {
   super(part);
   comments = CommentsDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS).getComments();
  }

  private CTComments getComments() {
   return comments;
  }

  @Override
  protected void commit() throws IOException {

System.out.println("============MyXWPFCommentsDocument is committed=================");

   XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
   xmlOptions.setSaveSyntheticDocumentElement(new QName(CTComments.type.getName().getNamespaceURI(), "comments"));
   PackagePart part = getPackagePart();
   OutputStream out = part.getOutputStream();
   comments.save(out, xmlOptions);
   out.close();
  }

 }

}

这适用于apache poi 3.17。由于apache poi 4.0.0ooxml部分被分开。因此必须有:

...
import org.apache.poi.ooxml.*;
...
import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
...


 类似资料:
  • 通过此处可以删除、审核文章评论 你可以根据自己的需要进行配置。

  • 获取评论总数 获取评论列表 删除评论 设置评论置顶 观众禁言-禁止评论 发布官方评论

  • 我有一个简单的批处理测试文件test.bat如下行: 当我运行它时,我希望得到的是测试,而不是: 为什么batch会试图解释这条评论?或者这里发生了什么?如果我去掉注释,脚本会按预期打印测试。 文件中也没有提到这一点。

  • 问题内容: 我尝试了soup.find(’!-‘),但似乎没有用。提前致谢。 编辑:感谢您有关如何查找所有评论的提示。我有一个后续问题。如何专门搜索评论? 例如,我有以下注释标记: 我真的只想要这些东西。“ 110518”是我要用作搜索目标的日期YYMMDD。但是,我不知道如何在特定的注释标签中找到某些内容。 问题答案: Pyparsing允许您使用内置表达式搜索HTML注释,并附加解析时回调以验

  • 打开已群发文章评论 $app->comment->open($msgId, $index = null); 关闭已群发文章评论 $app->comment->close($msgId, $index = null); 查看指定文章的评论数据 $app->comment->list(string $msgId, int $index, int $begin, int $count, int $t

  • 获取所有评论 GET /comments 请求查询参数: 名字 类型 描述 limit integer 可选,本次请求需要返回的数据条数。 index integer 可选,查询开始的评论位置,来源响应 id 字段。 direction string 可选,数据排序方向,以 id 进行排序,支持 asc 或 desc,默认 desc。 author integer 可选,需要筛选的评论作者,传递