. NET Word operation implementation: using Word

Source: Internet
Author: User

. NET Operation Word can be implemented using Word. Basically, vs.net will automatically convert the library file into a DLL component, so that we only need to create this component object in the source code to operate on the Word.

To achieve this, we need the Word object library file "MSWORD. OLB (word 2000 is MSWORD9.OLB). After Office Word is installed, you can find this file under the office 10 folder in the Office installation directory, after we introduce this file to the project, we can use various operation functions in the source code to operate the Word. In the left-side Navigation Pane, choose Project> Add reference> browse. In the select component dialog box that appears, find MSWORD. OLB, and click OK to introduce the object library file.

The Application of namespace in the CS code file, such as using Word and. NET, is as follows:

 
 
  1. UsingSystem;
  2. UsingSystem. Drawing;
  3. UsingSystem. Collections;
  4. UsingSystem. ComponentModel;
  5. UsingSystem. Windows. Forms;
  6. UsingWord;
  7.  
  8. NamespaceExamSecure
  9. {
  10.  /// 
  11.  /// Abstract description of ItemToDoc. 
  12.  /// 
  13.  Public ClassItemToDoc: System. Windows. Forms. Form
  14. {
  15. ObjectStrFileName;
  16. Object Nothing;
  17. Word. ApplicationClass myWordApp =NewWord. ApplicationClass ();
  18. Word. Document myWordDoc;
  19. StringStrContent ="";
  20.  
  21. PrivateSystem. ComponentModel. Container components =Null;
  22.  
  23. PublicItemToDoc ()
  24. {
  25. // 
  26. // Required for Windows Form Designer support 
  27. // 
  28. InitializeComponent ();
  29.  
  30. // 
  31. // TODO: add Any constructor code after InitializeComponent calls 
  32. // 
  33. }
  34. [STAThread]
  35. Static VoidMain ()
  36. {
  37. System. Windows. Forms. Application. Run (NewItemToDoc ());
  38. }
  39. /// 
  40. /// Clear all resources in use. 
  41. /// 
  42. Protected Override VoidDispose (BoolDisposing)
  43. {
  44. If(Disposing)
  45. {
  46. If(Components! =Null)
  47. {
  48. Components. Dispose ();
  49. }
  50. }
  51. Base. Dispose (disposing );
  52.  
  53. # Region Windows Form Designer generated code 
  54. /// 
  55. /// The designer supports the required methods-do not use the code editor to modify 
  56. /// Content of this method. 
  57. /// 
  58. Private VoidInitializeComponent ()
  59. {
  60. // 
  61. // ItemToDoc 
  62. // 
  63. This. AutoScaleBaseSize =NewSystem. Drawing. Size (6, 14 );
  64. This. ClientSize =NewSystem. Drawing. Size (292,273 );
  65. This. Name ="ItemToDoc";
  66. This. Text ="ItemToDoc";
  67. This. Load + =NewSystem. EventHandler (This. ItemToDoc_Load );
  68.  
  69. # Endregion 
  70.  
  71. Private VoidItemToDoc_Load (ObjectSender, System. EventArgs e)
  72. {
  73. WriteFile ();
  74. }
  75. Private VoidWriteFile ()
  76. {
  77. StrFileName = System. Windows. Forms. Application. StartupPath +"\ Question bank"+ GetRandomString () +"2.16.doc";
  78. Object Nothing = System. Reflection. Missing. Value;
  79. MyWordDoc = myWordApp. Documents. Add (RefNothing,RefNothing,RefNothing,RefNothing ); 
  80.  
  81. # Region writes read data from the database to the word file 
  82.  
  83. StrContent ="Question Bank \ n \ r";
  84. WriteFile (strContent );
  85. StrContent ="Test Database";
  86. WriteFile (strContent ); 
  87.  
  88.  
  89. # Endregion 
  90. // Save the content of the WordDoc object as a DOC object 
  91. MyWordDoc. SaveAs (RefStrFileName,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing );
  92. // Close the WordDoc Document Object 
  93. MyWordDoc. Close (RefNothing,RefNothing,RefNothing );
  94. // Close the WordApp Component Object 
  95. MyWordApp. Quit (RefNothing,RefNothing,RefNothing );
  96. }
  97.  
  98. /// 
  99. /// Obtain a random string 
  100. /// 
  101. /// 
  102. Private StringGetRandomString ()
  103. {
  104. DateTime iNow = DateTime. Now;
  105. StringStrDate = iNow. ToString ("YyyyMMddHHmmffff");
  106. Random ran =NewRandom ();
  107. IntIRan = Convert. ToInt32 (10000 * ran. NextDouble ());
  108. StringStrRan = iRan. ToString ();
  109. // If the number of digits is not enough, add 0. 
  110. IntIRanlen = strRan. Length;
  111. For(IntI = 0; I <4-iRanlen; I ++)
  112. {
  113. StrRan ="0"+ StrRan;
  114. }
  115. ReturnStrDate + strRan;
  116. }
  117.  
  118.  
  119. /// 
  120. /// Write the string to the Word file 
  121. /// 
  122. /// String to be written 
  123. Private VoidWriteFile (StringStr)
  124. {
  125. MyWordDoc. Paragraphs. Last. Range. Text = str;
  126. }
  127.  
  128.  
  129. }
  130. }

The above is the implementation code for. NET operations on Word.

  1. Summary of ASP. NET beginners
  2. In-depth research on Repeater controls: Maximum Flexibility
  3. Getting started with DataList controls
  4. Exploring the operation mechanism of the DataGrid Web Control
  5. Discuss the similarity between ASP. NET data Web controls

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.