很久以前写的了..发在blogger上的.结果blogger被封..现在解封了.转回来.
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Web;
5
using
Word;
6
7
namespace
Rules
8
{
9
/**////
10
/// 将word文档上传至服务器然后再转存为html格式文件
11
/// 再解析html文件修改其页面样式和css
12
/// BlackSoul 2005.11.08
13
///
14
public class WordToHTML
15

{
16
public WordToHTML()
{}
17
18
上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
19
/**////
20
/// 上传文件并转存为html
21
///
22
/// word文档在客户机的位置
23
/// 上传的html文件的地址
24
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
25

{
26
Word.ApplicationClass word = new Word.ApplicationClass();
27
Type wordType = word.GetType();
28
Word.Documents docs = word.Documents;
29
30
// 打开文件
31
Type docsType = docs.GetType();
32
33
//应当先把文件上传至服务器然后再解析文件为html
34
string filePath = uploadWord(wordFilePath);
35
36
//判断是否上传文件成功
37
if(filePath == "0")
38
return "0";
39
//判断是否为word文件
40
if(filePath == "1")
41
return "1";
42
43
object fileName = filePath;
44
45
Word.Document doc = (Word.Document)docsType.InvokeMember("Open",
46
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[]
{fileName, true, true});
47
48
// 转换格式,另存为html
49
Type docType = doc.GetType();
50
51
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
52
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
53
54
//被转换的html文档保存的位置
55
string ConfigPath = HttpContext.Current.Server.MapPath("upload/"+filename+".html");
56
object saveFileName = ConfigPath;
57
58
/**//*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
59
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
60
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
61
* 其它格式:
62
* wdFormatHTML
63
* wdFormatDocument
64
* wdFormatDOSText
65
* wdFormatDOSTextLineBreaks
66
* wdFormatEncodedText
67
* wdFormatRTF
68
* wdFormatTemplate
69
* wdFormatText
70
* wdFormatTextLineBreaks
71
* wdFormatUnicodeText
72
*/
73
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
74
null, doc, new object[]
{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
75
76
// 退出 Word
77
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);
78
//转到新生成的页面
79
return("upload/"+filename+".html");
80
}
81
#endregion
82
83
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
84

{
85
if(uploadFiles.PostedFile != null)
86

{
87
string fileName = uploadFiles.PostedFile.FileName ;
88
int extendNameIndex= fileName.LastIndexOf(".");
89
string extendName = fileName.Substring(extendNameIndex);
90
string newName = "";
91
try
92

{
93
//验证是否为word格式
94
if(extendName == ".doc")
95

{
96
97
DateTime now = DateTime.Now;
98
newName = now.DayOfYear.ToString()+uploadFiles.PostedFile.ContentLength.ToString();
99
//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
100
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
101
}
102
else
103

{
104
return "1";
105
}
106
}
107
catch
108

{
109
return "0";
110
}
111
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
112
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
113
}
114
115
else
116

{
117
return "0";
118
}
119
}
120
121
}
122
}

2

3

4

5

6

7

8



9


10

11

12

13

14

15



16



17

18


19


20

21

22

23

24

25



26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46



47

48

49

50

51

52

53

54

55

56

57

58


59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74



75

76

77

78

79

80

81

82

83

84



85

86



87

88

89

90

91

92



93

94

95



96

97

98

99

100

101

102

103



104

105

106

107

108



109

110

111

112

113

114

115

116



117

118

119

120

121

122

http://www.cnblogs.com/BlackSoul/archive/2006/09/30/wordtohtml.html