首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XWPF -删除单元格文本

XWPF -删除单元格文本
EN

Stack Overflow用户
提问于 2016-02-05 21:23:19
回答 1查看 3.3K关注 0票数 0

我有一个包含单个表的.docx文件。我想从第2行到末尾删除所有文本。但是,方法myTable.getRow(somecounter).getCell(somecounter2).setText("")不工作,因为它只将“”连接到现有值。我还尝试过制作一个XWPFRun,并做了run.setText(""),但是它不能很好地工作。

也尝试了this thread的解决方案,这次没有成功:(

有什么想法可以轻松地从单元格中删除文本吗?我的想法是从头开始做一张新的表格,然后用内容填满它,但它看起来真的很辛苦。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-06 08:31:21

您的要求“将所有文本从第2行移到末尾”的要求将有点复杂,因为Word表单元格可以包含许多其他内容,而不仅仅是文本。

考虑下表:

因此,如果要求将所有内容从第2行移除到末尾,那么只需将所有单元格替换为新的干净单元格即可。或者至少是那些只有一个空段落的人。

代码语言:javascript
复制
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"));

 }
}

正如https://poi.apache.org/faq.html#faq-N10025中提到的那样,这需要完整的ooxml 1.3.jar,因为poi-ooxml 3.13-*..jar并没有完全提供CTRowImpl。

如果没有完整的Ooxml-Schems-1.3.jar,您只需删除除第一个行之外的所有行,并添加新行。

代码语言:javascript
复制
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-Schems-1.3.jar的情况下,下面的操作应该与我的第一个示例相同。

代码语言:javascript
复制
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-3.13-*..jar一起提供。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35233771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档