C#嵌入word2007或更高版本的内容

webBrowser可以内嵌03版word或更低版本的word。但到了word07,word10,word13,office已经不再主张office嵌入,而是推荐用VSTO对office二次开发。

但是通过一些手段,可以显示word内容。

方法1、通过把word转化为html,然后将html显示在webBrowser上。

using Microsoft.Office.Interop.Word;
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            Guid g = Guid.NewGuid();
            OpenFileDialog fileDialog = new OpenFileDialog();
            DialogResult digResult = fileDialog.ShowDialog();
            if (digResult==DialogResult.OK)
            {
                string path = fileDialog.FileName;
	            ConvertDocument(g, path);
	            webBrowser1.Url = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + g.ToString() + ".html");
            }
            
        }
        private void ConvertDocument(Guid g, string fileName)
        {
            object m = System.Reflection.Missing.Value;
            object oldFileName = (object)fileName;
            object readOnly = (object)false;
            Microsoft.Office.Interop.Word._Application ac = null;
            try
            {
                // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.    
                ac = new Microsoft.Office.Interop.Word.Application();
                // Now we open the document.          
                Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                    ref m, ref m, ref m, ref m, ref m, ref m);
                // Create a temp file to save the HTML file to.           
                string tempFileName = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + g.ToString() + ".html";
                // Cast these items to object.  The methods we're calling  
                // only take object types in their method parameters.      
                object newFileName = (object)tempFileName;
                // We will be saving this file as HTML format.              
                object fileType = (object)WdSaveFormat.wdFormatHTML;
                // Save the file.                 
                doc.SaveAs(ref newFileName, ref fileType,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                    ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            }
            finally
            {
                // Make sure we close the application class.      
                if (ac != null)
                    ac.Quit(ref readOnly, ref m, ref m);
            }

        }

在Visual Studio + Visual Assist的开发环境下,输入“WdSaveFormat.”后会自动显示可以保存为的格式,有pdf,xps等等格式可以选择。


你可能感兴趣的:(C#,word2007,嵌入)