- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 tblPr 元素及其属性 tblpX 和 tblpY 来定位表格。我的问题是,当我打开 .docx 文件时,表格位于页面的左上角,忽略 tblpX 和 tblPY 值。您可以找到有关 .docx 表如何定位的详细信息 here或here 。它应该看起来像:
<w:tbl>
<w:tblPr>
<w:tblpPr w:vertAnchor="text" w:tblpY="200" />
</w:tblPr>
…
</w:tbl>
Apache POI 不提供“tblpY”和“tblpX”属性,因此我认为添加此属性的唯一方法是手动。这是我的代码:
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 中
<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 值?
最佳答案
tblPr 内的子节点应该是:tblpPr 不是 tblPr!
“p”语法上的单一差异让我感到困惑。
无论如何,希望有人能从中吸取教训。例如。 StackOverflow 还没有关于如何使用 Apache POI 绝对定位表格的解决方案...直到现在 mwahahaha
关于java - Apache POI,表定位被忽略(.docx、xwpf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47098363/
我有一个包含单个表的 .docx 文件。我想删除从第 2 行到末尾的所有文本。但是,方法 myTable.getRow(somecounter).getCell(somecounter2).setTe
我正在尝试创建一个包含一列的简单表格。 我创建一个新行,并在每行中创建一个新段落。问题是每一行都以一个空行开头 - 我猜是新段落创建了它。 我之前尝试设置间距、缩进等,但没有成功。 fo
我正在 JavaFX 中编写一个程序,用于转换没有缩进的文章,例如 MLA 样式的文档。我现在的问题是,如何为特定的 xwpfParagraph 启用悬挂缩进?我正在使用 Apache poi 3.1
我正在尝试使用 POI 将 word 文件转换为 txt 文件,但是当我使用 org.apache.poi.xwpf.usermodel.XWPFDocument 时它告诉我说“无法解析 xwpf”
XWPF 段落 POI -我想创建段落,但在本段落的最后一个文本或最后一行中没有自动换行。如何设置......谢谢.... String kalimat="Aaaa bbb ccc ddd eee f
我的目标是使用 Apache POI 在 Java 中处理 .docx 文档。我想从文档中提取所有内容来创建一个新文档,但仅限于我可以从处理后的文档中选择的特定内容。到目前为止,这适用于表格和文本,但
XWPF Word 文档中存在许多读取和编辑/替换书签的示例。但我想创建一个文档并创建新书签。创建文档 - 没问题: private void createWordDoc() throws IOExc
如何更改所选单词的字体颜色?我想为此创建一个函数,但我的水平对于这个 api 来说太基础了... 这是我的伪鳕鱼: public String setTextColor(String word){
对此进行了一些研究,过去似乎存在与此相关的错误,我不确定它们是否在最新版本中得到解决。 我正在阅读一个 docx 模板,进行一些修改并输出它。我正在复制文档的所有元素,除了位图之外,位图应包含文本的位
我正在尝试将一些形状和 Logo 文件添加到我的 word docx 文档的标题中。添加图片对我有用,但我没有找到任何如何添加形状的解决方案。谁能帮助我吗? String imgFile="logo.
我正在尝试将一些形状和 Logo 文件添加到我的 word docx 文档的标题中。添加图片对我有用,但我没有找到任何如何添加形状的解决方案。谁能帮助我吗? String imgFile="logo.
我正在尝试使用 Java/Coldfusion 中的 Apache Poi 构建一个 word 文档。到目前为止,它让我可以做很多我想要的格式,除了段落的行间距。有谁知道我如何将段落设置为单行间距?它
本文整理了Java中org.apache.poi.xwpf.usermodel.XWPFHeaderFooter类的一些代码示例,展示了XWPFHeaderFooter类的具体用法。这些代码示例主要来
本文整理了Java中org.apache.poi.xwpf.usermodel.XWPFStyle类的一些代码示例,展示了XWPFStyle类的具体用法。这些代码示例主要来源于Github/Stack
本文整理了Java中org.apache.poi.xwpf.usermodel.XWPFRelation类的一些代码示例,展示了XWPFRelation类的具体用法。这些代码示例主要来源于Github
本文整理了Java中org.apache.poi.xwpf.usermodel.XWPFPicture类的一些代码示例,展示了XWPFPicture类的具体用法。这些代码示例主要来源于Github/S
我尝试使用 tblPr 元素及其属性 tblpX 和 tblpY 来定位表格。我的问题是,当我打开 .docx 文件时,表格位于页面的左上角,忽略 tblpX 和 tblPY 值。您可以找到有关 .d
我想创建一个普通表格,并将其方向设置为“从右到左”“可以使用此选项设置Table Direction”,并使其对齐为“从左到右”,可以使用此选项设置Table Alignment 我已经尝试过这个:
我知道,HWPF 有这个功能,但我在 XWPF 中找不到任何类似的功能。 也许有解决此问题的方法。如果您知道一些,请与我分享。 提前致谢! 最佳答案 在 Word Office OpenXML 中,自
我正在尝试从使用 Apache poi XWPF 生成的 .docx 文档中删除一段。我可以使用 HWPF 轻松地使用 .doc word 文档来完成此操作,如下所示: for (String
我是一名优秀的程序员,十分优秀!