. 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:
- UsingSystem;
- UsingSystem. Drawing;
- UsingSystem. Collections;
- UsingSystem. ComponentModel;
- UsingSystem. Windows. Forms;
- UsingWord;
-
- NamespaceExamSecure
- {
- ///
- /// Abstract description of ItemToDoc.
- ///
- Public ClassItemToDoc: System. Windows. Forms. Form
- {
- ObjectStrFileName;
- Object Nothing;
- Word. ApplicationClass myWordApp =NewWord. ApplicationClass ();
- Word. Document myWordDoc;
- StringStrContent ="";
-
- PrivateSystem. ComponentModel. Container components =Null;
-
- PublicItemToDoc ()
- {
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent ();
-
- //
- // TODO: add Any constructor code after InitializeComponent calls
- //
- }
- [STAThread]
- Static VoidMain ()
- {
- System. Windows. Forms. Application. Run (NewItemToDoc ());
- }
- ///
- /// Clear all resources in use.
- ///
- Protected Override VoidDispose (BoolDisposing)
- {
- If(Disposing)
- {
- If(Components! =Null)
- {
- Components. Dispose ();
- }
- }
- Base. Dispose (disposing );
- }
-
- # Region Windows Form Designer generated code
- ///
- /// The designer supports the required methods-do not use the code editor to modify
- /// Content of this method.
- ///
- Private VoidInitializeComponent ()
- {
- //
- // ItemToDoc
- //
- This. AutoScaleBaseSize =NewSystem. Drawing. Size (6, 14 );
- This. ClientSize =NewSystem. Drawing. Size (292,273 );
- This. Name ="ItemToDoc";
- This. Text ="ItemToDoc";
- This. Load + =NewSystem. EventHandler (This. ItemToDoc_Load );
-
- }
- # Endregion
-
- Private VoidItemToDoc_Load (ObjectSender, System. EventArgs e)
- {
- WriteFile ();
- }
- Private VoidWriteFile ()
- {
- StrFileName = System. Windows. Forms. Application. StartupPath +"\ Question bank"+ GetRandomString () +"2.16.doc";
- Object Nothing = System. Reflection. Missing. Value;
- MyWordDoc = myWordApp. Documents. Add (RefNothing,RefNothing,RefNothing,RefNothing );
-
- # Region writes read data from the database to the word file
-
- StrContent ="Question Bank \ n \ r";
- WriteFile (strContent );
- StrContent ="Test Database";
- WriteFile (strContent );
-
-
- # Endregion
- // Save the content of the WordDoc object as a DOC object
- MyWordDoc. SaveAs (RefStrFileName,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing,RefNothing );
- // Close the WordDoc Document Object
- MyWordDoc. Close (RefNothing,RefNothing,RefNothing );
- // Close the WordApp Component Object
- MyWordApp. Quit (RefNothing,RefNothing,RefNothing );
- }
-
- ///
- /// Obtain a random string
- ///
- ///
- Private StringGetRandomString ()
- {
- DateTime iNow = DateTime. Now;
- StringStrDate = iNow. ToString ("YyyyMMddHHmmffff");
- Random ran =NewRandom ();
- IntIRan = Convert. ToInt32 (10000 * ran. NextDouble ());
- StringStrRan = iRan. ToString ();
- // If the number of digits is not enough, add 0.
- IntIRanlen = strRan. Length;
- For(IntI = 0; I <4-iRanlen; I ++)
- {
- StrRan ="0"+ StrRan;
- }
- ReturnStrDate + strRan;
- }
-
-
- ///
- /// Write the string to the Word file
- ///
- /// String to be written
- Private VoidWriteFile (StringStr)
- {
- MyWordDoc. Paragraphs. Last. Range. Text = str;
- }
-
-
- }
- }
The above is the implementation code for. NET operations on Word.
- Summary of ASP. NET beginners
- In-depth research on Repeater controls: Maximum Flexibility
- Getting started with DataList controls
- Exploring the operation mechanism of the DataGrid Web Control
- Discuss the similarity between ASP. NET data Web controls