如何迅速将Word转换为Html
控件中国网提供众多的各种转换控件。
//将Word转换为Html
public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
//应当先把文件上传至服务器然后再解析文件为html
//if (wordFilePath.PostedFile != null)
//{
//变换前路径 改动1
//string filePath = ViewState["sss"].ToString();
//}
string filePath = File1.PostedFile.FileName;
object fileName = filePath;
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.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 = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Uploadfiles \\wordTmp\\" + filename + ".html";
object saveFileName = ConfigPath;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[]{ saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
//转到新生成的页面
return ("Uploadfiles )