将word转换成其它文件
1

/**//// <summary>2
/// 将word文档转换为其它文档3
/// </summary>4
/// <param name="SourceFile">源文件</param>5
/// <param name="DestFile">目标文件</param>6
/// <param name="FormatType">转换的文件格式(webarchive:mht;html:html网页;rtf;xml)</param>7
/// <returns></returns>8
public static string WordToHtml(object SourceFile, object DestFile, string FormatType)9

{10
Word.Application app = new Word.Application(); 11
//bool isAlert = app.DisplayAlerts;12
// app.DisplayAlerts = false;13
string sErr = "ok";14
object oMissing = System.Reflection.Missing.Value;15
16
try17

{18
object objSaveFormat = Word.WdSaveFormat.wdFormatWebArchive; //单一网页格式 19
switch (FormatType.ToLower())20

{21
case "webarchive":22
objSaveFormat = Word.WdSaveFormat.wdFormatWebArchive;23
break;24
case "html":25
objSaveFormat = Word.WdSaveFormat.wdFormatHTML;26
break;27
case "rtf":28
objSaveFormat = Word.WdSaveFormat.wdFormatRTF;29
break;30
case "xml":31
objSaveFormat = Word.WdSaveFormat.wdFormatXML;32
break;33

34
}35
object objTrue = true;36
object objFalse = false;37
object objOpenFormat = Word.WdOpenFormat.wdOpenFormatDocument;38
object objEncoding = null;39
object objDirection = Word.WdDocumentDirection.wdLeftToRight;40
Word.Document doc = app.Documents.Open(ref SourceFile, ref objFalse, ref objFalse41
, ref objFalse, ref oMissing, ref oMissing, ref objTrue42
, ref oMissing, ref oMissing, ref objOpenFormat, ref objEncoding, ref objTrue43
, ref objFalse, ref objDirection, ref objFalse, ref oMissing);44

45
doc.SaveAs(ref DestFile, ref objSaveFormat, ref objFalse, ref oMissing, ref objFalse, ref oMissing, ref objFalse,46
ref oMissing, ref objTrue, ref objFalse, ref objFalse, ref objEncoding, ref objFalse, ref objFalse,47
ref oMissing, ref oMissing);48
object oSavaType = Word.WdSaveOptions.wdSaveChanges;49
doc.Close(ref oSavaType, ref oMissing, ref oMissing);50

51
}52

53
catch (Exception ex)54

{55

56
sErr = "文件转换出错:" + ex.Message;57

58
}59

60
finally61

{62
object oSavaType = Word.WdSaveOptions.wdDoNotSaveChanges;63
app.Quit(ref oSavaType, ref oMissing, ref oMissing);64

65
}66
return sErr;67

68
}
浙公网安备 33010602011771号