Operate word with. net

Source: Internet
Author: User
To operate word, 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. The procedure is to open project> Add reference> browse in the menu bar, and find MSWord in the "select component" dialog box. after olb, click OK to introduce this object library file. vs.net will automatically convert the library file to the DLL component, so that we only need to create this component object in the source code to achieve the purpose of word operations!

The Application of namespace in the CS code file, such as using word; example: using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using word; namespace examsecure
{
///
/// Abstract description of itemtodoc.
///
Public class itemtodoc: system. Windows. Forms. Form
{
Object strfilename;
Object nothing;
Word. applicationclass mywordapp = new word. applicationclass ();
Word. Document myworddoc;
String strcontent = ""; private system. componentmodel. Container components = NULL; Public itemtodoc ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();//
// Todo: add Any constructor code after initializecomponent calls
//
}
[Stathread]
Static void main ()
{
System. Windows. Forms. application. Run (New itemtodoc ());
}
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
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 void initializecomponent ()
{
//
// Itemtodoc
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,273 );
This. Name = "itemtodoc ";
This. Text = "itemtodoc ";
This. Load + = new system. eventhandler (this. itemtodoc_load );}
# Endregion private void itemtodoc_load (Object sender, system. eventargs E)
{
Writefile ();
}
Private void writefile ()
{

Strfilename = system. Windows. Forms. application. startuppath + "\ test database [" + getrandomstring () + "pai.doc ";
Object nothing = system. reflection. Missing. value;
Myworddoc = mywordapp. Documents. Add (ref nothing, ref nothing );

# Region writes read data from the database to the Word file strcontent = "Test Database \ n \ r ";
Writefile (strcontent );

Strcontent = "Test Database ";
Writefile (strcontent); # endregion

// Save the content of the worddoc object as a doc object
Myworddoc. saveas (ref strfilename, ref nothing, ref nothing, ref nothing, ref nothing );
// Close the worddoc Document Object
Myworddoc. Close (ref nothing, ref nothing, ref nothing );
// Close the wordapp Component Object
Mywordapp. Quit (ref nothing, ref nothing, ref nothing );
}///
/// Obtain a random string
///
///
Private string getrandomstring ()
{
Datetime Inow = datetime. now;
String strdate = Inow. tostring ("yyyymmddhhmmffff ");

Random ran = new random ();
Int Iran = convert. toint32 (10000 * ran. nextdouble ());
String strran = Iran. tostring ();
// If the number of digits is not enough, add 0.
Int iranlen = strran. length;
For (INT I = 0; I <4-iranlen; I ++)
{
Strran = "0" + strran;
}
Return strdate + strran;
}///
/// Write the string to the Word file
///
/// String to be written
Private void writefile (string Str)
{
Myworddoc. Paragraphs. Last. range. Text = STR;
}
}
}

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.