你可以试试
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 );</