// *我正在尝试将一个表从docx文件的一个文档复制到另一个docx文件。但我的表和最后一行的最后一个单元格与最后一个单元格中的内容一起重复。休息一切正常。我能够复制完整的表格,但只有当它到达最后一个单元格和最后一行时,我才会复制。这将是非常有帮助的,如果有人可以指导我,我正在做错误的地方,我已经尝试了整整2天,但它没有工作。
这是我的完整代码,我正在尝试只在文件中的一个表。 * //
公共类ExtractTables {
public static void main(String args[]) throws JAXBException, IOException, Docx4JException {
try {
WordprocessingMLPackage wordMLPackage;
wordMLPackage = WordprocessingMLPackage.load(new java.io.File("D://Table.docx"));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
ExtractTables wordExt = new ExtractTables();
wordExt.extractTable(factory, documentPart);
} catch (Docx4JException DOE) {
System.out.println("The exception is : ");
DOE.printStackTrace();
}
}
private void extractTable(ObjectFactory objFact,MainDocumentPart documentPart)抛出Docx4JException { 尝试{ WordprocessingMLPackage wml = WordprocessingMLPackage.createPackage();
int noTbls = 0;
int noRows = 0;
int noCells = 0;
int noParas = 0;
int noTexts = 0;
List<Object> allTables = getAllElementFromObject(documentPart, Tbl.class);
for (Object table : allTables) {
noTbls++;
Tbl tbl = (Tbl) table;
Tr tr = null;
Tc tc = null;
P p = null;
R r = null;
Text txt = null;
// Get all the Rows in the table
List<Object> allRows = getAllElementFromObject(tbl, Tr.class);
for (Object row : allRows) {
tr = (Tr) row;
noRows++;
// Get all the Cells in the Row
List<Object> allCells = getAllElementFromObject(tr, Tc.class);
for (Object cell : allCells) {
tc = (Tc) cell;
noCells++;
// Get all the Paragraph's in the Cell
List<Object> allParas = getAllElementFromObject(tc, P.class);
for (Object par : allParas) {
p = (P) par;
noParas++;
// Get all the Run's in the Paragraph
List<Object> allRuns = getAllElementFromObject(p, R.class);
for (Object runs : allRuns) {
r = (R) runs;
// Get the Text in the Run
List<Object> allText = getAllElementFromObject(r, Text.class);
for (Object text : allText) {
noTexts++;
txt = (Text) text;
}
System.out.println("No of Text in Para No: " + noParas + "are: " + noTexts);
}
}
System.out.println("No of Paras in Cell No: " + noCells + "are: " + noParas);
}
System.out.println("No of Cells in Row No: " + noRows + "are: " + noCells);
}
System.out.println("No of Rows in Table No: " + noTbls + "are: " + noRows);
r.getContent().add(txt);
p.getContent().add(r);
tc.getContent().add(p);
tr.getContent().add(tc);
tbl.getContent().add(tr);
wml.getMainDocumentPart().addObject(tbl);
wml.save(new File("D://TestTable.docx"));
}
System.out.println("Total no of Tables: " + noTbls);
}
catch (Docx4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static List getAllElementFromObject(Object obj,Class toSearch){
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
}