C # generates a Word document code sample

Source: Internet
Author: User
Tags header

This article mainly introduces the C # generation Word Document code example, this article directly gives the code example, needs the friend may refer to under

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108-109 public bool Createwordfile (string _filename, "data list or data that you C # to write") {#region start generating word try {  string strtitle = task Export ; Object oEndOfDoc = "//endofdoc"; Object nothing = System.Reflection.Missing.Value; Object filename = _filename; 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 no, ref nothing, ref Nothing);    //Set Header WordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter ("Wpfoa task Export"); WordApp.Selection.ParagraphFormat.Alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphright;//Set Right Alignment WordApp.ActiveWindow.View.SeekView = microsoft.office.interop.word.wdseekview.wdseekmaindocument;//out of header settings//task Export------name Word.Paragraph oPara1; OPara1 = WORDDOC.CONTENT.PARAGRAPHS.ADD (ref nothing); OPara1.Range.Text = strtitle; OPara1.Range.Font.Bold = 1; OPara1.Range.Font.Name = "Song Body"; OPara1.Range.Font.Size = 20; OPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; OPara1.Format.SpaceAfter = 5; PT spacing after paragraph. OPara1.Range.InsertParagraphAfter (); #region loops each table   foreach (Var v in lst_task) {#region loops each column, generating a row of data//description information word.paragraph oPara3; object orng = Wordd Oc. Bookmarks.get_item (ref oEndOfDoc). Range; OPara3 = WORDDOC.CONTENT.PARAGRAPHS.ADD (ref orng); OPara3.Range.Text = ""; OPara3.Range.Font.Bold = 0; OPara3.Range.Font.Name = "Song Body"; OPara3.Range.Font.Size = 9; OPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; OPara3.Format.SpaceBefore = 1; OPara3.Format.SpaceAfter = 1; OPara3.Range.InsertParagraphAfter ();  //Insert table word.table newtable; Word.Range wrdrng = WordDoc.Bookmarks.get_Item (ref OENdofdoc). Range; newtable = WordDoc.Tables.Add (wrdrng, 5, 4, ref nothing, ref nothing); NEWTABLE.COLUMNS[1]. Width = 60; NEWTABLE.COLUMNS[2]. Width = 145; NEWTABLE.COLUMNS[3]. Width = 80; NEWTABLE.COLUMNS[4]. Width = 145; NewTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; NewTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;  //Fill table Contents Newtable.cell (1, 1). Range.Text = "project name"; Newtable.cell (1, 2). Range.Text = "1"; Newtable.cell (1, 3). Range.Text = "module name"; Newtable.cell (1, 4). Range.Text = "2"; Newtable.cell (2, 1). Range.Text = "Name of the person in charge"; Newtable.cell (2, 2). Range.Text = "3"; Newtable.cell (2, 3). Range.Text = "state"; Newtable.cell (2, 4). Range.Text = "4"; Newtable.cell (3, 1). Range.Text = "Release Time"; Newtable.cell (3, 2). Range.Text = "5"; Newtable.cell (3, 3). Range.Text = "Required completion time"; Newtable.cell (3, 4). Range.Text = "6"; Newtable.cell (4, 1). Range.Text = "Finish Time"; Newtable.cell (4, 2). Range.Text = "7"; Newtable.cell (4, 3). RAnge. Text = "Assigned person"; Newtable.cell (4, 4). Range.Text = "8"; Newtable.cell (5, 1). Range.Text = "task description"; Newtable.cell (5, 2). Merge (Newtable.cell (5, 4)); Newtable.cell (5, 2). Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; Newtable.cell (5, 2). Range.Text = "12313213123213"; #endregion}     Worddoc.saveas (ref filename, ref nothing, ref no, ref nothing, ref no, ref nothing, R EF Nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref No Thing);   Worddoc.close (ref nothing, ref no, ref nothing); Wordapp.quit (ref nothing, ref no, ref nothing); #endregion//close Winword process system.diagnostics.process[] myprocess = System.Diagnostics.Process.GetProcessesByName (" Winword "); Myprocess[0]. Kill (); return true;  } catch {  system.diagnostics.process[] myprocess = System.Diagnostics.Process.GetProcessesByName (" Winword "); Myprocess[0]. Kill (); return false; } #endregion}

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.