• c# word文档合并


    调用方法

       string _foldPath = textBox1.Text; 
    
    
                    string foldPath = _foldPath;
                    if (string.IsNullOrEmpty(foldPath))
                    {
    
                        MessageBox.Show("请选择文件路径!");
                        return;
                    } 
    
    
    
                    string[] arrFiles = Directory.GetFiles(foldPath, "*.doc?");
    
                    if (arrFiles.Count()<1)
                    {
                        MessageBox.Show("路径不包含word文档!");
                        return;
                    }
                    string strBase = System.Environment.CurrentDirectory;
                    string tempDoc = string.Format(@"{0}Appmodel.doc",strBase);
                    string outDoc = string.Format(@"{0}App{1}.doc", strBase,DateTime.Now.ToString("yyyyMMddHHmmssfff"));   
                    WordDocumentMerger _WordDocumentMerger = new WordDocumentMerger();
                    _WordDocumentMerger.CopyMerge(tempDoc, arrFiles, outDoc);
    
                    textBox3.Text = string.Format("合并文件成功:{0}", outDoc);

    合并执行类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using System.Reflection;
    using System.IO;
    using System.Diagnostics;
    
    
    namespace Eipsoft.Common
    {
        /// <summary>
        /// Word文档合并类
        /// </summary>
        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();
                objDocLast.SpellingChecked = false;//关闭Word的拼写检查
                objDocLast.ShowSpellingErrors = false;//关闭Word的拼写错误提示 
            }
            #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 循环合并多个文件(复制合并重复的文件)
            ///
            /// 循环合并多个文件(复制合并重复的文件)
            ///
            /// 模板文件
            /// 需要合并的文件
            /// 合并后的输出文件
            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;
                }
            }
            ///
            /// 循环合并多个文件(复制合并重复的文件)
            ///
            /// 模板文件
            /// 需要合并的文件
            /// 合并后的输出文件
            public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
            {
                string[] arrFiles = Directory.GetFiles(strCopyFolder);
                CopyMerge(tempDoc, arrFiles, outDoc);
            }
            #endregion
    
            #region 循环合并多个文件(插入合并文件)
            ///
            /// 循环合并多个文件(插入合并文件)
            ///
            /// 模板文件
            /// 需要合并的文件
            /// 合并后的输出文件
            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;
                }
            }
            ///
            /// 循环合并多个文件(插入合并文件)
            ///
            /// 模板文件
            /// 需要合并的文件
            /// 合并后的输出文件
            public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
            {
                string[] arrFiles = Directory.GetFiles(strCopyFolder);
                InsertMerge(tempDoc, arrFiles, outDoc);
            }
            #endregion
        }
    }
  • 相关阅读:
    一个数组找出第k大的数(待补)
    变动二叉树
    判断一个二叉树
    Redis的过期策略和内存淘汰机制
    sql连接详解
    http 请求和格式
    java基础知识
    分页信息
    持续集成之Jenkins自动部署war包到远程服务器
    no-sql数据库之redis
  • 原文地址:https://www.cnblogs.com/lhlong/p/14366790.html
Copyright © 2020-2023  润新知