【问题标题】:how to Create table in word doc using docx4j in specific bookmark without overwritting the word doc如何在特定书签中使用 docx4j 在 word doc 中创建表格而不覆盖 word doc
【发布时间】:2013-08-17 17:40:31
【问题描述】:

我需要在特定书签的位置创建一个表。即我需要找到书签并插入表格。我如何使用 docx4j 做到这一点 提前致谢


对不起,Jason,我是 Stackoverflow 的新手,所以我无法清楚地写出我的问题,这是我的情况和问题。

我按照您的建议和我的需要对该代码进行了更改,代码在这里

//loop through the bookmarks
for (CTBookmark bm : rt.getStarts()) {

// do we have data for this one?
String bmname =bm.getName(); 
// find the right bookmark (in this case i have only one bookmark so check if it is    not null)
if (bmname!=null) {
String value = "some text for testing run";
//if (value==null) continue;

List<Object> theList = null;
//create bm list 
theList = ((ContentAccessor)(bm.getParent())).getContent();

// I set the range as 1 (I assume this start range is to say where the start the table creating)
int rangeStart = 1;



WordprocessingMLPackage wordPackage =   WordprocessingMLPackage.createPackage();

// create the table

Tbl table = factory.createTbl();

//add boards to the table 
addBorders(table);

for(int rows = 0; rows<1;rows++)
{// create a row
Tr row = factory.createTr();
for(int  colm = 0; colm<1;colm++)
{

// create a cell

Tc cell = factory.createTc();

// add the content to cell

cell.getContent().add(wordPackage.getMainDocumentPart()
.createParagraphOfText("cell"+colm));

// add the cell to row
row.getContent().add(cell);
} 

// add the row to table

table.getContent().add(row);

// now add a run (to test whether run is working or not)
org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(t);    
t.setValue(value);
//add table to list
theList.add(rangeStart, table);
//add run to list
//theList.add(rangeStart, run);
}

我不需要删除书签中的文本,所以我删除了它。 我不知道是什么问题,程序正在编译,但我无法打开 doc 一词,它显示“未知错误”。我测试写一些字符串“值”,它完美地写在那个书签和文档正在打开但不是在表格的情况下。请帮我 提前致谢

【问题讨论】:

  • 你是如何给表格添加边框的?

标签: java docx4j


【解决方案1】:

您可以修改示例代码BookmarksReplaceWithText.java

在你的情况下:

  • 第 89 行:父代不会是 p,而是 body 或 tc。您可以删除测试。
  • 第 128 行:您不想添加运行,而是要插入表格

您可以使用TblFactory 来创建您的表格,或者使用示例文档中的docx4j webapp to generate code

【讨论】:

  • 您好 Jason 感谢您的快速回复。我尝试按原样运行 BookmarksReplaceWithText.java 示例代码。即使包含所有 jar 文件,我的编译器也无法识别“RangeFinder”,即 import org.docx4j.finders.RangeFinder;无法解决。我该如何解决这个问题?
  • 这不是 docx4j 2.8.1 的一部分,因此您需要从 github.com/plutext/docx4j/tree/master/src/main/java/org/docx4j/… 下载它或使用最近的夜间版本,例如 docx4java.org/docx4j/docx4j-nightly-20130813.jar
  • 您好,杰森,感谢您提供 jar 信息。我尝试创建表而不是运行,没有抛出错误,但打开文档时出错,(输出文件无法打开。持续问题)。但与文本它的工作正常。我在 nect 评论中做了一些更改和代码如下
  • 我无法在此处发布代码,所以我将我的问题写为答案。
【解决方案2】:

由于某种原因,用表格替换书签对我来说没有用,所以我依靠用表格替换文本。我为我的用例使用 XHTML 导入器从 HTML 创建了我的表格

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

    String xhtml= <your table HTML>;

    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    int ct = 0;
    List<Integer> tableIndexes = new ArrayList<>();
    List<Object> documentContents = documentPart.getContent();
    for (Object o: documentContents) {

        if (o.toString().contains("PlaceholderForTable1")) {
            tableIndexes.add(ct);
        }
        ct++;
    }

    for (Integer i: tableIndexes) {
        documentPart.getContent().remove(i.intValue());
        documentPart.getContent().addAll(i.intValue(), XHTMLImporter.convert( xhtml, null));
    }

在我的输入单词文档中,我定义了文本“PlaceholderForTable1”,我想在其中插入我的表格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    相关资源
    最近更新 更多