gpt4 book ai didi

java - XWPF - 删除单元格文本

转载 作者:行者123 更新时间:2023-12-01 10:28:26 24 4
gpt4 key购买 nike

我有一个包含单个表的 .docx 文件。我想删除从第 2 行到末尾的所有文本。但是,方法 myTable.getRow(somecounter).getCell(somecounter2).setText("") 不起作用,因为它仅将“”连接到现有值。我还尝试制作 XWPFRun 并执行从 myTable.getRow(sc).getCell(sc2).getParagraphs().get(0).createRun( 创建的 run.setText("") ) 但它也不起作用。

还尝试了 this thread 中的解决方案,这次没运气:(

有什么想法可以轻松地从单元格中删除文本吗?我的想法是从头开始制作一个新表格并填充内容,但这看起来确实很困难。

最佳答案

要满足“删除第 2 行到末尾的所有文本”的要求会有点复杂,因为 Word 表格单元格可以包含除文本之外的许多其他内容。

考虑下表:

enter image description here

因此,如果要求删除从第 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com