首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以使用Apache POI在Word dokument中的特定位置插入HTML

是否可以使用Apache POI在Word dokument中的特定位置插入HTML
EN

Stack Overflow用户
提问于 2021-10-25 20:22:03
回答 1查看 232关注 0票数 0

我正在尝试使用Apache POI在ms Word文档中的特定位置插入HTML文本。

我一直在遵循我通过Yaun找到here的指示。这是一个很好的例子,但只展示了如何在文档的最后添加HTML。是不是因为它不可能插入到其他地方,或者有人知道如何插入,并且有时间向我解释或为我指出正确的方向?

提前感谢!

弗雷德里克

EN

Stack Overflow用户

回答已采纳

发布于 2021-10-27 12:16:46

要将某些内容插入XWPFDocument的主体中,需要一个指向插入位置的XmlCursor。在方法XWPFDocument. insertNewParagraph(org.apache.xmlbeans.XmlCursor cursor)XWPFDocument.insertNewTbl(org.apache.xmlbeans.XmlCursor cursor)中已经做了同样的事情。

所以我们需要一个方法insertAltChunk(XWPFDocument document, MyXWPFHtmlDocument myXWPFHtmlDocument, XmlCursor cursor)来将altChunk插入到document的位置cursor,指向myXWPFHtmlDocumentId

代码语言:javascript
运行
复制
...
 boolean isCursorInBody(XWPFDocument document, XmlCursor cursor) {
  XmlCursor verify = cursor.newCursor();
  verify.toParent();
  boolean result = (verify.getObject() == document.getDocument().getBody());
  verify.dispose();
  return result;
 }
 
 void insertAltChunk(XWPFDocument document, MyXWPFHtmlDocument myXWPFHtmlDocument, XmlCursor cursor) {
  if (isCursorInBody(document, cursor)) {
   QName ALTCHUNK = new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "altChunk");
   QName ID = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id");
   cursor.beginElement(ALTCHUNK);
   cursor.insertAttributeWithValue(ID, myXWPFHtmlDocument.getId());
   cursor.dispose();   
  }  
 }
...

完整的示例,基于我在How to add an altChunk element to a XWPFDocument using Apache POI中的代码,它在现有Word文档中的第二和第五段之前插入altChunk

代码语言:javascript
运行
复制
import java.io.*;

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

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

import org.apache.xmlbeans.XmlCursor;
import javax.xml.namespace.QName;

public class InsertHTMLaltChunkInWordAtCursor {

 //a method for creating the htmlDoc /word/htmlDoc#.html in the *.docx ZIP archive  
 //String id will be htmlDoc#.
 private static MyXWPFHtmlDocument createHtmlDoc(XWPFDocument document, String id) throws Exception {
  OPCPackage oPCPackage = document.getPackage();
  PackagePartName partName = PackagingURIHelper.createPartName("/word/" + id + ".html");
  PackagePart part = oPCPackage.createPart(partName, "text/html");
  MyXWPFHtmlDocument myXWPFHtmlDocument = new MyXWPFHtmlDocument(part, id);
  document.addRelation(myXWPFHtmlDocument.getId(), new XWPFHtmlRelation(), myXWPFHtmlDocument);
  return myXWPFHtmlDocument;
 }
 
 private static boolean isCursorInBody(XWPFDocument document, XmlCursor cursor) {
  XmlCursor verify = cursor.newCursor();
  verify.toParent();
  boolean result = (verify.getObject() == document.getDocument().getBody());
  verify.dispose();
  return result;
 }
 
 private static void insertAltChunk(XWPFDocument document, MyXWPFHtmlDocument myXWPFHtmlDocument, XmlCursor cursor) {
  if (isCursorInBody(document, cursor)) {
   QName ALTCHUNK = new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "altChunk");
   QName ID = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id");
   cursor.beginElement(ALTCHUNK);
   cursor.insertAttributeWithValue(ID, myXWPFHtmlDocument.getId());
   cursor.dispose();   
  }  
 }

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

  XWPFDocument document = new XWPFDocument(new FileInputStream("./WordDocument.docx"));
  
  XWPFParagraph paragraph;

  MyXWPFHtmlDocument myXWPFHtmlDocument;

  myXWPFHtmlDocument = createHtmlDoc(document, "htmlDoc1");
  myXWPFHtmlDocument.setHtml(myXWPFHtmlDocument.getHtml().replace("<body></body>",
   "<body><p>Simple <b>HTML</b> <i>formatted</i> <u>text</u></p></body>"));
  paragraph = document.getParagraphArray(1);
  XmlCursor cursor = paragraph.getCTP().newCursor();
  insertAltChunk(document, myXWPFHtmlDocument, cursor);
  
  myXWPFHtmlDocument = createHtmlDoc(document, "htmlDoc2");
  myXWPFHtmlDocument.setHtml(myXWPFHtmlDocument.getHtml().replace("<body></body>",
   "<body>" +
   "<table border=\"1\">"+
   "<caption>A table</caption>" +
   "<tr><th>Name</th><th>Date</th><th>Amount</th></tr>" +
   "<tr><td>John Doe</td><td>2018-12-01</td><td>1,234.56</td></tr>" +
   "</table>" +
   "</body>"
   ));
  paragraph = document.getParagraphArray(4);
  cursor = paragraph.getCTP().newCursor();
  insertAltChunk(document, myXWPFHtmlDocument, cursor);

  FileOutputStream out = new FileOutputStream("InsertHTMLaltChunkInWordAtCursor.docx");
  document.write(out);
  out.close();
  document.close();

 }

 //a wrapper class for the  htmlDoc /word/htmlDoc#.html in the *.docx ZIP archive
 //provides methods for manipulating the HTML
 //TODO: We should *not* using String methods for manipulating HTML!
 private static class MyXWPFHtmlDocument extends POIXMLDocumentPart {

  private String html;
  private String id;

  private MyXWPFHtmlDocument(PackagePart part, String id) throws Exception {
   super(part);
   this.html = "<!DOCTYPE html><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><style></style><title>HTML import</title></head><body></body>";
   this.id = id;
  }

  private String getId() {
   return id;
  }

  private String getHtml() {
   return html;
  }

  private void setHtml(String html) {
   this.html = html;
  }

  @Override
  protected void commit() throws IOException {
   PackagePart part = getPackagePart();
   OutputStream out = part.getOutputStream();
   Writer writer = new OutputStreamWriter(out, "UTF-8");
   writer.write(html);
   writer.close();
   out.close();
  }

 }

 //the XWPFRelation for /word/htmlDoc#.html
 private final static class XWPFHtmlRelation extends POIXMLRelation {
  private XWPFHtmlRelation() {
   super(
    "text/html", 
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk", 
    "/word/htmlDoc#.html");
  }
 }
}

另请参见How to replace text(tag) with HTML in docx using Apache POI?

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69714314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档