//retText是路径
1 public string ExtractHtml(string rtfText)
2 {
3 try
4 {
5 //Create word object
6 Word.Application applicationObject = new Word.Application();
7 Type wordType = applicationObject.GetType();
8
9 //define path for save your temporary file.
10 string userTemp = @"F:CodeDisplayPrintfileDisplayPrintfilefile";
11 //Open and save your rtf as HTML in your temp path.
12 object missing = Type.Missing;
13 object fileName = rtfText;
14 object False = false;
15 object rtfFileFormt = Word.WdSaveFormat.wdFormatRTF;
16 applicationObject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
17
18 Word.Document documentObject =applicationObject.Documents.Open(ref fileName, ref missing, ref missing, ref missing,
19 ref missing, ref missing, ref missing, ref missing, ref missing,
20 ref missing, ref missing, ref False, ref missing, ref missing,
21 ref missing, ref missing);
22
23 object tempFileName =@"F:CodeDisplayPrintfileDisplayPrintfilefile empHtm.html";
24 object fileFormt = Word.WdSaveFormat.wdFormatHTML;
25 object makeFalse = false;
26 object makeTrue = true;
27 string absolutePath = tempFileName.ToString();
28 if (File.Exists(absolutePath))
29 {
30 try
31 {
32 File.Delete(absolutePath);
33 }
34 catch { }
35 }
36
37 documentObject.SaveAs(ref tempFileName, ref fileFormt,
38 ref makeFalse, ref missing, ref makeFalse,
39 ref missing, ref missing, ref missing, ref makeFalse, ref missing, ref missing,
40 ref missing, ref missing, ref missing, ref missing, ref missing);
41 GC.Collect();
42 GC.WaitForPendingFinalizers();
43 documentObject.Close(ref makeFalse, ref missing, ref missing);
44 GC.Collect();
45 GC.WaitForPendingFinalizers();
46 //File.Delete(rtfText);
47
48 String htmlCode = "";
49
50 //Extract html source from the temporary html file.
51 if (File.Exists(absolutePath))
52 {
53 WebClient client = new WebClient();
54 htmlCode = client.DownloadString(absolutePath);
55 GC.Collect();
56 GC.WaitForPendingFinalizers();
57 try
58 {
59 File.Delete(absolutePath);
60 }
61 catch { }
62 }
63
64 else
65 {
66 htmlCode = "";
67 }
68
69 return htmlCode;
70 }
71 catch
72 {
73 return "";
74 }
75 }
demo: 下载