将word文档在窗体上直接显示

 
首先添加引用:Microsoft Word 12.0 Object Library
 
Microsoft.Office.Interop.Word.ApplicationClass appClass = new Microsoft.Office.Interop.Word.ApplicationClass();

            Type wordType = appClass.GetType();

            Microsoft.Office.Interop.Word.Documents docs = appClass.Documents;

            //打开文件

            Type docsType = docs.GetType();

            object fileName = obje;//word的地址

            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 });

            // 转换格式,另存为

            Type docType = doc.GetType();
            string htmlDir=CDeploySys.strTempGeneral+"\\htmlDir";
            if (!System.IO.Directory.Exists(htmlDir))
            {
                System.IO.Directory.CreateDirectory(htmlDir);
            }

            object saveFileName = htmlDir+@"\"+Path.GetFileNameWithoutExtension(fileName.ToString())+".html";//html的地址

            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,

               doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

            //如果是Microsoft Word 9.0 Object Library的写法,可能写成:

            //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,

            //doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML});


            // 退出 Word

            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, appClass, null);
            webBorwer.Url = new Uri(saveFileName.ToString());

你可能感兴趣的:(WinForm,C#)