最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#如何根據(jù)Word模版生成Word文件

這篇文章給大家分享的是有關C#如何根據(jù)Word模版生成Word文件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、芒康網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5頁面制作、商城網(wǎng)站定制開發(fā)、集團公司官網(wǎng)建設、成都外貿網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為芒康等各大城市提供網(wǎng)站開發(fā)制作服務。

具體內容如下

1、指定的word模版

C#如何根據(jù)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);
    }
 
    /// 
    /// 模版包含頭部信息和表格,表格重復使用
    /// 
    /// 重復表格的數(shù)據(jù)
    /// word中要替換的表達式和表格字段的對應關系
    /// 簡單的非重復型數(shù)據(jù)
    public bool GenerateWord(DataTable dt, Dictionary expPairColumn, 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(Dictionary dc)
    {
      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 < 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 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、效果

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

感謝各位的閱讀!關于“C#如何根據(jù)Word模版生成Word文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!


當前題目:C#如何根據(jù)Word模版生成Word文件
當前地址:http://fisionsoft.com.cn/article/goiooj.html