如何删除/从TableList POI删除表 [英] How to remove / delete the table from the TableList POI

查看:693
本文介绍了如何删除/从TableList POI删除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的模板文件DOCX,以填补在每个表中的数据。但在某些情况下,我不希望同桌,反正是有使用的XWPFTable可以删除/删除?
谢谢你。

i am using a template docx file to fill the data on each table. but in some case i dont want same table, is there anyway using which XWPFTable can be deleted / removed? Thanks.

推荐答案

您可以尝试

int position = document.getPosOfTable( table );
document.removeBodyElement( position );

下面是一个例子,在其中提供一个模板文件(在它的表),该程序删除第一表,并保存在文档

Here is an example, in which you provide a template file (with tables in it), the program deletes the first table and saves the document.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class TableTest {

    public static void main( String[] args ) {

        String filename = "template.docx";

        try {
            FileInputStream in = new FileInputStream( new File( filename ) );
            XWPFDocument document = new XWPFDocument( in );

            showTablesInfo( document );

            // Deleting the first table of the document
            deleteOneTable( document, 0 );

            showTablesInfo( document );

            saveDoc( document, "template after processing.docx" );

        } catch ( FileNotFoundException e ) {
            System.out.println( "File " + filename + " not found." );
        } catch ( IOException e ) {
            System.out.println( "IOException while processing file " + filename + ":\n" + e.getMessage() );
        }
    }

    private static void showTablesInfo( XWPFDocument document ) {
        List<XWPFTable> tables = document.getTables();
        System.out.println( "\n document has " + tables.size() + " table(s)." );

        for ( XWPFTable table : tables ) {
            System.out.println( "\t table with position #" + document.getPosOfTable( table ) + " has "
                    + table.getRows().size() + " rows" );
        }
    }

    /**
     * Deletes a table, given its relative position in document.
     */
    private static void deleteOneTable( XWPFDocument document, int tableIndex ) {
        try {
            int bodyElement = getBodyElementOfTable( document, tableIndex );
            System.out.println( "deleting table with bodyElement #" + bodyElement );
            document.removeBodyElement( bodyElement );
        } catch ( Exception e ) {
            System.out.println( "There is no table #" + tableIndex + " in the document." );
        }
    }

    private static int getBodyElementOfTable( XWPFDocument document, int tableNumberInDocument ) {
        List<XWPFTable> tables = document.getTables();
        XWPFTable theTable = tables.get( tableNumberInDocument );

        return document.getPosOfTable( theTable );
    }

    private static void saveDoc( XWPFDocument document, String filename ) {
        try {
            FileOutputStream out = new FileOutputStream( new File( filename ) );
            document.write( out );
            out.close();
        } catch ( FileNotFoundException e ) {
            System.out.println( e.getMessage() );
        } catch ( IOException e ) {
            System.out.println( "IOException while saving to " + filename + ":\n" + e.getMessage() );
        }
    }

}

注意!方法 removeBodyElement XWPFDocument 类已经被称为外遍历所有文档的表内,以循环。否则,你的风险有 ConcurrentModificationException的,如删除一个表会影响到文档中的其他表的索引。

Attention! Method removeBodyElement of the XWPFDocument class has to be called outside the loop which cycles through all the document's tables. Otherwise, you risk to have ConcurrentModificationException, as deleting one table will affect the indices of other tables in the document.

在上面的例子中选择一个表中删除的条件很简单:在文档的第一个表。另一种方法来提取 XWPFTable 删除,是写一个小方法,它会越来越,让我们说,第一列的第一行的第一个单元格中的文本(标题),等等。希望这会有所帮助,祝你好运!

In the above example the criteria of selecting a table to delete is simple: the first table of the document. Another way to extract the XWPFTable to delete, is writing a small method which will be getting, let's say, text of the first cell of the first line (title of the first column), etc. Hope this will help, good luck!

这篇关于如何删除/从TableList POI删除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆