欢迎来到山村网

C#生成Word文档代码示例

2019-03-02 14:01:36浏览:362 来源:山村网   
核心摘要:  这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下  ? 1234567891011121314151617181

  这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下

  ?

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 43 44 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 84 85 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, "数据List或者你C#要写的数据") { #region 开始生成Word try { string strtitle = "任务导出"; object oEndOfDoc = "//endofdoc"; Object Nothing = System.Reflection.Missing.Value; Object filename = _filename; //创建Word文档 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, ref Nothing, ref Nothing); //设置页眉 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任务导出"); WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMaindocument;//跳出页眉设置 //任务导出------名字 Word.Paragraph oPara1; oPara1 = WordDoc.Content.Paragraphs.Add(ref Nothing); oPara1.Range.Text = strtitle; oPara1.Range.Font.Bold = 1; oPara1.Range.Font.Name = "宋体"; oPara1.Range.Font.Size = 20; oPara1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; oPara1.Format.SpaceAfter = 5; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter(); #region 循环每个表 foreach (var v in lst_task) { #region 循环每一个列,产生一行数据 //描述信息 Word.Paragraph oPara3; object oRng = WordDoc.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 = "宋体"; oPara3.Range.Font.Size = 9; oPara3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; oPara3.Format.SpaceBefore = 1; oPara3.Format.SpaceAfter = 1; oPara3.Range.InsertParagraphAfter(); //插入表格 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; //填充表格内容 newTable.Cell(1, 1).Range.Text = "项目名称"; newTable.Cell(1, 2).Range.Text = "1"; newTable.Cell(1, 3).Range.Text = "模块名称"; newTable.Cell(1, 4).Range.Text = "2"; newTable.Cell(2, 1).Range.Text = "负责人名"; newTable.Cell(2, 2).Range.Text = "3"; newTable.Cell(2, 3).Range.Text = "状态"; newTable.Cell(2, 4).Range.Text = "4"; newTable.Cell(3, 1).Range.Text = "发布时间"; newTable.Cell(3, 2).Range.Text = "5"; newTable.Cell(3, 3).Range.Text = "要求完成时间"; newTable.Cell(3, 4).Range.Text = "6"; newTable.Cell(4, 1).Range.Text = "完成时间"; newTable.Cell(4, 2).Range.Text = "7"; newTable.Cell(4, 3).Range.Text = "分配人"; newTable.Cell(4, 4).Range.Text = "8"; newTable.Cell(5, 1).Range.Text = "任务描述"; 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 Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); #endregion //关闭WinWord进程 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 }
(责任编辑:豆豆)
下一篇:

C#实现把txt文本数据快速读取到excel中

上一篇:

python中管道用法入门实例

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com