最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#根据Word模版生成Word文件
时间:2022-06-25 07:46:01 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了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); } ////// 模版包含头部信息和表格,表格重复使用 /// /// 重复表格的数据 /// word中要替换的表达式和表格字段的对应关系 /// 简单的非重复型数据 public bool GenerateWord(DataTable dt, DictionaryexpPairColumn, Dictionary 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; } } /// /// 单个替换 模版没有重复使用的表格 /// /// 要替换的 public bool GenerateWord(Dictionarydc) { return GenerateWord(null, null, dc); } private bool GenerateTable(DataTable dt, Dictionary expPairColumn) { try { int tableNums = dt.Rows.Count; Word.Table tb = wDoc.Tables[1]; tb.Range.Copy(); Dictionary dc = new Dictionary (); for (int i = 0; i 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 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、效果
相关文章
- 斗罗大陆猎魂世界幽香绮罗有什么技能 10-22
- 三国志异闻录周泰技能怎么样 10-22
- 口袋斗罗大陆暗器如何选择 10-22
- 王者荣耀曹操万灵伏威皮肤多少钱-王者荣耀曹操万灵伏威价格皮肤介绍 10-22
- 人渣兑换码是什么 人渣scum最新2025兑换码一览 10-22
- 原子陨落兑换码分享 原子陨落最新2025兑换码大全 10-22