C # generate WORD code

Source: Internet
Author: User

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. tolongdatestring () + ". Doc ";
Object filename = "C: // CNSI //" + name; // file storage path
// Create a Word document
Microsoft. Office. InterOP. Word. Application wordapp = new Microsoft. Office. InterOP. Word. applicationclass ();
Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. 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
Microsoft. Office. InterOP. Word. Table newtable = worddoc. Tables. Add (wordapp. selection. Range, 12, 3, ref nothing, ref nothing );
// Set the table style
Newtable. Borders. outsidelinestyle = Microsoft. Office. InterOP. Word. wdlinestyle. wdlinestylethickthinlargegap;
Newtable. Borders. insidelinestyle = Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. Word. wdcellverticalignment. wdcellalignverticalcenter; // vertically Center
Wordapp. selection. paragraphformat. Alignment = Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. Word. wdcolor. wdcolordarkblue; // set the font color in the cell.
// Merge Cells
Newtable. Cell (2, 1). Merge (newtable. Cell (2, 3 ));
Wordapp. selection. cells. verticalalignment = Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. Word. wdunits. wdline;
Object movecount = 5;
Object moveextend = Microsoft. Office. InterOP. Word. wdmovementtype. wdextend;
Wordapp. selection. movedown (ref moveunit, ref movecount, ref moveextend );
Wordapp. selection. cells. Merge ();
// Insert an image
String filename = @ "C:/picture.jpg"; // 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
Microsoft. Office. InterOP. Word. Shape S = worddoc. application. activedocument. inlineshapes [1]. converttoshape ();
S. wrapformat. type = Microsoft. Office. InterOP. 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 = Microsoft. Office. InterOP. 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;
}
Address: http://www.51itshare.cn/html/WangLuoBianCheng/8676/

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.