C # operate the word code,
# Region read word /// <summary> // read all text in word (excluding the table) /// </summary> /// <returns> the character content in the word (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 Test Project/ReadFromWordDoc/test.doc"; object readOnly = true; object isVisible = false; try {app = new Word. applicati OnClass (); doc = app. documents. open (ref FileName, ref missing, ref readOnly, ref missing, ref isVisible, ref missing, ref missing); string textString = ""; // read all the content and 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. (In word,/a indicates an empty string, but in C #, it indicates a halo ~~) Otherwise, textString = textString is reported when the console program is displayed. replace ("/r", "/n"); // Replace the carriage return and 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 # Add and write word to region /// <summary> /// append and write word /// </summary> /// <param name = "InsertText"> string </param> public void WriteToWord (string InsertText) {Word. applicationClass app = null; Word. document doc = null; object missing = System. reflection. missin G. value; object FileName = m_FilePath; // @ "E:/Learning Test 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 isVisible, ref missing, ref m Issing); // activate the Word document doc. activate (); // append to the last section (the section is marked by/n) doc. paragraphs. last. range. text = InsertText + "/n"; // Add an terminator (add a segment); otherwise, it will be replaced when it is inserted again. // Save the 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
Recommended: http://www.cnblogs.com/roucheng/p/3521864.html