通过获取段落集合,判断段落中的text等属性是否为空,来进行删除

话不多说,见代码

public static void changeText(XWPFDocument document){
    //获取文字段落集合
    List<XWPFParagraph> paragraphs = document.getParagraphs();
    //所有类型集合(文字段落、表格、图片等)
    List<IBodyElement> listBe = document.getBodyElements();
    List<Integer> runList = new ArrayList<>();
    int n = 0;
    for(int i = 0; i < listBe.size(); i++){
        //BodyElementType.PARAGRAPH : 枚举中的文字段落
        //文字为空时,先添加到list中;
        //注意picture类型也在PARAGRAPH中,需要校验embeddedPictures的长度是否为0
        //为0表示空行,大于0表示有图片,可能还有其他类型,暂时没遇到,各位自行斟酌
        if(StringUtils.isEmpty(paragraphs.get(n).getRuns().get(0).text())
            && paragraphs.get(n).getRuns().get(0).getEmbeddedPictures().size() == 0){
            runList.add(i);
        //非文字段落n-1
        if(listBe.get(i).getElementType() != BodyElementType.PARAGRAPH){
    //遍历list删除
    for(int i = runList.size() - 1; i >= 0; i--){
        document.removeBodyElement(runList.get(i));
                                    本文讲述的是关于POI读取word文档时对空白段落进行删除操作,小编查阅了很多渠道,找到的都是POI对于表格空白行的删除操作,关于word的空白段落的操作简直是少之又少,为了便于广大搬砖者的使用,小编在这里推出了目前小编感觉还算是比较好用的关于空白段落删除操作,如果有出现问题的地方,请指正!......
2.创建docx模板(doc不可以)
第一步:准备好jar(这里面的 ooxml-schemas-1.1.jar 大家可以尝试用poi-shemas-3.12-20150511.jar替换试试,但是偶尔会报java.lang.NoSuchMethodException: org.openxmlformats.schemas.wordprocessingml.x2006.....等错误,网络说poi-shemas-3.12-20150511.jar是简化版,需要用 ooxm
                                    I have a word template that has multiple similar tables and some paragraphs associated to those tables placed just before them. Depending on the amount of data, I populate some tables and others are n...
    int summary = doc.getBodyElements().size();
    doc.removeBodyElement(summary-1);
}catch(Exception e) { e.printStackTr...
doc = new CustomXWPFDocument(is); //is为路径
 // 获取表4并进行删除
List<XWPFTable> tables = doc.getTables(); 
XWPFTable table = tables.get(3); //表4
// 删除表4多余的部分
for(int i = 21;  i != biao4List.size();i--){ //表4biao4List.siz
n:移动n行数startRow到endRow数据域(正数:向下移,负数:向上移)
1、工作薄出现空行,想要删除空行,假设空行的行号:rowIndex,
工作薄最后一行行号:LastRowInd...