java获取word上方的标题样式,如何使用docx4j阅读word文档并使用所有样式获取部分文档...

I am using docx4j to deal with word document formatting. I have one word document which is divided in number of tables. I want to read all the tables and if I find some keywords then I want to take those contents to another word document with all the formatting. My word document is as follow.

G7mbh.jpg

Like from above I want to take content which is below Some Title. Here my keyword is Sample Text. So whenever Sample Text gets repeated, content needs to be fetched to new word document.

I am using following code.

MainDocumentPart mainDocumentPart = null;

WordprocessingMLPackage docxFile = WordprocessingMLPackage.load(new File(fileName));

mainDocumentPart = docxFile.getMainDocumentPart();

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

ClassFinder finder = new ClassFinder(Tbl.class);

new TraversalUtil(mainDocumentPart.getContent(), finder);

Tbl tbl = null;

int noTbls = 0;

int noRows = 0;

int noCells = 0;

int noParas = 0;

int noTexts = 0;

for (Object table : finder.results) {

noTbls++;

tbl = (Tbl) table;

// Get all the Rows in the table

List allRows = DocxUtility.getDocxUtility()

.getAllElementFromObject(tbl, Tr.class);

for (Object row : allRows) {

Tr tr = (Tr) row;

noRows++;

// Get all the Cells in the Row

List allCells = DocxUtility.getDocxUtility()

.getAllElementFromObject(tr, Tc.class);

toCell:

for (Object cell : allCells) {

Tc tc = (Tc) cell;

noCells++;

// Get all the Paragraph's in the Cell

List allParas = DocxUtility.getDocxUtility()

.getAllElementFromObject(tc, P.class);

for (Object para : allParas) {

P p = (P) para;

noParas++;

// Get all the Run's in the Paragraph

List allRuns = DocxUtility.getDocxUtility()

.getAllElementFromObject(p, R.class);

for (Object run : allRuns) {

R r = (R) run;

// Get the Text in the Run

List allText = DocxUtility.getDocxUtility()

.getAllElementFromObject(r, Text.class);

for (Object text : allText) {

noTexts++;

Text 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);

}

System.out.println("Total no of Tables: " + noTbls );

解决方案

Assuming your text is in a single run (ie not split across runs), then you can search for it via XPath. Or you can manually traverse using TraversalUtil. See docx4j's Getting Started for more info.

So finding your stuff is pretty easy. Copying the formatting it uses, and any rels in it, is in the general case, complicated. See my post http://www.docx4java.org/blog/2010/11/merging-word-documents/ for more on the issues involved.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值