该文将详细介绍如何使用Spire.Doc for Java 获取 Word文本框中的表格内容及删除Word文本框中的表格。
获取表格:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.io.*;
public class readTableFromTextBox {
    public static void main(String[] args) throws IOException{
        //加载示例文档
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");
        //获取第一个文本框
        TextBox textbox = doc.getTextBoxes().get(0);
        //获取文本框中第一个表格
        Table table = textbox.getBody().getTables().get(0);
        //保存文本
        String output = "output/readTableFromTextBox.txt";
        File file = new File(output);
        if (!file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        //遍历表格中的段落并提取文本
        for (int i = 0; i < table.getRows().getCount(); i++) {
            TableRow row = table.getRows().get(i);
            for (int j = 0; j < row.getCells().getCount(); j++) {
                TableCell cell = row.getCells().get(j);
                for (int k = 0; k < cell.getParagraphs().getCount(); k++) {
                    Paragraph paragraph = cell.getParagraphs().get(k);
                    bw.write(paragraph.getText() + "\t");
                }
            }
            bw.write("\r\n");
        }
        bw.flush();
        bw.close();
        fw.close();
    }
}提取表格内容效果图:

删除表格:
import com.spire.doc.*;
import com.spire.doc.fields.*;
public class deleteTableFromTextBox {
    public static void main(String[] args) {
        //加载示例文档
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");
        //获取第一个文本框
        TextBox textbox = doc.getTextBoxes().get(0);
        //获取文本框中第一个表格
        Table table = textbox.getBody().getTables().get(0);
        //删除第一个表格
        textbox.getBody().getTables().removeAt(0);
        //保存文档
        String output = "output/deleteTableFromTextBox.docx";
        doc.saveToFile(output, FileFormat.Docx_2013);
    }
}
 
    


 
					



