C # operate the word code,

Source: Internet
Author: User

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

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.