- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含单个表的 .docx 文件。我想删除从第 2 行到末尾的所有文本。但是,方法 myTable.getRow(somecounter).getCell(somecounter2).setText("")
不起作用,因为它仅将“”连接到现有值。我还尝试制作 XWPFRun 并执行从 myTable.getRow(sc).getCell(sc2).getParagraphs().get(0).createRun( 创建的
但它也不起作用。run.setText("")
)
还尝试了 this thread 中的解决方案,这次没运气:(
有什么想法可以轻松地从单元格中删除文本吗?我的想法是从头开始制作一个新表格并填充内容,但这看起来确实很困难。
最佳答案
要满足“删除第 2 行到末尾的所有文本”的要求会有点复杂,因为 Word
表格单元格可以包含除文本之外的许多其他内容。
考虑下表:
因此,如果要求删除从第 2 行到末尾的所有内容,那么您只需将所有单元格替换为新的干净单元格即可。或者至少对于那些只有空段落的内容。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
/*
needs the full ooxml-schemas-1.3.jar as mentioned in https://poi.apache.org/faq.html#faq-N10025
since the CTRowImpl is not fully shipped with poi-ooxml-schemas-3.13-*.jar
*/
public class WordCleanTableRows {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("document.docx");
XWPFDocument doc = new XWPFDocument(fis);
List<XWPFTable> tables = doc.getTables();
XWPFTable table = tables.get(0);
XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]);
for (int r = 0; r < rows.length; r++) {
if (r > 0) {
XWPFTableRow row = rows[r];
CTTc[] cells = row.getCtRow().getTcList().toArray(new CTTc[0]);
for (int c = 0; c < cells.length; c++) {
CTTc cTTc = cells[c];
//clear only the paragraphs in the cell, keep cell styles
cTTc.setPArray(new CTP[] {CTP.Factory.newInstance()});
cells[c] = cTTc;
}
row.getCtRow().setTcArray(cells);
//System.out.println(row.getCtRow());
}
}
doc.write(new FileOutputStream("new document.docx"));
}
}
这需要完整的 ooxml-schemas-1.3.jar,如 https://poi.apache.org/faq.html#faq-N10025 中提到的因为 CTRowImpl 并未完全随 poi-ooxml-schemas-3.13-*.jar 一起提供。
如果没有完整的 ooxml-schemas-1.3.jar,您可以简单地删除除第一行之外的所有行并添加新行。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.*;
public class WordCleanTableRows2 {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("document.docx");
XWPFDocument doc = new XWPFDocument(fis);
List<XWPFTable> tables = doc.getTables();
XWPFTable table = tables.get(0);
XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]);
for (int r = 0; r < rows.length; r++) {
if (r > 0) {
XWPFTableRow row = rows[r];
table.removeRow(1); //remove second row. others shift upwards
table.createRow(); //add new row at the end
}
}
doc.write(new FileOutputStream("new document.docx"));
}
}
编辑:
以下内容应该在没有 ooxml-schemas-1.3.jar 的情况下工作,并且与我的第一个示例相同。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import java.math.BigInteger;
public class WordCleanTableRows3 {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("document.docx");
XWPFDocument doc = new XWPFDocument(fis);
List<XWPFTable> tables = doc.getTables();
XWPFTable table = tables.get(0);
XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]);
for (int r = 0; r < rows.length; r++) {
if (r > 0) {
XWPFTableRow row = rows[r];
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
//get CTTc and replace the CTPArray with one empty CTP
cell.getCTTc().setPArray(new CTP[] {CTP.Factory.newInstance()});
//set some default styles for the paragraphs in the cells:
//http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/wordprocessingml/x2006/main/CTParaRPr.java
CTP cTP = cell.getCTTc().getPArray(0);
cTP.addNewPPr();
cTP.getPPr().addNewRPr();
cTP.getPPr().getRPr().addNewB().setVal(STOnOff.ON);
cTP.getPPr().getRPr().addNewColor().setVal("FF0000");
cTP.getPPr().getRPr().addNewSz().setVal(BigInteger.valueOf(40));
}
}
}
doc.write(new FileOutputStream("new document.docx"));
}
}
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP 随 poi-ooxml-schemas-3.13-*.jar 一起提供。
关于java - XWPF - 删除单元格文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35233771/
我有一个包含单个表的 .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
我是一名优秀的程序员,十分优秀!