继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

C# 操作Word 中的OLE对象——插入、编辑、读取 OLE

Eiceblue
关注TA
已关注
手记 107
粉丝 9
获赞 48

概述

OLE,Object Linking and Embedding,即对象连接与嵌入。我们在设计程序时,OLE可以用来创建复合文档,把文字、声音、图像、表格、应用程序等类型的信息组合在一起,在Word中,我们可以通过OLE来实现以上要素信息的组合。下面的示例中将介绍如何通过C# 来操作Word中的OLE对象,示例内容包含以下要点:

  • 插入OLE到Word

  • 编辑Word中的OLE

  • 读取Word中的OLE

 

使用工具

下载安装该类库,在程序中添加引用控件lib文件夹下的Spire.Doc.dll

https://img3.mukewang.com/5d28278d00019a7f02880342.jpg


示例代码(供参考)

【示例1】插入OLE到Word

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing; 
namespace InsertOLE_Doc
 {    
    class Program 
      {     
          static void Main(string[] args)    
             {            
             //实例化一个Document类对象            
             Document doc = new Document();            
             
             //向文档中添加一个Section对象,并添加段落            
             Section sec = doc.AddSection();            
             Paragraph p = sec.AddParagraph();             
             
             //实例化一个DocPicture类对象,加载图片            
             DocPicture picture = new DocPicture(doc);            
             Image image = Image.FromFile(@"chart1.png");            
             picture.LoadImage(image);             
             
             //在文档中插入一个工作表, OleLinkType 枚举值控制该OLE是链接还是嵌入                    
             DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Link);            
             
             //DocOleObject obj = p.AppendOleObject(@"testfile.xlsx", picture, OleLinkType.Embed);             
             
             //保存并打开文档            
             doc.SaveToFile("添加OLE.docx");            
             System.Diagnostics.Process.Start("添加OLE.docx");     
             }   
        }
   }

完成代码后,调试运行程序,生成文档。

测试结果,如下图所示:

 https://img4.mukewang.com/5d28280700015a8a08850649.jpg


【示例2】编辑Word中的OLE

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO; 
namespace EditOLE_Doc
 {    
    class Program   
       {     
          static void Main(string[] args)     
            {            
            //实例化一个Document对象,加载含有OLE的Word文档            
            Document doc = new Document();            
            doc.LoadFromFile("test.docx");             
            
            //获取第一个Section            
            Section sec = doc.Sections[0];             
            //遍历这个Section中的所有子元素,找到段落下的OLE对象            
            foreach (DocumentObject obj in sec.Body.ChildObjects)          
              {               
                 if (obj is Paragraph)            
                     {                    
                      Paragraph par = obj as Paragraph;                    
                      foreach (DocumentObject paraObj in par.ChildObjects)                  
                        {                       
                         //找到OLE对象,根据类型来进行更改操作                      
                           if (paraObj.DocumentObjectType == DocumentObjectType.OleObject)                     
                              {                          
                                DocOleObject Ole = paraObj as DocOleObject;                           
                                //如果是链接, 修改对象的链接路径                         
                                  if (Ole.LinkType == OleLinkType.Link)                          
                                    {                               
                                     //同时还要手动去更改OLE的图片                                
                                     DocPicture pic = Ole.OlePicture;                                
                                     pic.LoadImage(Image.FromFile("Img.png"));                                
                                     Ole.LinkPath = @"sample.docx";                           
                                     }                            
                                   //如果是嵌入,更改数据即可                           
                                   byte[] bys = File.ReadAllBytes(@"sample.docx");                         
                                     if (Ole.LinkType == OleLinkType.Embed)                         
                                        {                                
                                         DocPicture pic = new DocPicture(doc);                                
                                         pic.LoadImage(Image.FromFile(@"Img.png"));                               
                                         Ole.ObjectType = "Word.Document.12";                                
                                         Ole.SetOlePicture(pic);                                
                                         Ole.SetNativeData(bys);                          
                                         }                        
                                     }                    
                                }               
                           }            
                      }            
                                
                  //保存修改后的文档,并打开            
                  doc.SaveToFile("修改OLE.docx", Spire.Doc.FileFormat.Docx2010);            
                  System.Diagnostics.Process.Start("修改OLE.docx");    
              }   
          }
     }

调试运行程序后,生成文档。打开文档时,生成的文档中原有的插入OLE的图片和链接的文档都已经更改了,如下所示:

https://img.mukewang.com/5d2829c80001c6ea10660627.jpg


【示例3】读取Word中的OLE

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO; 
namespace ReadOLE_Doc
{ 
    class Program 
        {   
             static void Main(string[] args)    
                 {            
                  //实例化一个Document对象,加载一个有OLE对象的文档           
                  Document doc = new Document();                     
                  doc.LoadFromFile(@"test.docx");             
                  
                  //遍历文档所有Section                     
                  foreach (Section sec in doc.Sections)         
                    {                
                     //遍历Section下面所有的子元素               
                      foreach (DocumentObject obj in sec.Body.ChildObjects)              
                        {                   
                          if (obj is Paragraph)                
                             {                       
                               Paragraph par = obj as Paragraph;                        
                               //遍历这个section下面的段落                      
                                  foreach (DocumentObject o in par.ChildObjects)                      
                                    {                            
                                     //找到OLE对象,并根据类型提取                          
                                       if (o.DocumentObjectType == DocumentObjectType.OleObject)                        
                                          {                             
                                             DocOleObject Ole = o as DocOleObject;                               
                                             /*ObjectType属性可以获取ole对象的具体类型。                                
                                             注意,如果是用Spire.Doc添加的ole对象的话,需要在AppendOleObject                            
                                             的时候先声明OleObjectType,不然这里得不到具体的类型,只会得到Package*/                               
                                             
                                             string s = Ole.ObjectType;                                
                                             //"AcroExch.Document.11"是指PDF对象对应的ProgID                            
                                                 if (s == "AcroExch.Document.11")                              
                                                   {                                   
                                                     File.WriteAllBytes("Result.pdf", Ole.NativeData);                              
                                                    }                                
                                                    //"Excel.Sheet.12"是指 Excel03之后的工作表对应的ProgID                                
                                                    else if (s == "Excel.Sheet.12")                              
                                                      {                                  
                                                        File.WriteAllBytes("Result.xlsx", Ole.NativeData);                               
                                                       }                                
                                                    //"Word.Document.12"是指03之后的Word对应的ProgID                             
                                                    else if (s == "Word.Document.12")                              
                                                      {                                  
                                                        File.WriteAllBytes("Result.docx", Ole.NativeData);                             
                                                      }                          
                                                 }                     
                                            }                 
                                      }                
                                }            
                          }        
                    }    
              }
        }

调试运行程序,生成文档,如下:

https://img2.mukewang.com/5d282ca800014b0910970577.jpg


以上是本次关于C# 操作word中的OLE的全部内容。示例方法供参考。

本文完。


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP