功能概述及注意事项:
该源码主要是通过Asp.net2.0实现Word文档上传并自动转换为Html文件,原理是将word文档上传至服务器然后再转存为html格式文件,再解析html文件修改其页面样式和css。
wordTmp为上传是word暂存文件夹
html为转换后html保存文件夹
注意:请设置web.config中的<identity impersonate="true" userName="administrator" password="51aspx"/>帐号和密码,否则会提示检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
该用户类型为拥有user权限的用户即可
示例Word文件是些笑话,希望能博得大家多多笑容!
部分CS源码:
1
public
partial
class
_Default : System.Web.UI.Page
2
{
3
// public WordToHTML() { }
4
5
上传文件并转换为html wordToHtml(wordFilePath)
69
70
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
71
{
72
if (uploadFiles.PostedFile != null)
73
{
74
string fileName = uploadFiles.PostedFile.FileName;
75
int extendNameIndex = fileName.LastIndexOf(".");
76
string extendName = fileName.Substring(extendNameIndex);
77
string newName = "";
78
try
79
{
80
//验证是否为word格式
81
if (extendName == ".doc")
82
{
83
84
DateTime now = DateTime.Now;
85
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
86
//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
87
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
88
}
89
else
90
{
91
return "1";
92
}
93
}
94
catch
95
{
96
return "0";
97
}
98
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
99
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
100
}
101
102
else
103
{
104
return "0";
105
}
106
}
107
108
protected void btnUpload_Click(object sender, EventArgs e)
109
{
110
try
111
{
112
//上传
113
uploadWord(File1);
114
//转换
115
wordToHtml(File1);
116
}
117
catch (Exception ex)
118
{
119
throw ex;
120
}
121
finally
122
{
123
Response.Write("恭喜,转换成功!");
124
}
125
}
126
127
public
partial
class
_Default : System.Web.UI.Page2
{3
// public WordToHTML() { }4

5
上传文件并转换为html wordToHtml(wordFilePath)69

70
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)71
{72
if (uploadFiles.PostedFile != null)73
{74
string fileName = uploadFiles.PostedFile.FileName;75
int extendNameIndex = fileName.LastIndexOf(".");76
string extendName = fileName.Substring(extendNameIndex);77
string newName = "";78
try79
{80
//验证是否为word格式81
if (extendName == ".doc")82
{83

84
DateTime now = DateTime.Now;85
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();86
//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径87
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));88
}89
else90
{91
return "1";92
}93
}94
catch95
{96
return "0";97
}98
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;99
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);100
}101

102
else103
{104
return "0";105
}106
}107

108
protected void btnUpload_Click(object sender, EventArgs e)109
{110
try111
{112
//上传113
uploadWord(File1);114
//转换115
wordToHtml(File1);116
}117
catch (Exception ex)118
{119
throw ex;120
}121
finally122
{123
Response.Write("恭喜,转换成功!");124
}125
}126

127
本文转自 liudao 博客园博客,原文链接:http://www.cnblogs.com/liudao/archive/2007/05/15/746771.html,如需转载请自行联系原作者