C#根据Word模版生成Word文件
本文实例为大家分享了C#根据Word模版生成Word文的具体代码,供大家参考,具体内容如下
1、指定的word模版
2、生成word类
添加com Microsoft word 11.0 Object Library 引用
using System; using System.Collections.Generic; using System.Data; using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; using System.IO; namespace Headfree.DefUI { public class WordUtility { private object tempFile = null; private object saveFile = null; private static Word._Document wDoc = null; //word文档 private static Word._Application wApp = null; //word进程 private object missing = System.Reflection.Missing.Value; public WordUtility(string tempFile, string saveFile) { this.tempFile = Path.Combine(Application.StartupPath, @tempFile); this.saveFile = Path.Combine(Application.StartupPath, @saveFile); } /// <summary> /// 模版包含头部信息和表格,表格重复使用 /// </summary> /// <param name="dt">重复表格的数据</param> /// <param name="expPairColumn">word中要替换的表达式和表格字段的对应关系</param> /// <param name="simpleExpPairValue">简单的非重复型数据</param> public bool GenerateWord(DataTable dt, Dictionary<string, string> expPairColumn, Dictionary<string, string> simpleExpPairValue) { if (!File.Exists(tempFile.ToString())) { MessageBox.Show(string.Format("{0}模版文件不存在,请先设置模版文件。", tempFile.ToString())); return false; } try { wApp = new Word.Application(); wApp.Visible = false; wDoc = wApp.Documents.Add(ref tempFile, ref missing, ref missing, ref missing); wDoc.Activate();// 当前文档置前 bool isGenerate = false; if (simpleExpPairValue != null && simpleExpPairValue.Count > 0) isGenerate = ReplaceAllRang(simpleExpPairValue); // 表格有重复 if (dt != null && dt.Rows.Count > 0 && expPairColumn != null && expPairColumn.Count > 0) isGenerate = GenerateTable(dt, expPairColumn); if (isGenerate) wDoc.SaveAs(ref saveFile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); DisposeWord(); return true; } catch (Exception ex) { MessageBox.Show("生成失败" + ex.Message); return false; } } /// <summary> /// 单个替换 模版没有重复使用的表格 /// </summary> /// <param name="dc">要替换的</param> public bool GenerateWord(Dictionary<string, string> dc) { return GenerateWord(null, null, dc); } private bool GenerateTable(DataTable dt, Dictionary<string, string> expPairColumn) { try { int tableNums = dt.Rows.Count; Word.Table tb = wDoc.Tables[1]; tb.Range.Copy(); Dictionary<string, object> dc = new Dictionary<string, object>(); for (int i = 0; i < tableNums; i++) { dc.Clear(); if (i == 0) { foreach (string key in expPairColumn.Keys) { string column = expPairColumn[key]; object value = null; value = dt.Rows[i][column]; dc.Add(key, value); } ReplaceTableRang(wDoc.Tables[1], dc); continue; } wDoc.Paragraphs.Last.Range.Paste(); foreach (string key in expPairColumn.Keys) { string column = expPairColumn[key]; object value = null; value = dt.Rows[i][column]; dc.Add(key, value); } ReplaceTableRang(wDoc.Tables[1], dc); } return true; } catch (Exception ex) { DisposeWord(); MessageBox.Show("生成模版里的表格失败。" + ex.Message); return false; } } private bool ReplaceTableRang(Word.Table table, Dictionary<string, object> dc) { try { object replaceArea = Word.WdReplace.wdReplaceAll; foreach (string item in dc.Keys) { object replaceKey = item; object replaceValue = dc[item]; table.Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); } return true; } catch (Exception ex) { DisposeWord(); MessageBox.Show(string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message)); return false; } } private bool ReplaceAllRang(Dictionary<string, string> dc) { try { object replaceArea = Word.WdReplace.wdReplaceAll; foreach (string item in dc.Keys) { object replaceKey = item; object replaceValue = dc[item]; wApp.Selection.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); } return true; } catch (Exception ex) { MessageBox.Show(string.Format("{0}模版中没有找到指定的要替换的表达式。{1}", tempFile, ex.Message)); return false; } } private void DisposeWord() { object saveOption = Word.WdSaveOptions.wdSaveChanges; wDoc.Close(ref saveOption, ref missing, ref missing); saveOption = Word.WdSaveOptions.wdDoNotSaveChanges; wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程 } } }
3、效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
下一篇:C#实现备忘录功能
本文标题:C#根据Word模版生成Word文件
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5424.html
您可能感兴趣的文章
- 01-10C#实现简单合并word文档的方法
- 01-10http图片上传安全性问题 根据ContentType (MIME) 判断其实不准确、不
- 01-10C#实现HTML转WORD及WORD转PDF的方法
- 01-10C#实现在网页中根据url截图并输出到网页的方法
- 01-10如何使用C#从word文档中提取图片
- 01-10C# DateTime.ToString根据不同语言生成相应的时间格式
- 01-10C# 中DateTime 的使用技巧汇总
- 01-10C#在DataTable中根据条件删除某一行的实现方法
- 01-10C#实现DataSet内数据转化为Excel和Word文件的通用类完整实例
- 01-10C#根据日期计算星期几的实例代码
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 01-10SublimeText编译C开发环境设置