asp.net将word转换为html保存
来源:互联网 发布:2015中国外汇储备数据 编辑:程序博客网 时间:2024/05/22 16:42
很久以前写的了..发在blogger上的.结果blogger被封..现在解封了.转回来.
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Web;
5using Word;
6
7namespace 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}
2using System.Collections;
3using System.ComponentModel;
4using System.Web;
5using Word;
6
7namespace 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
- asp.net将word转换为html保存
- asp.net将word转换为html保存
- asp.net将word转换为html保存
- asp.net将word转换为html保存
- 如何使用,ASP.Net,将Word,转换为,HTML文件,教程实例
- 如何将ASP.NET页面保存为HTML文件
- Asp.Net把word文件转换为html文件
- 将html流保存为word文件
- 将html流保存为word文件
- php将html保存为word文档
- C# 将Word文档转换为HTML
- C# 将Word文档转换为HTML
- 使用poi将word转换为html
- asp.net 将table保存为Excle
- 使用Jacob调用word将word转换为html
- WORD:将word文档转换为HTML网页
- 将 ASP 转换为 ASP.NET
- ASP.NET将Word文档转化为HTML格式的文档
- DevExpress.NET.v3 for 2003汉化包
- 给页面减减肥!
- ATLAS拖拉之简单小结
- CSS解决未知高度垂直居中
- 一个表单动态输入提示
- asp.net将word转换为html保存
- 问题好多阿!!![by beabsolutezero]
- C#中如何插入照片到Excel
- C#利用正则表达式实现字符串搜索
- 10.1回家拍的片子。
- .NET编程规范
- 摆摆控件,就可以实现多功能(提交、修改、浏览)的表单了
- 老外写的正则表达式的类
- 发布全部开源的Asp.net 2.0 RBAC 权限管理系统了,大家支持下。测试版发布: http://Wenzy.37live.com