javaworddoc 转xml 解析word ⾥⾯的树
最近做的⼀个项⽬遇到⼀个需要将word ⾥⾯画的⼀个树导⼊到数据库,于是就想⽤doc 转成xml ,然后再解析到数据库⾥⾯。word
⾥⾯的树是这样的:
转成xml 后有了⼀下关系结构:
格式转换了⽹上好多⽅法都不好⽤,最后看到⼀个⽤word 录制宏,然后⽤jacob 调⽤宏的⽅法来实现批量转换。宏代码:
<o:relationtable v:ext="edit">
<o:rel v:ext="edit" idsrc="#_s1028" iddest="#_s1028"/>
<o:rel v:ext="edit" idsrc="#_s1029" iddest="#_s1028" idcntr="#_s1032"/>
<o:rel v:ext="edit" idsrc="#_s1030" iddest="#_s1028" idcntr="#_s1033"/>
<o:rel v:ext="edit" idsrc="#_s1117" iddest="#_s1028" idcntr="#_s1118"/>
<o:rel v:ext="edit" idsrc="#_s1161" iddest="#_s1028" idcntr="#_s1162"/></o:relationtable>
Sub hong1()
'
' hong1 宏
'
'
Dim name As String
name = "01"
For i = 1 To 4
ChangeFileOpenDirectory "D:\doc\"
Documents.Open filename:=name & ".doc", ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:=""
ChangeFileOpenDirectory "D:\doc2xml\"
java xml是什么ActiveDocument.SaveAs2 filename:=name & ".xml", FileFormat:=wdFormatFlatXML, _
LockComments:=False, password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, CompatibilityMode:=11
ActiveWindow.Close
name = name + 1
If name < 10 Then name = "0" & name
Next i
End Sub
调⽤宏的java代码:
static void runMacros(String path) {
ActiveXComponent word = new ActiveXComponent("Word.Application");
Dispatch documents = Property("Documents").toDispatch();
//String filename = "01.doc";
File file = new File(path);
File[] files = file.listFiles();
for (File tf : files) {
Dispatch document = Dispatch.call(documents, "Open", tf.getAbsolutePath()).toDispatch();
Dispatch.call(word, "Run", new Variant("macro1"), new Variant(path), new Name()),                    new Variant(path), new Name().substring(Name().lastIndexOf("."))));
}
// Dispatch.call(documents, "Close");
}
实现转换之后再⽤dom4j来解析xml树。基本搞定了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。