时间:2021-05-20
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 }
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在C#中打开word文档其实不算太难,方法也比较多。一.C#中打开word文档方法复制代码代码如下://在项目引用里添加上对MicrosoftWord11.0o
1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC。2.定制WORD文档的模板文件,在C#中操作WORD模板,生成新的WORD文档。
如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用1、添加引用->COM->MicrosoftWord11.0Ob
本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧。分享给大家供大家参考。具体实现方法如下:引言:前段时间有项目要用c#生成Word格式
本文实例为大家分享了C#根据Word模版生成Word文的具体代码,供大家参考,具体内容如下1、指定的word模版2、生成word类添加comMicrosoftw