WORD文件合并处理

近段时间在公司遇到一个问题,需要把所有的word文件进行合并,形成一个word文件便于打印,主要解决的是成批量的文件的时候,如果一个个操作的话,会非常的繁琐。

需要引用using Microsoft.Office.Interop.Word;  

忘记是看哪位大神的博客了,在此贴出来,以便于其它同仁能够借鉴,如有侵权等,请联系本人,在此向借鉴的大神表示歉意。

FolderBrowserDialog dialog = new FolderBrowserDialog
            {
                Description = @"请选择文件路径"
            };


            if (dialog.ShowDialog() != DialogResult.OK) return;
            string foldPath = dialog.SelectedPath;
            DirectoryInfo folder = new DirectoryInfo(foldPath);
            FileInfo[] files = folder.GetFiles();
            var app = new Microsoft.Office.Interop.Word.Application();
            var doc = app.Documents.Add();
            foreach (var d in files)
            {
                if (d.Extension == ".doc")
                {
                    app.Selection.InsertFile(d.FullName);
                }
                else if (d.Extension == ".jpg" || d.Extension == ".png")
                {
                    object linkToFile = false;
                    object saveWithDocument = true;
                    object anchor = app.Selection.Range;
                    app.ActiveDocument.InlineShapes.AddPicture(d.FullName, ref linkToFile, ref saveWithDocument, ref anchor);
                    app.ActiveDocument.InlineShapes[1].Width = 300f;//图片宽度
                    app.ActiveDocument.InlineShapes[1].Height = 400f;//图片高度 
                }


                //app.Selection.InsertBreak(WdBreakType.wdPageBreak);
            }
            doc.SaveAs(FileName: Path.Combine(folder.FullName, "最终合并.docx"));
            doc.Close();
            app.Quit();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值