C # Examples of how to create a Word document, insert a table, set a style, and more

Source: Internet
Author: User
Tags datetime
This article mainly describes the C # on the creation of Word documents, insert tables, set style and other operational examples, has a certain reference value, now share to everyone, the need for friends can refer to

Using Word;

The following examples include C # actions to create a Word document, insert a table, set a style, and so on:

(Some of the code in the example is omitted from the Data Information section, and it is important to introduce some C # methods of manipulating Word documents.)

 public string Createwordfile (String checkedinfo) ...   {String message = ""; Try ...    {Object nothing = System.Reflection.Missing.Value; Directory.CreateDirectory ("C:/cnsi");    Create the directory where the file is located string name = "Cnsi_" + DateTime.Now.ToShortString () + ". Doc"; Object filename = "c://cnsi//" + name;    File save path//create Word document Word.Application WordApp = new Word.applicationclass ();    Word.Document WordDoc = WORDAPP.DOCUMENTS.ADD (ref nothing, ref-nothing, ref-nothing, ref-nothing);    Add 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 Right alignment    WordApp.ActiveWindow.View.SeekView = wdseekview.wdseekmaindocument;//jump out of header settings    WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//Set the document's line spacing//move focus and wrap object count = 14; Object Wdline = word.wdunits.wdline;//is changed by one line; WordApp.Selection.MoveDown (ref wdline, ref count, ref nothing);//Mobile Focus WordApp.Selection.TypeParagraph ();//Insert paragraph//text     Document CREATE TABLE Word.table newtable = WordDoc.Tables.Add (WordApp.Selection.Range, 3, ref nothing, ref nothing);     Set 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; Populate table Contents Newtable.cell (1, 1).     Range.Text = "Product detail table"; Newtable.cell (1, 1). Range.bold = 2;//Sets the font in the cell to bold//merge cell Newtable.cell (1, 1).     Merge (Newtable.cell (1, 3));     WordApp.Selection.Cells.VerticalAlignment = word.wdcellverticalalignment.wdcellalignverticalcenter;//Vertical Center     WordApp.Selection.ParagraphFormat.Alignment = word.wdparagraphalignment.wdalignparagraphcenter;//Horizontal Center//Fill table contents Newtable.cell (2, 1). range.text = "Product basic information"; Newtable.cell (2, 1). Range.Font.Color = word.wdcolor.wdcolordarkblue;//Sets the font color within the cell//Merge cell Newtable.cell (2, 1).     Merge (Newtable.cell (2, 3));     WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; Populate table Contents Newtable.cell (3, 1).     Range.Text = "brand Name:"; Newtable.cell (3, 2).     Range.Text = brandname; Vertically merges cell Newtable.cell (3, 3).     Select ();//Select one line of 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 Picture String FileName = picture;//picture where the path is 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;//picture width worddoc.application.activedocument.inlineshapes[1]. Height = 100f;//picture height//Set the picture to a four-week surround type word.shape s = worddoc.application.activedocument.inlineshapes[1].     ConvertToShape ();           S.wrapformat.type = Word.WdWrapType.wdWrapSquare; Newtable.cell (12, 1).     Range.Text = "Product special attribute"; Newtable.cell (12, 1).      Merge (Newtable.cell (12, 3)); Add row worddoc.content.tables[1] to the table.            Rows.Add (ref nothing); WordDoc.Paragraphs.Last.Range.Text = "Document creation time:" + DateTime.Now.ToString ();//"inscribed" WordDoc.Paragraphs.Last.Alignment = W Ord.     Wdparagraphalignment.wdalignparagraphright; File save Worddoc.saveas (ref filename, ref nothing, ref no, ref nothing, ref nothing, ref no, ref nothing, ref     Nothing, ref nothing, ref no, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);     Worddoc.close (ref nothing, ref no, ref nothing);     Wordapp.quit (ref nothing, ref no, ref nothing); Message=name+ "Document generated successfully to save to C:cnsi"; } catch ... {message = "file Export exception!"}   ";  } return message; }


Related Article

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.