如何使用Java删除包含特定字符串的Word表格行

huangapple go评论66阅读模式
英文:

How to remove rows of a word table that contains a string using java

问题

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"));
}
英文:

this code remove all the rows of the table, but i want to remove a specific rows(if rows contain for example number 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(&quot;C:\\TMPL.docx&quot;);


    XWPFDocument doc = new XWPFDocument(fis);

    List&lt;XWPFTable&gt; tables = doc.getTables();
    XWPFTable table = tables.get(0);

    XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]);

    for (int r = 0; r &lt; rows.length; r++) {
        if (r &gt; 0) {
            XWPFTableRow row = rows[r];
            CTTc[] cells = row.getCtRow().getTcList().toArray(new CTTc[0]);
            for (int c = 0; c &lt; 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(&quot;new document.docx&quot;));
}

答案1

得分: 2

一旦您找到包含数字2的行,您可以使用方法table.removeRow(i)(文档链接见此处)来删除位置为i的整行。

英文:

Once you find a row containing the number 2, you can use the method table.removeRow(i) (documented here) to remove the complete row at position i.

huangapple
  • 本文由 发表于 2020年9月8日 17:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63791069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定