河南新大牧业有限公司,戴拿奥特曼全集国语,乙肝作弊药
本文以一个简单的小例子,简述利用c#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正。
在项目中,点击项目名称右键-->管理nuget程序包,打开nuget包管理器窗口,进行搜索下载即可,如下图所示:
1 using microsoft.office.interop.word; 2 using system; 3 using system.collections.generic; 4 using system.data; 5 using system.io; 6 using system.linq; 7 using system.reflection; 8 using system.text; 9 using system.threading.tasks; 10 11 namespace etword 12 { 13 public class wordhelper 14 { 15 public 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, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, ref omissing, 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(ref omissing); 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(ref heading2); 43 opara0.range.insertparagraphafter(); 44 system.data.datatable dttemplate = databasehelper.gettemplatebydept(dept); 45 int jj = 0; 46 foreach (datarow dr1 in dttemplate.rows) 47 { 48 string template = dr1["template"].tostring(); 49 string user1 = dr1["user1"].tostring(); 50 string remark = dr1["remark"].tostring(); 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 = 1; 56 57 wordapp.selection.movedown(ref wdline, ref ncount, ref omissing); 58 wordapp.selection.typeparagraph(); 59 paragraph opara1 = worddoc.content.paragraphs.add(ref omissing); 60 opara1.range.select(); 61 opara1.range.text = string.format("{0}-{1}、{2}", ii + 1, jj + 1, template); 62 //opara1.range.font.bold = 1; 63 //opara1.format.spaceafter = 5; 64 opara1.set_style(ref heading3); 65 opara1.range.insertparagraphafter(); 66 wordapp.selection.movedown(ref wdline, ref ncount, ref omissing); 67 wordapp.selection.typeparagraph(); 68 //设置表格 69 table table = worddoc.tables.add(wordapp.selection.range, row, column, ref omissing, ref omissing); 70 71 table.borders.outsidelinestyle = wdlinestyle.wdlinestylesingle; 72 table.borders.insidelinestyle = wdlinestyle.wdlinestylesingle; 73 table.range.font.bold = 0; 74 table.preferredwidthtype = wdpreferredwidthtype.wdpreferredwidthauto; 75 table.columns[1].width = 60f; 76 table.columns[2].width = 100f; 77 table.columns[3].width = 100f; 78 table.columns[4].width = 60f; 79 table.columns[5].width = 100f; 80 //列的合并 81 cell cell = table.cell(1, 2); 82 cell.merge(table.cell(1, 5)); 83 cell cell2 = table.cell(2, 2); 84 cell2.merge(table.cell(2, 5)); 85 cell cell3 = table.cell(3, 2); 86 cell3.merge(table.cell(3, 5)); 87 //赋值 88 table.cell(1, 1).range.text = "流程名称:"; 89 table.cell(2, 1).range.text = "使用人:"; 90 table.cell(3, 1).range.text = "流程说明:"; 91 table.cell(4, 1).range.text = "节点"; 92 table.cell(4, 2).range.text = "节点名"; 93 table.cell(4, 3).range.text = "处理人员"; 94 table.cell(4, 4).range.text = "处理方式"; 95 table.cell(4, 5).range.text = "跳转信息"; 96 table.cell(1, 2).range.text = template; 97 table.cell(2, 2).range.text = user1; 98 table.cell(3, 2).range.text = remark; 99 int kk = 5; 100 foreach (datarow dr2 in dtdata.rows) 101 { 102 table.cell(kk, 1).range.text = (kk - 4).tostring(); 103 table.cell(kk, 2).range.text = dr2["nodename"].tostring(); 104 table.cell(kk, 3).range.text = dr2["doname"].tostring(); 105 table.cell(kk, 4).range.text = dr2["dotype"].tostring(); 106 table.cell(kk, 5).range.text = string.empty; 107 kk++; 108 } 109 table.cell(kk - 1, 5).range.select(); 110 111 wordapp.selection.movedown(ref wdline, ref ncount, ref omissing);//移动焦点 112 wordapp.selection.typeparagraph();//插入段落 113 114 jj++; 115 } 116 ii++; 117 } 118 119 //保存 120 worddoc.save(); 121 worddoc.close(ref omissing, ref omissing, ref omissing); 122 wordapp.quit(ref omissing, ref omissing, ref omissing); 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 static void createfile(string filepath) 135 { 136 if (!file.exists(filepath)) 137 { 138 using (filestream fs = file.create(filepath)) 139 { 140 141 } 142 } 143 } 144 } 145 }
关于源码下载链接
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论