很久以前写的了..发在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///
14public class WordToHTML
15{
16public WordToHTML(){}
17
18上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
19/**////
20/// 上传文件并转存为html
21///
22/// word文档在客户机的位置
23/// 上传的html文件的地址
24public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
25{
26Word.ApplicationClass word = new Word.ApplicationClass();
27Type wordType = word.GetType();
28Word.Documents docs = word.Documents;
29
30// 打开文件
31Type docsType = docs.GetType();
32
33//应当先把文件上传至服务器然后再解析文件为html
34string filePath = uploadWord(wordFilePath);
35
36//判断是否上传文件成功
37if(filePath == "0")
38return "0";
39//判断是否为word文件
40if(filePath == "1")
41return "1";
42
43object fileName = filePath;
44
45Word.Document doc = (Word.Document)docsType.InvokeMember("Open",
46System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {fileName, true, true});
47
48// 转换格式,另存为html
49Type docType = doc.GetType();
50
51string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
52System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
53
54//被转换的html文档保存的位置
55string ConfigPath = HttpContext.Current.Server.MapPath("upload/"+filename+".html");
56object 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*/
73docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
74null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
75
76// 退出 Word
77wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);
78//转到新生成的页面
79return("upload/"+filename+".html");
80}
81#endregion
82
83public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
84{
85if(uploadFiles.PostedFile != null)
86{
87string fileName = uploadFiles.PostedFile.FileName ;
88int extendNameIndex= fileName.LastIndexOf(".");
89string extendName = fileName.Substring(extendNameIndex);
90string newName = "";
91try
92{
93//验证是否为word格式
94if(extendName == ".doc")
95{
96
97DateTime now = DateTime.Now;
98newName = now.DayOfYear.ToString()+uploadFiles.PostedFile.ContentLength.ToString();
99//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
100uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
101}
102else
103{
104return "1";
105}
106}
107catch
108{
109return "0";
110}
111//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
112return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
113}
114
115else
116{
117return "0";
118}
119}
120
121}
122}
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///
14public class WordToHTML
15{
16public WordToHTML(){}
17
18上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
19/**////
20/// 上传文件并转存为html
21///
22/// word文档在客户机的位置
23/// 上传的html文件的地址
24public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
25{
26Word.ApplicationClass word = new Word.ApplicationClass();
27Type wordType = word.GetType();
28Word.Documents docs = word.Documents;
29
30// 打开文件
31Type docsType = docs.GetType();
32
33//应当先把文件上传至服务器然后再解析文件为html
34string filePath = uploadWord(wordFilePath);
35
36//判断是否上传文件成功
37if(filePath == "0")
38return "0";
39//判断是否为word文件
40if(filePath == "1")
41return "1";
42
43object fileName = filePath;
44
45Word.Document doc = (Word.Document)docsType.InvokeMember("Open",
46System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {fileName, true, true});
47
48// 转换格式,另存为html
49Type docType = doc.GetType();
50
51string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
52System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
53
54//被转换的html文档保存的位置
55string ConfigPath = HttpContext.Current.Server.MapPath("upload/"+filename+".html");
56object 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*/
73docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
74null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
75
76// 退出 Word
77wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);
78//转到新生成的页面
79return("upload/"+filename+".html");
80}
81#endregion
82
83public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
84{
85if(uploadFiles.PostedFile != null)
86{
87string fileName = uploadFiles.PostedFile.FileName ;
88int extendNameIndex= fileName.LastIndexOf(".");
89string extendName = fileName.Substring(extendNameIndex);
90string newName = "";
91try
92{
93//验证是否为word格式
94if(extendName == ".doc")
95{
96
97DateTime now = DateTime.Now;
98newName = now.DayOfYear.ToString()+uploadFiles.PostedFile.ContentLength.ToString();
99//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
100uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
101}
102else
103{
104return "1";
105}
106}
107catch
108{
109return "0";
110}
111//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
112return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
113}
114
115else
116{
117return "0";
118}
119}
120
121}
122}
http://www.cnblogs.com/BlackSoul/archive/2006/09/30/wordtohtml.html