How to Create a table in a Word document using c #

Source: Internet
Author: User

Copy codeThe Code is as follows: public string CreateWordFile ()
{
String message = "";
Try
{
Object Nothing = System. Reflection. Missing. Value;
String name = "xiehuan.doc ";
Object filename = @ "C: \ Users \ xiehuan \ xxx \" + 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 = 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
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 (System. Exception e)
{
MessageBox. Show (e. Message );
}
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.