using
System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using Word;
namespace Rules
... {
/**////
/// 将word文档上传至服务器然后再转存为html格式文件
/// 再解析html文件修改其页面样式和css
/// BlackSoul 2005.11.08
///
public class WordToHTML
...{
public WordToHTML() ...{ }
上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
/**////
/// 上传文件并转存为html
///
/// word文档在客户机的位置
/// 上传的html文件的地址
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
...{
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
//应当先把文件上传至服务器然后再解析文件为html
string filePath = uploadWord(wordFilePath);
//判断是否上传文件成功
if (filePath == "0")
return "0";
//判断是否为word文件
if (filePath == "1")
return "1";
object fileName = filePath;
Word.Document doc = (Word.Document) docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] ...{ fileName, true, true });
// 转换格式,另存为html
Type docType = doc.GetType();
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("upload/" + filename + ".html");
object saveFileName = ConfigPath;
/**//*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
* 其它格式:
* wdFormatHTML
* wdFormatDocument
* wdFormatDOSText
* wdFormatDOSTextLineBreaks
* wdFormatEncodedText
* wdFormatRTF
* wdFormatTemplate
* wdFormatText
* wdFormatTextLineBreaks
* wdFormatUnicodeText
*/
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] ...{ saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//转到新生成的页面
return ("upload/" + filename + ".html");
}
#endregion
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
...{
if (uploadFiles.PostedFile != null)
...{
string fileName = uploadFiles.PostedFile.FileName;
int extendNameIndex = fileName.LastIndexOf(".");
string extendName = fileName.Substring(extendNameIndex);
string newName = "";
try
...{
//验证是否为word格式
if (extendName == ".doc")
...{
DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
}
else
...{
return "1";
}
}
catch
...{
return "0";
}
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
}
else
...{
return "0";
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Web;
using Word;
namespace Rules
... {
/**////
/// 将word文档上传至服务器然后再转存为html格式文件
/// 再解析html文件修改其页面样式和css
/// BlackSoul 2005.11.08
///
public class WordToHTML
...{
public WordToHTML() ...{ }
上传文件并转换为html wordToHtml(wordFilePath)#region 上传文件并转换为html wordToHtml(wordFilePath)
/**////
/// 上传文件并转存为html
///
/// word文档在客户机的位置
/// 上传的html文件的地址
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
...{
Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
//应当先把文件上传至服务器然后再解析文件为html
string filePath = uploadWord(wordFilePath);
//判断是否上传文件成功
if (filePath == "0")
return "0";
//判断是否为word文件
if (filePath == "1")
return "1";
object fileName = filePath;
Word.Document doc = (Word.Document) docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] ...{ fileName, true, true });
// 转换格式,另存为html
Type docType = doc.GetType();
string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
//被转换的html文档保存的位置
string ConfigPath = HttpContext.Current.Server.MapPath("upload/" + filename + ".html");
object saveFileName = ConfigPath;
/**//*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
* 其它格式:
* wdFormatHTML
* wdFormatDocument
* wdFormatDOSText
* wdFormatDOSTextLineBreaks
* wdFormatEncodedText
* wdFormatRTF
* wdFormatTemplate
* wdFormatText
* wdFormatTextLineBreaks
* wdFormatUnicodeText
*/
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] ...{ saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//转到新生成的页面
return ("upload/" + filename + ".html");
}
#endregion
public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)
...{
if (uploadFiles.PostedFile != null)
...{
string fileName = uploadFiles.PostedFile.FileName;
int extendNameIndex = fileName.LastIndexOf(".");
string extendName = fileName.Substring(extendNameIndex);
string newName = "";
try
...{
//验证是否为word格式
if (extendName == ".doc")
...{
DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();
//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径
uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
}
else
...{
return "1";
}
}
catch
...{
return "0";
}
//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName;
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
}
else
...{
return "0";
}
}
}
}