程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成歸並多個word文檔的辦法

C#完成歸並多個word文檔的辦法

編輯:C#入門知識

C#完成歸並多個word文檔的辦法。本站提示廣大學習愛好者:(C#完成歸並多個word文檔的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成歸並多個word文檔的辦法正文


本文實例講述了C#完成歸並多個word文檔的辦法,長短常具有適用價值的技能。分享給年夜家供年夜家參考。

詳細完成辦法以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace driverexam.WordReport
{
  public class WordDocumentMerger
  {
    private ApplicationClass objApp = null;
    private Document objDocLast = null;
    private Document objDocBeforeLast = null;
    public WordDocumentMerger()
    {
      objApp = new ApplicationClass();
    }
    #region 翻開文件
    private void Open(string tempDoc)
    {
      object objTempDoc = tempDoc;
      object objMissing = System.Reflection.Missing.Value;

   objDocLast = objApp.Documents.Open(
      ref objTempDoc, //FileName 
      ref objMissing, //ConfirmVersions 
      ref objMissing, //ReadOnly 
      ref objMissing, //AddToRecentFiles 
      ref objMissing, //PasswordDocument 
      ref objMissing, //PasswordTemplate 
      ref objMissing, //Revert 
      ref objMissing, //WritePasswordDocument 
      ref objMissing, //WritePasswordTemplate 
      ref objMissing, //Format 
      ref objMissing, //Enconding 
      ref objMissing, //Visible 
      ref objMissing, //OpenAndRepair 
      ref objMissing, //DocumentDirection 
      ref objMissing, //NoEncodingDialog 
      ref objMissing //XMLTransform 
      );
      objDocLast.Activate();
    }
    #endregion

    #region 保留文件到輸入模板
    private void SaveAs(string outDoc)
    {
      object objMissing = System.Reflection.Missing.Value;
      object objOutDoc = outDoc;
      objDocLast.SaveAs(
      ref objOutDoc, //FileName 
      ref objMissing, //FileFormat 
      ref objMissing, //LockComments 
      ref objMissing, //PassWord 
      ref objMissing, //AddToRecentFiles 
      ref objMissing, //WritePassword 
      ref objMissing, //ReadOnlyRecommended 
      ref objMissing, //EmbedTrueTypeFonts 
      ref objMissing, //SaveNativePictureFormat 
      ref objMissing, //SaveFormsData 
      ref objMissing, //SaveAsAOCELetter, 
      ref objMissing, //Encoding 
      ref objMissing, //InsertLineBreaks 
      ref objMissing, //AllowSubstitutions 
      ref objMissing, //LineEnding 
      ref objMissing //AddBiDiMarks 
      );
    }
    #endregion

    #region 輪回歸並多個文件(復制歸並反復的文件)
    /// <summary> 
    /// 輪回歸並多個文件(復制歸並反復的文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrCopies">須要歸並的文件</param> 
    /// <param name="outDoc">歸並後的輸入文件</param> 
    public void CopyMerge(string tempDoc, string[] arrCopies, string outDoc)
    {
      object objMissing = Missing.Value;
      object objFalse = false;
      object objTarget = WdMergeTarget.wdMergeTargetSelected;
      object objUseFormatFrom = WdUseFormattingFrom.wdFormattingFromSelected;
      try
      {
        //翻開模板文件 
        Open(tempDoc);
        foreach (string strCopy in arrCopies)
        {
          objDocLast.Merge(
          strCopy, //FileName 
          ref objTarget, //MergeTarget 
          ref objMissing, //DetectFormatChanges 
          ref objUseFormatFrom, //UseFormattingFrom 
          ref objMissing //AddToRecentFiles 
          );
          objDocBeforeLast = objDocLast;
          objDocLast = objApp.ActiveDocument;
          if (objDocBeforeLast != null)
          {
            objDocBeforeLast.Close(
            ref objFalse, //SaveChanges 
            ref objMissing, //OriginalFormat 
            ref objMissing //RouteDocument 
            );
          }
        }
        //保留到輸入文件 
        SaveAs(outDoc);
        foreach (Document objDocument in objApp.Documents)
        {
          objDocument.Close(
          ref objFalse, //SaveChanges 
          ref objMissing, //OriginalFormat 
          ref objMissing //RouteDocument 
          );
        }
      }
      finally
      {
        objApp.Quit(
        ref objMissing, //SaveChanges 
        ref objMissing, //OriginalFormat 
        ref objMissing //RoutDocument 
        );
        objApp = null;
      }
    }
    /// <summary> 
    /// 輪回歸並多個文件(復制歸並反復的文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrCopies">須要歸並的文件</param> 
    /// <param name="outDoc">歸並後的輸入文件</param> 
    public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
    {
      string[] arrFiles = Directory.GetFiles(strCopyFolder);
      CopyMerge(tempDoc, arrFiles, outDoc);
    }
    #endregion

    #region 輪回歸並多個文件(拔出歸並文件)
    /// <summary> 
    /// 輪回歸並多個文件(拔出歸並文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrCopies">須要歸並的文件</param> 
    /// <param name="outDoc">歸並後的輸入文件</param> 
    public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
    {
      object objMissing = Missing.Value;
      object objFalse = false;
      object confirmConversion = false;
      object link = false;
      object attachment = false;
      try
      {
        //翻開模板文件 
        Open(tempDoc);
        foreach (string strCopy in arrCopies)
        {
          objApp.Selection.InsertFile(
          strCopy,
          ref objMissing,
          ref confirmConversion,
          ref link,
          ref attachment
          );
        }
        //保留到輸入文件 
        SaveAs(outDoc);
        foreach (Document objDocument in objApp.Documents)
        {
          objDocument.Close(
          ref objFalse, //SaveChanges 
          ref objMissing, //OriginalFormat 
          ref objMissing //RouteDocument 
          );
        }
      }
      finally
      {
        objApp.Quit(
        ref objMissing, //SaveChanges 
        ref objMissing, //OriginalFormat 
        ref objMissing //RoutDocument 
        );
        objApp = null;
      }
    }
    /// <summary> 
    /// 輪回歸並多個文件(拔出歸並文件) 
    /// </summary> 
    /// <param name="tempDoc">模板文件</param> 
    /// <param name="arrCopies">須要歸並的文件</param> 
    /// <param name="outDoc">歸並後的輸入文件</param> 
    public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
    {
      string[] arrFiles = Directory.GetFiles(strCopyFolder);
      InsertMerge(tempDoc, arrFiles, outDoc);
    }
    #endregion
  }
}

信任本文所述對年夜家的C#法式設計有必定的自創價值。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved