设计了一个Word文档批量生成小工具软件

最近,因工作需要开发了一个文档自动生成的小工具软件,即批量替换Word文档模板中设定标签为指定的内容。Word文档模板为事先已经排版好的报表、公文等文件,但留下部分内容需要更改,将该内容设定为一个标签(如[申请人]),然后用Excel表中该标签指定的内容进行替换。如只替换单个文件手动修改即可,用该软件意义不大,但要修改上百个文档,该软件的作用就体现出来了。
该软件是在Visual Studio 2017 Community下用C#和WPF设计实现的,操作Excel和Word文档用微软自己的Mircrosoft.Office.Interop.ExcelMircrosoft.Office.Interop.Word组件。主界面如下:
软件运行主界面
其中:
1、Excel总表格式为N行、M列数据。其中第一行为标签名,第一列为要创建的目标文件夹名,其余内容为不同文件夹下将文档模板中出现的标签名替换成的内容,其内容样式如下:
这里写图片描述

注意:子文件夹可以是需处理的文件号
2、文档模板是指要处理的文档报表、证明等Word文件,文件内容中有部分标签需要用Excel总表中相关内容进行替换,替换后的内容另存到Excel总表中第一列表示的目标文件夹中,文件名为《目标文件名+文档模板文件名》。如以下报表:
文档模板样式
3、操作步骤:
第一步:选择起始文件目录,目标文件夹将作为起始文件目录的子目录;
第二步:选择Excel总表;
第三步:添加文档模板(可添加多个文档模板);
第四步:选择要创建的文件夹(即要处理的文件号),可多选;
第五步:以上四步操作完成后,即可点击《批量处理》进行文档的自动替换生成。

设计该软件的关键时如何操作Excel和Word文当,现列出其主要代码,操作Excel文档的主要代码如下:

           //打开Excel总表
            //创建1个工作簿,相当于1个Excel文件
            //Excel的文档结构是 Workbook->Worksheet(1个Workbook可以包含多个Worksheet)
            Excel.Application excel = new Excel.Application();
            try
            {
                excel.Visible = false;
                excel.DisplayAlerts = false;
                Excel.Workbook wb = null;
                wb = excel.Workbooks.Open(dialog.FileName);
                Excel.Range rng2 = null;
                Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1]; //索引从1开始 
                try
                {

                    int rowCount = 0;//有效行,索引从1开始
                    int colCount = 0;//有效列,索引从1开始
                    rowCount = ws.UsedRange.Rows.Count;//赋值有效行
                    colCount = ws.UsedRange.Columns.Count;//赋值有效列
                    excelMarkList.Clear();
                    excelContentList.Clear();
                    LstDirGen.Items.Clear();
                    for (int i = 1; i <= colCount; i++)
                    {
                        rng2 = (Excel.Range)ws.Cells[1, i];
                        excelMarkList.Add(rng2.Value2);
                    }
                    for (int i = 2; i <= rowCount; i++)
                    {
                        //tmpList.Clear();
                        List<string> tmpList = new List<string>();
                        for (int j = 1; j <= colCount; j++)
                        {
                            rng2 = (Excel.Range)ws.Cells[i, j];
                            if (j == 1)
                            {
                                //将第一列加到列表控件中 
                                LstDirGen.Items.Add(rng2.Value2);
                            }
                            tmpList.Add(rng2.Value2);
                        }
                        excelContentList.Add(tmpList);
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (wb != null) wb.Close();
                    if (ws != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                    }
                    if (rng2 != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rng2);
                        rng2 = null;
                    }
                    if (excel != null)
                    {
                        excel.Workbooks.Close();
                        excel.Quit();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (excel != null)
                {
                    excel.Workbooks.Close();
                    excel.Quit();
                }

            }

操作Word文档的主要代码如下:

            MSWord.Application app = null;
            MSWord.Document doc = null;
            object oMissing = System.Reflection.Missing.Value;
            object replace = MSWord.WdReplace.wdReplaceAll;
            app = new MSWord.Application();//创建word应用程序
            try
            {
                int inc = 0;
                for (int i = 0; i < selectItemsIndex.Count; i++)
                {
                    //创建子目录
                    string tmpDir = initPath + "/" + selectDirList[i];
                    if (!Directory.Exists(tmpDir))
                    {
                        Directory.CreateDirectory(tmpDir);
                    }
                    //模板文档循环

                    foreach (string docFile in docList)
                    {
                        string dstFile = initPath + "/" + selectDirList[i] + "/" +
                           selectDirList[i] + Path.GetFileName(docFile);
                        //拷贝文件
                        File.Copy(docFile, dstFile, true);
                        object fileName = (dstFile);//模板文件
                                                    //打开模板文件                            
                        doc = app.Documents.Open(ref fileName,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                        for (int j = 0; j < excelMarkList.Count; j++)
                        {
                            string strOldText = excelMarkList[j];
                            string strNewText = excelContentList[selectItemsIndex[i]][j];
                            doc.Content.Find.Text = strOldText;
                            object FindText, ReplaceWith, Replace;// 
                            object MissingValue = Type.Missing;
                            FindText = strOldText;//要查找的文本 
                            ReplaceWith = strNewText;//替换文本 
                            Replace = MSWord.WdReplace.wdReplaceAll;
                            doc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
                            doc.Content.Find.Execute(
                            ref FindText, ref MissingValue,
                            ref MissingValue, ref MissingValue,
                            ref MissingValue, ref MissingValue,
                            ref MissingValue, ref MissingValue, ref MissingValue,
                            ref ReplaceWith, ref Replace,
                            ref MissingValue, ref MissingValue,
                            ref MissingValue, ref MissingValue);
                        }

                        doc.Close(MSWord.WdSaveOptions.wdSaveChanges);
                        bgworker.ReportProgress(++inc);
                        dspOkFile = dstFile;
                    }

                }
                bgworker.CancelAsync();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
            finally
            {
                //关闭wordApp组件对象 
                app.Quit();
                app = null;

            }

编译好的软件放在百度网盘上,有兴趣的朋友可以下载使用
软件下载地址: https://pan.baidu.com/s/1vF_-jmFnbLdFmpbr1aiEeA

猜你喜欢

转载自blog.csdn.net/mysolisoft/article/details/82048351