C # manipulate ole--in Word to insert, edit, read OLE

Source: Internet
Author: User
Tags ole

Overview

Ole,object linking and embedding, that is, object connection and embedding. When we design a program, OLE can be used to create compound documents that combine types of text, sounds, images, tables, applications, and so on, and in Word we can use OLE to make a combination of the above feature information. The following example shows how to manipulate OLE in Word through C #, which contains the following points:

    • Insert OLE to Word
    • Edit OLE in Word
    • To read OLE in Word
Using tools
    • Spire.doc for. NET
      Note: Download and install the class library, while editing the code, be aware of adding reference Spire.Doc.dll (DLL files can be obtained in the Bin folder under the installation path) in the program
Sample code (for reference) "Example 1" Insert OLE to Word

C#

  using spire.doc;using spire.doc.documents;using spire.doc.fields;using system.drawing;namespace            Insertole_doc{class Program {static void Main (string[] args) {//instantiation of a document class object            Document doc = new document (); Add a Section object to the document, and add the paragraph sections sec = Doc.            AddSection (); Paragraph p = sec.            Addparagraph ();            Instantiate a Docpicture class object, load Picture docpicture = new Docpicture (DOC);            Image image = Image.FromFile (@ "Chart1.png"); Picture.            LoadImage (image); Inserts a worksheet in the document, Olelinktype enumeration value controls whether the OLE is a link or embedded docoleobject obj = P.appendoleobject (@ "testfile.xlsx", pic            Ture, Olelinktype.link);            Docoleobject obj = p.appendoleobject (@ "testfile.xlsx", picture, olelinktype.embed); Save and open document Doc.            SaveToFile ("Add Ole.docx");        System.Diagnostics.Process.Start ("Add Ole.docx"); }    }}

After the code is finished, debug the Run program and generate the document.
Test results as shown in:

"Example 2" editing OLE in Word

(The document generated in the above article is a test file)
C#

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) {//Instantiate a Document object, loading the wo containing OLE            Rd Documentation Document DOC = new document (); Doc.            LoadFromFile ("Test.docx"); Gets the first section of the SEC = doc.            Sections[0]; Traverse all child elements in this section to find the OLE object under paragraph foreach (DocumentObject obj in sec. Body.childobjects) {if (obj is Paragraph) {Paragraph par = O                    BJ as Paragraph; foreach (DocumentObject paraobj in par. childobjects) {//Find OLE object, change action according to type if (paraobj.documen Tobjecttype = = documentobjecttype.oleobject) {Docoleobject Ole = Paraob                            J as Docoleobject;              If it is a link, modify the link path of the object              if (Ole.linktype = = Olelinktype.link) {//At the same time also to manually go to more                                Change the image of Ole docpicture pic = ole.olepicture; Pic.                                LoadImage (Image.FromFile ("img.png"));                            Ole.linkpath = @ "Sample.docx";                            }//If it is embedded, change data can 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);     }                        }                    }                }            }       Save the modified document and open doc.            SaveToFile ("Modified Ole.docx", Spire.Doc.FileFormat.Docx2010);        System.Diagnostics.Process.Start ("Modified Ole.docx"); }    }}

After you debug the run program, generate the document. When you open a document, the resulting document has both the inserted OLE picture and the linked document changed, as follows:

"Example 3" reads OLE in Word

C#

Using spire.doc;using spire.doc.documents;using spire.doc.fields;using system.io;namespace ReadOLE_Doc{class Program {static void Main (string[] args) {//Instantiate a Document object, loading a file with OLE object doc =                      New Document (); Doc.            LoadFromFile (@ "Test.docx"); Traverse the document all sections of foreach (section sec in Doc. Sections) {//Traverse all the sub-elements under section foreach (DocumentObject-obj in sec). Body.childobjects) {if (obj is Paragraph) {P                        Aragraph par = obj as Paragraph; Traverse this section under the paragraph foreach (DocumentObject o in par. childobjects) {//Find OLE object and extract if by type (o.docum Entobjecttype = = documentobjecttype.oleobject) {docoleobject Ol E = o As docoleobject;                                The/*objecttype property can get the specific type of the OLE object. Note that if you add an OLE object with Spire.doc, you need to declare the oleobjecttype when you appendoleobject, otherwise you will not get a specific type, only the package*                                /String s = Ole.objecttype;                                "acroexch.document.11" means the ProgID if (s = = "acroexch.document.11") corresponding to the PDF object                                {file.writeallbytes ("Result.pdf", ole.nativedata); }//"excel.sheet.12" refers to the ProgID of the worksheet corresponding to the EXCEL03 Els E if (s = = "excel.sheet.12") {file.writeallbytes ("Resul                                T.xlsx ", ole.nativedata); }//"word.document.12" refers to the ProgID of the Word after 03, or else if (s = = "Wo Rd. Document.12 ") {file.writeallbytes (" Result.docx ", ole.nativedata);                }                            }                        }                    } }            }        }    }}

Debug run the program, generate the document, as follows:

The above is all about C # operations for OLE in Word. An example method is provided for reference.

The end of this article.
If you want to reprint, please specify the source!

C # manipulate ole--in Word to insert, edit, read OLE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.