62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将word文档转为HTML
/// </summary>
/// <param name="wordFileName">word路径</param>
/// <returns>返回HTML路径</returns>
public string WordToHtml(object wordFileName)
{
object Unknown = Type.Missing;
//在此处放置用户代码以初始化页面
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式,另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf(".")) + ".html";
strSaveFileName = strSaveFileName.Replace("upload", "html");
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出 Word
//wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);