之前有试过有itext读取文档内容然后截取,取得我想要的内容但取到的内容是没有格式的,格式的丢失让我想到用jacob的copy、paste方法,但由于我只找到拷贝单一段落或是全文的方法,这个不是我要的效果,我只想取得文档里面的部分内容,请问有人做过类似的或是有什么好的想法吗? //这是copy全文方法 ComThread.InitSTA(); //被复制的文档 ActiveXComponent word2= new ActiveXComponent("Word.Application"); word2.setProperty("Visible", new Variant(false)); Dispatch documents2 = word2.getProperty("Documents").toDispatch(); //复制的文档 ActiveXComponent word= new ActiveXComponent("Word.Application"); word.setProperty("Visible", new Variant(false)); Dispatch documents = word.getProperty("Documents").toDispatch(); doc2 = Dispatch.call(documents2, "Open", saveNewRealpath).toDispatch(); //复制模板的内容 doc= Dispatch.call(documents, "Open",fileRealPath).toDispatch(); Dispatch wordContent = Dispatch.get(doc, "Content").toDispatch(); /*Dispatch paragraphs = Dispatch.get(doc2, "Paragraphs").toDispatch(); Dispatch paragraph = Dispatch.call(paragraphs, "Item",new Variant(40)).toDispatch(); Dispatch range = Dispatch.get(paragraph, "Range").toDispatch(); Dispatch.call(range, "Copy");*/ Dispatch.call(wordContent, "Copy"); Dispatch selection = Dispatch.get(word2, "Selection").toDispatch(); Dispatch textRange = Dispatch.get(selection, "Range").toDispatch(); Dispatch.call(textRange, "Paste"); Dispatch.call(doc2, "Save"); Dispatch.call(doc2, "Close", new Variant(true)); Dispatch.call(word2, "Quit"); doc2 = null; word2 = null; Dispatch.call(doc, "Close", new Variant(true)); Dispatch.call(word , "Quit"); doc = null; word = null; documents2 = null; ComThread.Release(); |
|
来自: 三十的狼 > 《java操作word》