Use C # To dynamically generate a Word document and enter the data in the Word Table

Source: Internet
Author: User

To use C # To operate on word, add reference first:

1. Add reference-> com-> Microsoft Word 11.0 Object Library

2. Add the. CS File

Using word;

The following example contains C # operations such as creating, inserting tables, and setting styles for Word documents:

(In this exampleCodeSome data-related information is omitted. It is important to introduce some C # operations on Word documents)

Public String createwordfile (string checkedinfo)

...{

String message = "";

Try

...{

Object nothing = system. reflection. Missing. value;

Directory. createdirectory ("C:/CNSI"); // directory where the file is created

String name = "CNSI _" + datetime. Now. tow.string () + ". Doc ";

Object filename = "C: // CNSI //" + name; // file storage path

// Create a Word document

Word. Application wordapp = new word. applicationclass ();

Word. Document worddoc = wordapp. Documents. Add (ref nothing, ref nothing );

// Add a header

Wordapp. activewindow. View. type = wdviewtype. wdoutlineview;

Wordapp. activewindow. View. seekview = wdseekview. wdseekprimaryheader;

Wordapp. activewindow. activepane. selection. insertafter ("[header content]");

Wordapp. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphright; // set the right alignment

Wordapp. activewindow. View. seekview = wdseekview. wdseekmaindocument; // jump out of the header settings

Wordapp. selection. paragraphformat. linespacing = 15f; // set the line spacing of the document.

// Move the focus and wrap the line

Object COUNT = 14;

Object wdline = word. wdunits. wdline; // change the line;

Wordapp. selection. movedown (ref wdline, ref count, ref nothing); // move the focus

Wordapp. selection. typeparagraph (); // insert a paragraph

// Create a table in the document

Word. Table newtable = worddoc. Tables. Add (wordapp. selection. Range, 12, 3, ref nothing, ref nothing );

// Set the table style

Newtable. Borders. outsidelinestyle = word. wdlinestyle. wdlinestylethickthinlargegap;

Newtable. Borders. insidelinestyle = word. wdlinestyle. wdlinestylesingle;

Newtable. Columns [1]. width = 100f;

Newtable. Columns [2]. width = 220f;

Newtable. Columns [3]. width = 105f;

// Fill in the table content

Newtable. Cell (1, 1). range. Text = "product details table ";

Newtable. Cell (1, 1). range. Bold = 2; // set the font of the cell to bold.

// Merge Cells

Newtable. Cell (1, 1). Merge (newtable. Cell (1, 3 ));

Wordapp. selection. cells. verticalalignment = word. wdcellverticalignment. wdcellalignverticalcenter; // vertically centered

Wordapp. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphcenter; // horizontally centered

// Fill in the table content

Newtable. Cell (2, 1). range. Text = "Basic Product Information ";

Newtable. Cell (2, 1). range. Font. Color = word. wdcolor. wdcolordarkblue; // set the font color in the cell.

// Merge Cells

Newtable. Cell (2, 1). Merge (newtable. Cell (2, 3 ));

Wordapp. selection. cells. verticalignment = word. wdcellverticalignment. wdcellalignverticalcenter;

// Fill in the table content

Newtable. Cell (3, 1). range. Text = "brand name :";

Newtable. Cell (3, 2). range. Text = BrandName;

// Vertically merge Cells

Newtable. Cell (3, 3). Select (); // select a row

Object moveunit = word. wdunits. wdline;

Object movecount = 5;

Object moveextend = word. wdmovementtype. wdextend;

Wordapp. selection. movedown (ref moveunit, ref movecount, ref moveextend );

Wordapp. selection. cells. Merge ();

// Insert an image

String filename = picture; // path of the image

Object linktofile = false;

Object savewithdocument = true;

Object anchor = worddoc. application. selection. range;

Worddoc. application. activedocument. inlineshapes. addpicture (filename, ref linktofile, ref savewithdocument, ref anchor );

Worddoc. application. activedocument. inlineshapes [1]. width = 100f; // Image Width

Worddoc. application. activedocument. inlineshapes [1]. Height = 100f; // Image Height

// Set the image to a peripheral image

Word. Shape S = worddoc. application. activedocument. inlineshapes [1]. converttoshape ();

S. wrapformat. type = word. wdwraptype. wdwrapsquare;

Newtable. Cell (12, 1). range. Text = "special properties ";

Newtable. Cell (12, 1). Merge (newtable. Cell (12, 3 ));

// Add rows to the table

Worddoc. content. Tables [1]. Rows. Add (ref nothing );

Worddoc. Paragraphs. Last. range. Text = "document creation time:" + datetime. Now. tostring (); // "pay-as-you-go"

Worddoc. Paragraphs. Last. Alignment = word. wdparagraphalignment. wdalignparagraphright;

// Save the file

Worddoc. saveas (ref filename, ref nothing, ref nothing, ref nothing, ref nothing );

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

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

Message = Name + "the document is generated successfully and saved to C: CNSI ";

}

Catch

...{

Message = "An error occurred while exporting the file! ";

}

Return message;

}

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.