从单词中提取表格

时间:2015-01-14 13:23:41

标签: java ms-word aspose

我想在Word中提取所有表,这段代码只提取第一个表

word doc tables.size() return 1

中的任意数量的表格

tables = Arrays.asList(tempDoc.getChildNodes(NodeType.TABLE, true)

代码:

import com.aspose.words.Document;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.Table;

public class ExtractTableFromWord {
static List<Node> tables;

// static List<Node> tables = new ArrayList<Node>();
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    Document doc = new Document("D://New folder//21.docx");

    // Create a clone of the document
    Document tempDoc = doc.deepClone();
    for (int j = 0; j < doc.getPageCount(); j++) {

        tables = Arrays.asList(tempDoc.getChildNodes(NodeType.TABLE, true)
                .toArray());
        System.out.println(tables.size() + "-------------");
    }
    // Clear tempDoc
    tempDoc.getSections().clear();
    tempDoc.ensureMinimum();

    for (int i = 0; i < tables.size(); i++) {
        Table docTable = (Table) tables.get(i);
        Table docTableCopy = (Table) docTable.deepClone(true);

        // Insert table into document
        tempDoc.getFirstSection().getBody().insertAfter(docTableCopy,
                tempDoc.getFirstSection().getBody().getFirstParagraph());

        // Insert paragraph between tables
        tempDoc.getFirstSection().getBody().insertAfter(
                new Paragraph(tempDoc),
                tempDoc.getFirstSection().getBody().getFirstParagraph());
    }

    tempDoc.save("D:\\out put\\outTables.html");

}
}

0 个答案:

没有答案