C # operate Word documents

Source: Internet
Author: User

 

Dynamic Creation of Word documents

Object Path; // file path variable

MSWord. Application wordapp; // word ApplicationProgramVariable

MSWord. Document worddoc; // Word document variable

Path = @ "C: \ teacheremploy.doc"; // path

Wordapp = new MSWord. applicationclass (); // Initialization

// Delete an existing one

If (file. exists (string) path ))

{

File. Delete (string) path );

}

// Because the com library is used, many variables need to be replaced by missing. value.

Object nothing = missing. value;

Worddoc = wordapp. Documents. Add (ref nothing, ref nothing );

 

Set document width
Wordapp. selection. pagesetup. leftmargin = wordapp. centimeterstopoints (float. parse ("2"); // left edge distance

Wordapp. activewindow. activepane. horizontalpercentscrolled = 11;

Wordapp. selection. pagesetup. rightmargin = wordapp. centimeterstopoints (float. parse ("2"); // distance from the right edge

Object start = type. missing;

Object end = type. missing;

Object Unit = type. missing;

Object COUNT = type. missing;

Worddoc. Range (ref start, ref end). Delete (ref unit, ref count );

 

When adding text in the first line of the document, do not specify the start and end values. Use the default nothing directly.

// Title

String strtitle = "Yancheng Normal University professional and technical personnel recruitment period task book ";

Worddoc. Range (ref start, ref end). insertbefore (strtitle );

// Set the title Style

Worddoc. Range (ref start, ref end). Font. Name = "verdana ";

Worddoc. Range (ref start, ref end). Font. Bold = 1;

Worddoc. Range (ref start, ref end). Font. size = 17f;

Worddoc. Range (ref start, ref end). paragraphformat. Alignment =

MSWord. wdparagraphalignment. wdalignparagraphcenter;

Range is specified to add after the title


// Add a table

Object Location = strtitle. length;

MSWord. Range = worddoc. Range (ref location, ref location );

MSWord. Table = worddoc. Tables. Add (range, 4, 2, ref nothing, ref nothing ); 

Tables is a collection of all tables in a Word document. Each table is accessed through an index.

// Table style settings

Worddoc. Tables [1]. range. Font. Name = "";

Worddoc. Tables [1]. range. Font. Bold = 0;

Worddoc. Tables [1]. range. Font. size = 12f;

Worddoc. Tables [1]. range. paragraphformat. Alignment =

Microsoft. Office. InterOP. Word. wdparagraphalignment. wdalignparagraphleft;

 

// Locate the cell

Cell (I, j) to locate a cell (as shown in Figure)

 

 

Merge and split Cells

Object rownum = 1;

Object columnnum = 5;

Table. Cell (1, 2). Split (ref rownum, ref columnnum); // split

Table. Cell (6, 2). Merge (table. Cell (6, 3); // merge

 

Center horizontally and vertically

Table. Cell (8, 1). range. cells. verticalignment = MSWord. wdcellverticalignment. wdcellalignverticalcenter; // center vertically

Table. Cell (8, 1). range. paragraphformat. Alignment = MSWord. wdparagraphalignment. wdalignparagraphcenter; // horizontally centered

 

Save Word documents and release resources

// Wdsaveformat is the word document storage format

Object format = MSWord. wdsaveformat. wdformatdocument;

// Save the content of the worddoc object as a doc object

Worddoc. saveas (ref path, ref format, ref nothing, ref nothing, ref nothing, ref nothing );

// Close the worddoc Document Object

Worddoc. Close (ref nothing, ref nothing, ref nothing );

// Close the wordapp Component Object

Wordapp. Quit (ref nothing, ref nothing, ref nothing );

Section set and table set

Worddoc. Paragraphs // section set

Worddoc. Tables // table set

If you operate worddoc. Paragraphs and worddoc. Tables directly, the effect will be reflected in all tables and paragraphs.

You can access each section and table through the index. Note that the index is based on 1!

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.