前两天在园子里看到了BlackSoul 写的《asp.net将word转换为html保存 》的文章,觉得很实用,但是搭建项目时候缺不是那么回事儿,中间有很多问题,比如说word组建的引用、web.config权限的设置,看到了很多人在网上也在问这个问题,我把问题一一解决,测试成功,现在把全部项目源码文件分享给大家下载。
项目截图:
![](http://www.51aspx.com/codeImg/218C.jpg)
功能概述及注意事项:
该源码主要是通过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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
// public WordToHTML() { }
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
7
/// 上传文件并转存为html
8
///
9
/// word文档在客户机的位置
10
/// 上传的html文件的地址
11
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
14
Type wordType = word.GetType();
15
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
// 打开文件
18
Type docsType = docs.GetType();
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
//应当先把文件上传至服务器然后再解析文件为html
21
string filePath = uploadWord(wordFilePath);
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
//判断是否上传文件成功
24
if (filePath == "0")
25
return "0";
26
//判断是否为word文件
27
if (filePath == "1")
28
return "1";
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
object fileName = filePath;
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[]
{ fileName, true, true });
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
// 转换格式,另存为html
36
Type docType = doc.GetType();
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
39
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
//被转换的html文档保存的位置
42
string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
43
object saveFileName = ConfigPath;
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
46
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
47
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
48
* 其它格式:
49
* wdFormatHTML
50
* wdFormatDocument
51
* wdFormatDOSText
52
* wdFormatDOSTextLineBreaks
53
* wdFormatEncodedText
54
* wdFormatRTF
55
* wdFormatTemplate
56
* wdFormatText
57
* wdFormatTextLineBreaks
58
* wdFormatUnicodeText
59
*/
60
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
null, doc, new object[]
{ saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
// 退出 Word
64
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
65
//转到新生成的页面
66
return ("/" + filename + ".html");
67
}
68
#endregion
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
if (uploadFiles.PostedFile != null)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
string fileName = uploadFiles.PostedFile.FileName;
75
int extendNameIndex = fileName.LastIndexOf(".");
76
string extendName = fileName.Substring(extendNameIndex);
77
string newName = "";
78
try
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
//验证是否为word格式
81
if (extendName == ".doc")
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
return "1";
92
}
93
}
94
catch
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
else
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
return "0";
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
protected void btnUpload_Click(object sender, EventArgs e)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
try
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
//上传
113
uploadWord(File1);
114
//转换
115
wordToHtml(File1);
116
}
117
catch (Exception ex)
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
119
throw ex;
120
}
121
finally
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
Response.Write("恭喜,转换成功!");
124
}
125
}
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
word文件中前几个笑话:
1、 碗掉下来,天大个疤
2、一次文艺晚会,主持人上台报幕:下面请欣赏:新疆歌舞,掀起你的头盖骨!
毛骨悚然!!!!!
3、老虎不发猫,你当我是病危呀!
4、上高中时,课堂纪律混乱,老师一怒之下揪起XXX,说:XXX,你给我站墙上去!
~~全班暴寒!
5、一次我开车,坐我旁边的女同事突然问:“你怎么开车不系安全套的?”
6、我:那是我们物理老师。。。
还有30多个都在word文档中
完整项目源码下载