Example of how to manipulate word in C #

Source: Internet
Author: User
This article mainly introduces the method of C # operation Word, combined with the example form Analysis C # for Word document read, write, save and other related operation skills, need friends can refer to the next

The example in this article describes how C # operates with Word. Share to everyone for your reference, as follows:


#region Read word///<summary>///read all text content in Word (no table)///</summary>///<returns>word character content (plain text) </  Returns>public string Readallfromword () {Word.applicationclass app = null;  Word.Document doc = null;  Object missing = System.Reflection.Missing.Value;  Object FileName = m_filepath;//@ "e:/Learning pilot Project/readfromworddoc/test.doc";  Object readOnly = true;  Object isVisible = false;    try {app = new Word.applicationclass (); doc = App. Documents.Open (ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref Missi    Ng, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);    String textstring = ""; Read all the contents ask hovertree.com TextString = doc. Content.Text.Trim ();//int parcount = This.getparcount (doc);//number of segments//for (int i = 1; I <= parcount; i++)/ /{//TextString = TextString + Doc. Paragraphs[i]. Range.Text.Trim ();//doc. content.text.tRim ();////} textstring = Textstring.replace ("/A", ""); The replacement empty string is empty.  (The/A represents an empty string in Word, but in C #, it represents the Bell Halo ~ ~) Otherwise the console program will sound TextString = Textstring.replace ("/R", "/n");  Replace carriage return to return textstring;  } catch (Exception ex) {throw ex; } finally {if (doc! = null) {try {doc.      Close (ref missing, ref missing, ref missing);    } catch {} doc = null; } if (app! = null) {try {app.      Quit (ref missing, ref missing, ref missing);    } catch {} app = null; } GC.    Collect (); Gc.  WaitForPendingFinalizers (); }} #endregion #region append write to Word//<summary>///append write word///</summary>///<param name= "InsertText" >  The string to be written </param>public void Writetoword (String inserttext) {Word.applicationclass app = null;  Word.Document doc = null;  Object missing = System.Reflection.Missing.Value;  Object FileName = m_filepath;//@ "e:/Learning pilot Project/readfromworddoc/test.doc";  Object readOnly = false; Object IsvisIble = false;    try {app = new Word.applicationclass (); doc = App. Documents.Open (ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref Missi    Ng, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); Activate the Word document Doc.    Activate (); Append to the last paragraph (the paragraphs are marked with/n as the doc).    Paragraphs.Last.Range.Text = InsertText + "/n";//Add a terminator (add a paragraph), or it will be replaced when it is inserted again. Save Doc.  Save ();  } catch (Exception ex) {throw ex; } finally {if (doc! = null) {try {doc.      Close (ref missing, ref missing, ref missing);    } catch {} doc = null; } if (app! = null) {try {app.      Quit (ref missing, ref missing, ref missing);    } catch {} app = null; } GC.    Collect (); Gc.  WaitForPendingFinalizers (); }} #endregion
Related Article

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.