gpt4 book ai didi

java - 如何从 TableList POI 中移除/删除表格

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:17 24 4
gpt4 key购买 nike

我正在使用一个模板 docx 文件来填充每个表上的数据,但在某些情况下我不想要相同的表,无论如何可以使用哪个 XWPFTable 可以删除/删除?

最佳答案

你可以试试

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

这是一个示例,其中您提供了一个模板文件(其中包含表格),程序删除了第一个表格并保存了文档。

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() );
}
}

}

注意! XWPFDocument 类的方法removeBodyElement 必须在循环遍历所有文档表格的循环之外调用。否则,您可能会遇到 ConcurrentModificationException,因为删除一个表会影响文档中其他表的索引。

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

关于java - 如何从 TableList POI 中移除/删除表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854259/

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