最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#根據(jù)Word模版生成Word文件

 更新時(shí)間:2017年10月27日 16:27:29   作者:大西瓜3721  
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)Word模版生成Word文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#根據(jù)Word模版生成Word文的具體代碼,供大家參考,具體內(nèi)容如下

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進(jìn)程
    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>
    /// 模版包含頭部信息和表格,表格重復(fù)使用
    /// </summary>
    /// <param name="dt">重復(fù)表格的數(shù)據(jù)</param>
    /// <param name="expPairColumn">word中要替換的表達(dá)式和表格字段的對(duì)應(yīng)關(guān)系</param>
    /// <param name="simpleExpPairValue">簡單的非重復(fù)型數(shù)據(jù)</param>
    public bool GenerateWord(DataTable dt, Dictionary<string, string> expPairColumn, Dictionary<string, string> simpleExpPairValue)
    {
      if (!File.Exists(tempFile.ToString()))
      {
        MessageBox.Show(string.Format("{0}模版文件不存在,請(qǐng)先設(shè)置模版文件。", 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();// 當(dāng)前文檔置前
 
        bool isGenerate = false;
 
        if (simpleExpPairValue != null && simpleExpPairValue.Count > 0)
          isGenerate = ReplaceAllRang(simpleExpPairValue);
 
        // 表格有重復(fù)
        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>
    /// 單個(gè)替換 模版沒有重復(fù)使用的表格
    /// </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}模版中沒有找到指定的要替換的表達(dá)式。{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}模版中沒有找到指定的要替換的表達(dá)式。{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); //關(guān)閉Word進(jìn)程
    }
  }
}

3、效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c# WPF中CheckBox樣式的使用總結(jié)

    c# WPF中CheckBox樣式的使用總結(jié)

    這篇文章主要介紹了c# WPF中CheckBox樣式的使用總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定

    C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定

    這篇文章介紹了C#使用反射實(shí)現(xiàn)延遲綁定的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#面向?qū)ο髮?shí)現(xiàn)圖書管理系統(tǒng)

    C#面向?qū)ο髮?shí)現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#面向?qū)ο髮?shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#獲取指定文件著作權(quán)信息的方法

    C#獲取指定文件著作權(quán)信息的方法

    這篇文章主要介紹了C#獲取指定文件著作權(quán)信息的方法,涉及C#中FileVersionInfo類的使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔

    C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔

    這篇文章主要為大家詳細(xì)介紹了C#使用NPOI將List數(shù)據(jù)導(dǎo)出到Excel文檔,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#項(xiàng)目中引用Swagger的詳細(xì)步驟和配置方式

    C#項(xiàng)目中引用Swagger的詳細(xì)步驟和配置方式

    本文詳細(xì)介紹了如何在C#項(xiàng)目中安裝和配置Swagger,包括添加相關(guān)NuGet包、配置Swagger服務(wù)和啟用Swagger中間件,還講解了如何為API添加注釋和描述,配置安全定義,以及如何使用Swagger進(jìn)行API測試和調(diào)試
    2025-02-02
  • 在WPF中實(shí)現(xiàn)全局快捷鍵功能

    在WPF中實(shí)現(xiàn)全局快捷鍵功能

    這篇文章介紹了在WPF中實(shí)現(xiàn)全局快捷鍵功能的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)獲取磁盤空間大小的方法

    C#實(shí)現(xiàn)獲取磁盤空間大小的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)獲取磁盤空間大小的方法,分別基于System.IO.DriveInfo.GetDrives方法與ManagementClass("Win32_LogicalDisk")來實(shí)現(xiàn)這一功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例

    C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例

    下面小編就為大家分享一篇C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#中System.Array.CopyTo() 和 System.Array.Clon() 的區(qū)別

    C#中System.Array.CopyTo() 和 System.Array.Clon()&nbs

    System.Array.CopyTo()和System.Array.Clone()是用于數(shù)組復(fù)制的兩種不同方法,本文就來介紹C,#中System.Array.CopyTo() 和 System.Array.Clon() 的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04

最新評(píng)論

抚松县| 北辰区| 苍溪县| 龙游县| 伊金霍洛旗| 德兴市| 林西县| 临桂县| 西丰县| 建德市| 团风县| 深州市| 邢台县| 滁州市| 仪征市| 密山市| 招远市| 微山县| 金塔县| 乌苏市| 嘉定区| 霞浦县| 德庆县| 马龙县| 彝良县| 洛阳市| 慈溪市| 景泰县| 墨竹工卡县| 南京市| 三门县| 友谊县| 开鲁县| 辉县市| 武功县| 安义县| 福海县| 依安县| 文山县| 陵水| 郴州市|