java - 如何使用java删除包含字符串的单词表的行
问题描述
此代码删除表的所有行,但我想删除特定行(如果行包含例如数字 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.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("C:\\TMPL.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"));
}
解决方案
找到包含数字 2 的行后,您可以使用该方法table.removeRow(i)
(在此处记录)删除 position 处的完整行i
。
推荐阅读
- c# - 在达到 Windows 窗体的最大大小后更新控件的位置
- c - 输出不是我所期望的,代码没有打印任何错误,我不知道出了什么问题
- python - 如何使用 python pandas 数据框使用元组更新 db2 中的记录
- selenium - 如何处理 IE selenium 上的新标签?
- python - 无法将性别数据更改为二进制值
- .net-core - 我没有运气将多个值传递给重定向的 Razor 页面
- forms - Alfresco Workflow 表单保存并关闭
- ios - 如何使用swift等待来自api响应的值以进行登录视图
- reactjs - useRef.current.contains 不是函数
- rest - 我怎样才能在春天得到特定的变量?