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等等格式可以选择。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值