C# 动态生成word文档
发布时间:2020-12-16 01:13:09 所属栏目:百科 来源:网络整理
导读:本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正。 在工程中引用word的动态库 在项目中,点击项目名称右键--管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示: 涉及知识点
本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正。 在工程中引用word的动态库在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示: 涉及知识点
生成文档效果图核心代码1 using Microsoft.Office.Interop.Word; 2 System; 3 System.Collections.Generic; 4 System.Data; 5 System.IO; 6 System.Linq; 7 System.Reflection; 8 System.Text; 9 System.Threading.Tasks; 10 11 namespace ETWord 12 { 13 public class WordHelper 14 { 15 static void CreateWordFile(string filePath) 16 { 17 18 try 19 { 20 CreateFile(filePath); 21 // 22 MessageFilter.Register(); 23 object wdLine = WdUnits.wdLine; 24 object oMissing = Missing.Value; 25 object fileName = filePath; 26 object heading2 = WdBuiltinStyle.wdStyleHeading2; 27 object heading3 = WdBuiltinStyle.wdStyleHeading3; 28 29 _Application wordApp = new Application(); 30 wordApp.Visible = true; 31 _Document wordDoc = wordApp.Documents.Open(ref fileName,ref oMissing,1)">ref oMissing); 32 System.Data.DataTable dtDepts = DatabaseHelper.getDept(); 33 int ii = 0 34 foreach (DataRow dr in dtDepts.Rows) 35 { 36 string dept = dr["dept"].ToString(); 37 Paragraph oPara0 = wordDoc.Content.Paragraphs.Add( 38 oPara0.Range.Text = string.Format({0}-{1}",ii + 1,dept); 39 //oPara0.Range.Font.Bold = 1; 40 oPara0.Format.SpaceAfter = 5; 41 oPara0.Range.Select(); 42 oPara0.set_Style( heading2); 43 oPara0.Range.InsertParagraphAfter(); 44 System.Data.DataTable dtTemplate = DatabaseHelper.getTemplateByDept(dept); 45 int jj = 46 foreach (DataRow dr1 dtTemplate.Rows) 47 { 48 string template = dr1[template 49 string user1 = dr1[user1 50 string remark = dr1[remark 51 System.Data.DataTable dtData = DatabaseHelper.getDataByDeptAndTemplate(dept,template); 52 int count = dtData.Rows.Count; 53 int row = count + 4 54 int column = 5 55 object ncount = 56 57 wordApp.Selection.MoveDown(ref wdLine,1)">ref ncount,1)"> 58 wordApp.Selection.TypeParagraph(); 59 Paragraph oPara1 = wordDoc.Content.Paragraphs.Add( 60 oPara1.Range.Select(); 61 oPara1.Range.Text = {0}-{1}、{2}1,jj + 62 oPara1.Range.Font.Bold = 1; 63 oPara1.Format.SpaceAfter = 5; 64 oPara1.set_Style( heading3); 65 oPara1.Range.InsertParagraphAfter(); 66 wordApp.Selection.MoveDown( 67 68 设置表格 69 Table table = wordDoc.Tables.Add(wordApp.Selection.Range,row,column,1)"> 70 71 table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle; 72 table.Borders.InsideLineStyle = 73 table.Range.Font.Bold = 74 table.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthAuto; 75 table.Columns[1].Width = 60f; 76 table.Columns[2].Width = 100f; 77 table.Columns[3].Width = 78 table.Columns[4].Width = 79 table.Columns[5].Width = 80 列的合并 81 Cell cell = table.Cell(2); 82 cell.Merge(table.Cell()); 83 Cell cell2 = table.Cell(2,1)"> 84 cell2.Merge(table.Cell( 85 Cell cell3 = table.Cell(3,1)"> 86 cell3.Merge(table.Cell( 87 赋值 88 table.Cell(1).Range.Text = 流程名称: 89 table.Cell(使用人: 90 table.Cell(流程说明: 91 table.Cell(4,1)">节点 92 table.Cell(2).Range.Text = 节点名 93 table.Cell(3).Range.Text = 处理人员 94 table.Cell(4).Range.Text = 处理方式 95 table.Cell(5).Range.Text = 跳转信息 96 table.Cell(2).Range.Text = template; 97 table.Cell( user1; 98 table.Cell( remark; 99 int kk = 100 foreach (DataRow dr2 dtData.Rows) 101 { 102 table.Cell(kk,1)">1).Range.Text = (kk - ).ToString(); 103 table.Cell(kk,1)">2).Range.Text = dr2[NodeName104 table.Cell(kk,1)">3).Range.Text = dr2[DoName105 table.Cell(kk,1)">4).Range.Text = dr2[DoType106 table.Cell(kk,1)">5).Range.Text = .Empty; 107 kk++108 } 109 table.Cell(kk - ).Range.Select(); 110 111 wordApp.Selection.MoveDown(ref oMissing);移动焦点 112 wordApp.Selection.TypeParagraph();插入段落 113 114 jj++115 } 116 ii++117 } 118 119 保存 120 wordDoc.Save(); 121 wordDoc.Close(122 wordApp.Quit(123 MessageFilter.Revoke(); 124 125 } 126 catch (Exception e) 127 128 Console.WriteLine(e.Message); 129 Console.WriteLine(e.StackTrace); 130 131 132 } 133 134 private void CreateFile(135 136 if (!File.Exists(filePath)) 137 138 using (FileStream fs = File.Create(filePath)) 139 140 141 142 143 144 } 145 } 关于word开发中字体大小满足中文出版中使用字号作为字体大小的单位的需要,它允许用户同时使用“号”和 “磅”作为字体大小的单位。 磅值与字号之间的换算关系如下: word中设置宽度时,采用磅为单位,磅与厘米之间的换算关系如下: ? 备注
关于源码下载链接 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |