C#生成Word文档代码示例
程序员文章站
2023-10-27 09:10:46
public bool createwordfile(string _filename, "数据list或者你c#要写的数据")
{...
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 }
上一篇: 爆笑之逗B剧场第221季
下一篇: 最近优化个人博客,一下调整了很多问题