Ejemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //ComWordHelper wordHelper = new ComWordHelper();
            var p1 = "F:\\Test\\test1.docx";
            var p2 = "F:\\Test\\test2.docx";
            var p3 = "F:\\Test\\test3.docx";
            //wordHelper.InsertMerge(p1, p2, p3);
            AsposeWordHepler asposeWordHepler = new AsposeWordHepler();

            asposeWordHepler.MergeDocument(p1, p2);
            //asposeWordHepler.ReplaceString(p3, "Ω", "欧姆");
        }
Ejemplo n.º 2
0
        private void CreateReportWord(WorkOrderInfo workOrderInfo, string wordModelDirPath)
        {
            //生成Word数据集
            FillTemplateWord fillTemplateWord = new FillTemplateWord();
            var result = fillTemplateWord.GetWordInfos(workOrderInfo);

            if (!result.Succeed)
            {
                var errmsg = "工单号:" + workOrderInfo.WorkOrderNo + ",错误信息:" + result.Message;
                MessageBox.Show(errmsg);
                return;
            }

            //找到模板文档
            System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(wordModelDirPath);

            var specmodelfiles = directoryInfo.GetFiles().ToList();

            specmodelfiles = specmodelfiles.Where(x => x.Extension.Contains("doc") && x.Name.Contains(workOrderInfo.Spec + ")")).ToList();
            if (specmodelfiles == null || specmodelfiles.Count() == 0)
            {
                var errmsg = "工单号:" + workOrderInfo.WorkOrderNo + ",规格型号:" + workOrderInfo.Spec + ",错误信息:文件夹无此规格型号的模板。";
                MessageBox.Show(errmsg);
                return;
            }
            var wordmodelpath = specmodelfiles.FirstOrDefault().FullName;

            var wordlist = result.ChannelWordInfos;
            //根据Word数据集,生成对应名称的Word模板文档,并向每个文档填充对应数据
            AsposeWordHepler asposeWordHepler = new AsposeWordHepler();
            List <string>    filnames         = new List <string>();
            int i = 0;

            foreach (var word in wordlist)
            {
                string newfilename = "";
                if (i > 0)
                {
                    newfilename = word.WorkOrderNo + "出厂检验报告(" + word.Spec + ")-" + i.ToString() + ".docx";
                }
                else
                {
                    newfilename = word.WorkOrderNo + "出厂检验报告(" + word.Spec + ").docx";
                }

                var rpdirpath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Reports");
                if (!System.IO.Directory.Exists(rpdirpath))
                {
                    System.IO.Directory.CreateDirectory(rpdirpath);
                }
                var newfilefullname = System.IO.Path.Combine(rpdirpath, newfilename);

                asposeWordHepler.Copy(wordmodelpath, newfilefullname);
                fillTemplateWord.ReplaceWord(newfilefullname, word);
                filnames.Add(newfilefullname);
                i++;
            }
            //第一个文档合并其他文档的数据
            if (filnames.Count() > 1)
            {
                var fristname = filnames.FirstOrDefault();
                filnames.Remove(fristname);
                foreach (var item in filnames)
                {
                    asposeWordHepler.MergeDocument(fristname, item);
                    System.IO.File.Delete(item);
                }
            }
        }