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