栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过Apache Poi添加评论

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过Apache Poi添加评论

在Office OpenXML Word文档(

XWPF
)中,注释是
Commentsdocument
/word/comments.xml
*
.docx
ZIP存档中的特殊内容。因此,首先我们需要访问该文档。但是直到现在,它们
XWPFdocument
只会在创建时读取该软件包的一部分。没有写访问权限,也没有创建该程序包部件的可能性。

因此,我们首先必须提供这样一种可能性,以便

/word/comments.xml
在* .docx ZIP归档文件中创建程序包部件并对其进行写访问。

在下面的示例中,该方法

MyXWPFCommentsdocument createCommentsdocument(XWPFdocumentdocument)
创建包装零件
/word/comments.xml
及其相关关系。该类
MyXWPFCommentsdocument
是对该包装部件具有写访问权的包装器类。

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 CreateWordWithComments {//a method for creating the Commentsdocument /word/comments.xml in the *.docx ZIP archive   private static MyXWPFCommentsdocument createCommentsdocument(XWPFdocument document) throws Exception {  OPCPackage oPCPackage = document.getPackage();  PackagePartName partName = PackagingURIHelper.createPartName("/word/comments.xml");  PackagePart part = oPCPackage.createPart(partName, "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml");  MyXWPFCommentsdocument myXWPFCommentsdocument = new MyXWPFCommentsdocument(part);  String rId = "rId" + (document.getRelationParts().size()+1);  document.addRelation(rId, XWPFRelation.COMMENT, myXWPFCommentsdocument);  return myXWPFCommentsdocument; } public static void main(String[] args) throws Exception {  XWPFdocument document = new XWPFdocument();  MyXWPFCommentsdocument myXWPFCommentsdocument = createCommentsdocument(document);  CTComments comments = myXWPFCommentsdocument.getComments();  CTComment ctComment;  XWPFParagraph paragraph;  XWPFRun run;//first comment  BigInteger cId = BigInteger.ZERO;  ctComment = comments.addNewComment();  ctComment.setAuthor("Axel Ríchter");  ctComment.setInitials("AR");  ctComment.setDate(new GregorianCalendar(Locale.US));  ctComment.addNewP().addNewR().addNewT().setStringValue("The first comment.");  ctComment.setId(cId);  paragraph = document.createParagraph();  paragraph.getCTP().addNewCommentRangeStart().setId(cId);  run = paragraph.createRun();  run.setText("Paragraph with the first comment.");  paragraph.getCTP().addNewCommentRangeEnd().setId(cId);  paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);//paragraph without comment  paragraph = document.createParagraph();  run = paragraph.createRun();  run.setText("Paragraph without comment.");//second comment  cId = cId.add(BigInteger.ONE);  ctComment = comments.addNewComment();  ctComment.setAuthor("Axel Ríchter");  ctComment.setInitials("AR");  ctComment.setDate(new GregorianCalendar(Locale.US));  ctComment.addNewP().addNewR().addNewT().setStringValue("The second comment.");  ctComment.setId(cId);  paragraph = document.createParagraph();  paragraph.getCTP().addNewCommentRangeStart().setId(cId);  run = paragraph.createRun();  run.setText("Paragraph with the second comment.");  paragraph.getCTP().addNewCommentRangeEnd().setId(cId);  paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);//write document  document.write(new FileOutputStream("CreateWordWithComments.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.newInstance().addNewComments();  }  private CTComments getComments() {   return comments;  }  @Override  protected void commit() throws IOException {   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.0
ooxml
部分被分开。因此必须有:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号