国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#根據Word模版生成Word文件

2019-10-29 21:08:57
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C#根據Word模版生成Word文的具體代碼,供大家參考,具體內容如下

1、指定的word模版

C#,Word模版,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#,Word模版,Word文件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克前旗| 龙口市| 宁武县| 阿勒泰市| 如皋市| 馆陶县| 景宁| 金阳县| 卢氏县| 兴和县| 鞍山市| 灵台县| 正镶白旗| 县级市| 珠海市| 宜宾市| 隆子县| 墨脱县| 诸暨市| 鄂托克旗| 天全县| 运城市| 郁南县| 玛沁县| 万年县| 车险| 柳江县| 福州市| 阳朔县| 嘉祥县| 崇明县| 曲沃县| 静海县| 策勒县| 舟曲县| 北票市| 北票市| 从化市| 灵宝市| 济南市| 黑龙江省|