In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve Word in C # operation". In daily operation, I believe many people have doubts about how to realize Word in C # operation. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve Word in C # operation". Next, please follow the editor to study!
C # operation Word practical example: the following is a complete code written by myself, the function: in a specified Word document to find the specified keyword, and print out the paragraph containing the keyword. The Range object used. Now let's look at the specific implementation process:
Using System; using System.Collections; using Word; / / C # manipulates Word utility instance namespace SearchWordDoc {/ / summary > / SearchWordDo < clocks summary / < / summary > public class SearchWordDoc {/ / search word in document. / / strName is the document name which is searched. / / strFind is the key word or phrase. / / return the match paragraphs. Public ArrayList swd (string strFName, string strFind) {ArrayList textsFound = new ArrayList (); / / matched texts object missingValue = Type.Missing; Word.ApplicationClass wdApp = null; / / Word Application object / / C # operate the Word utility instance try {object fName = strFName as object; wdApp = new ApplicationClass () / create a Word application object Word.Document wdDoc = wdApp.Documents.Open (ref fName, ref missingValue, ref missingValue); / / open a Word object / / the Word object has paragraphs or not if (wdDoc.Paragraphs! = null & & wdDoc.Paragraphs.Count > 0) {int count = wdDoc.Paragraphs.Count / / the number of doc paragraphs Word.Range rng; / / Word.Range object Word.Find fnd; / / Word.Find object Console.WriteLine ("There are {0} paragraphs in document'{1}'.", count, strFName); / / C # operate Word practical instance for (int I = 1; I < = count; + + I) / / search key words in every paragraphs {rng = wdDoc.Paragraphs [I] .range; fnd = rng.Find Fnd.ClearFormatting (); fnd.Text = strFind; if (fnd.Execute (ref missingValue, ref missingValue) {/ / if find the matched paragrahps, add it into the textsFound ArrayList. TextsFound.Add ("[" + i.ToString () + "]" + wdDoc.Paragraph [I]. Range.Text.Trim ());} / / Word practical instance} catch (NullReferenceException e) {Console.WriteLine (e.ToString ()); wdApp.Quit (ref missingValue, ref missingValue, ref missingValue);} catch (Exception e) {Console.WriteLine (e.ToString ()) WdApp.Quit (ref missingValue, ref missingValue, ref missingValue);} / / release the Word application object wdApp.Quit (ref missingValue, ref missingValue, ref missingValue); return textsFound;} / / Display usage public void usage () {Console.WriteLine ("\ nUsage: SearchWordDoc doc_name string_found" + "[start_paragraph_NO.]\ n\ t\ t [end _ paragraph_NO.]") } / / Print the result public void printText (ArrayList lst) {if (lst = = null) {Console.WriteLine ("Error: Null ArrayList.\ n"); return;} int len = lst.Count; for (int I = 0; I < len; + + I) {Console.WriteLine ("\ t" + lst [I] as string) } Console.WriteLine ("\ nThere are {0} records.", len);} / Function Main public static void Main (string [] args) {ArrayList textsFound = new ArrayList (); SearchWordDoc sobject = new SearchWordDoc (); / / C # operate Word utility instance switch (args.Length) {case 0: case 1: sobject.usage (); break Case 2: textsFound = sobject.swd (args [0], args [1]); Console.WriteLine ("Search Result:\ n"); sobject.printText (textsFound); break; default: sobject.usage (); break;}} / / End
The operation of C # on Word is very similar to that of Excel and so on.
At this point, the study of "how to implement Word in C # operation" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.