首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache POI,表定位被忽略(.docx,xwpf)

Apache POI,表定位被忽略(.docx,xwpf)
EN

Stack Overflow用户
提问于 2017-11-03 22:41:00
回答 1查看 718关注 0票数 0

我正在尝试使用tblPr元素及其属性tblpX和tblpY来定位一个表。我的问题是,当我打开.docx文件时,表格位于页面的左上角,忽略了tblpX和tblPY的值。您可以找到有关如何定位herehere的.docx表的详细信息。它应该看起来像这样:

代码语言:javascript
复制
<w:tbl>
  <w:tblPr>
  <w:tblpPr w:vertAnchor="text" w:tblpY="200" />
  </w:tblPr>  
  …
</w:tbl>

Apache POI没有提供"tblpY“和"tblpX”属性,所以我认为添加该属性的惟一方法是手动添加。下面是我这样做的代码:

代码语言:javascript
复制
public static XWPFTable createTable(XWPFDocument doc) {

  //CTTbl ctTable = CTTbl.Factory.newInstance();
  XWPFTable table = doc.createTable();//new XWPFTable(ctTable, doc, 0, 0);

  XmlObject x = (XmlObject) table.getCTTbl().getTblPr();
  XmlCursor c = x.newCursor();  // Create a cursor at the element
  c.toNextToken();              // Move cursor after the tblPr tag
  c.insertElement("tblPr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

  c.toPrevSibling(); //Now go to the tblPr
  XmlObject x2 = c.getObject(); //Get the tblPr object
  c.dispose();
  c = x2.newCursor(); //Now our cursor is inside the second tblPr
  c.toNextToken();
  c.insertAttributeWithValue("tblpX", "http://schemas.openxmlformats.org/wordprocessingml/2006/main", "500");
  c.insertAttributeWithValue("tblpY", "http://schemas.openxmlformats.org/wordprocessingml/2006/main", "500");
  c.dispose();

  XWPFTableRow tr = table.getRow(0);
  XWPFTableCell cell = tr.getCell(0);
  cell.setText("some text");

  return table;  

}

我打开了.docx并验证了以下内容在document.xml中

代码语言:javascript
复制
<w:tbl>
  <w:tblPr>
    <w:tblPr w:tblpX="500" w:tblpY="500"/>
    <w:tblW w:w="0" w:type="auto"/>
    <w:tblBorders>
      <w:top w:val="single"/>
      <w:left w:val="single"/>
      <w:bottom w:val="single"/>
      <w:right w:val="single"/>
      <w:insideH w:val="single"/>
      <w:insideV w:val="single"/>
    </w:tblBorders>
  </w:tblPr>
  <w:tr>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>some text</w:t>
        </w:r>
      </w:p>
    </w:tc>
  </w:tr>
</w:tbl>

那么我到底做错了什么呢?为什么桌子仍然在左上角?为什么microsoft word忽略我的tblpX和tblpY值?

EN

回答 1

Stack Overflow用户

发布于 2017-11-03 23:06:13

tblPr内部的子节点应该是: tblpPr而不是tblPr!

"p“的语法上的唯一区别就是让我大吃一惊。

无论如何,希望有人能从中吸取教训。例如,没有关于如何使用Apache POI来绝对定位表格的StackOverflow解决方案。直到现在,mwahahaha

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

https://stackoverflow.com/questions/47098363

复制
相关文章

相似问题

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