当前位置: 首页 > 知识库问答 >
问题:

是否可以使用ApachePOI在单词dokument中的特定位置插入HTML

楚宏胜
2023-03-14

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

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

提前谢谢!

弗雷德里克

共有1个答案

司徒正信
2023-03-14

要在XWPFDocument的主体中插入内容,需要一个指向插入位置的XmlCursor。在方法XWPFDocument中已经做了同样的工作。插入新段落(org.apache.xmlbeans.XmlCursor cursor)XWPFDocument。insertNewTbl(org.apache.xmlbeans.XmlCursor cursor)

因此,我们需要一个方法插入AltChunk(XWPFDocument, MyXWPFHtmlDocumXWPFHtmlDocumXWPFHtmlDocumXmlCouror cursor)在指向myXWPFHtmlDocumentsIdcursor位置插入altChunk

...
 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();   
  }  
 }
...

完整的示例,基于我在如何使用Apache POI将altChunk元素添加到XWPFDocument中的代码,它在现有的Word文档的第二段和第五段之前插入altChunks。

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");
  }
 }
}

另请参见如何使用ApachePOI?在docx中用HTML替换文本(标记)?。

 类似资料:
  • 假设我有一个列表列表。 现在我想检查某一组单词后是否存在单词“is”。 我写了一个单词lise 有没有办法用if语句检查它? 如果我在句子中写< code > if ' is:它将返回List1中的所有三个句子,我希望它返回前两个句子。 有没有办法检查单词' Is '是否正好定位在word_list中的单词之后?提前感谢。

  • 问题内容: 我正在使用Spring和struts,并且在“ /META-INF/context.xml”中具有以下条目 是否可以通过以下方式实现, 我的applicationContext.xml具有以下内容, 我想从属性文件中选取jdbc.username和jdbc.pwd的值。 问题答案: 使用Spring的PlaceholderPropertyConfigurer(仅替换Spring上下文中

  • 问题内容: 我正在编写一个脚本,要求我在配置文件的特定部分中添加行。例如 之前: 后: 如您所见,添加了新行。我的bash脚本如何插入行?我猜我将需要使用sed。 问题答案: 如果要在特定字符串匹配之后添加一行:

  • 问题内容: 假设我有一个大小为n的对象的ArrayList。现在,我想在特定位置插入另一个对象,比方说在索引位置k(大于0且小于n),并且我希望索引位置k或之后的其他对象向前移动一个索引位置。因此,有什么方法可以直接在Java中执行此操作。实际上,我想在添加新对象时保持列表排序。 问题答案: 要 插入 的特定索引,使用值到ArrayList中: 此方法将移动列表的后续元素。但是您不能保证列表会保持

  • 我想写一个函数,将一个节点添加到一个树中,该树以根、节点的值、我想添加新节点的节点的值和一个数字作为参数,根据该数字我将节点添加为左或右子节点(偶数=右子节点,奇数=左子节点)。我不明白我错在哪里,这与BST的概念相同,只是其他条件。如果有任何帮助,我将不胜感激。 例如: Tnode*insert(Tnode*root,int data,int father,int leftOrRight);插入

  • 问题内容: 说我有XML: 如何在As和C之间插入“ nodeB”?在PHP中,最好通过SimpleXML?喜欢: 问题答案: 以下是在其他一些SimpleXMLElement之后插入新的SimpleXMLElement的函数。由于使用SimpleXML不可能直接做到这一点,因此它在幕后使用了一些DOM类/方法来完成工作。 以及如何使用它的示例(特定于您的问题): 如果您想/需要解释它是 如何 工