记录C# 导入模版填写内容

该代码段展示了一种方法,通过Microsoft.Office.Interop.Word库在C#中处理Word文档。首先,它从模板文件中复制内容到新文件,并用字典中的键值对替换书签。然后,将指定的图片插入到特定书签位置。最后,将处理后的Word文档导出为PDF格式。
摘要由CSDN通过智能技术生成

        public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList)
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            string TemplateFile = wordTemplatePath;
            File.Copy(TemplateFile, newFileName);
            _Document doc = new Microsoft.Office.Interop.Word.Document();
            object obj_NewFileName = newFileName;
            object obj_Visible = false;
            object obj_ReadOnly = false;
            object obj_missing = System.Reflection.Missing.Value;

            doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
                ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
                ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
                ref obj_missing, ref obj_missing, ref obj_missing,
                ref obj_missing);
            doc.Activate();
            if (wordLableList.Count > 0)
            {
                object what = WdGoToItem.wdGoToBookmark;
                foreach (var item in wordLableList)
                {

                    object lableName = item.Key;
                    if (doc.Bookmarks.Exists(item.Key))
                    {
                        doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//光标移动书签的位置
                        doc.ActiveWindow.Selection.TypeText(item.Value);//在书签处插入的内容 
                        doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置插入内容的Alignment
                    }
                }
            }
            foreach (Bookmark bk in doc.Bookmarks)
            {
                 if (bk.Name == "创伤图片")
                {
                    bk.Select();
                    Selection sel = app.Selection;
                    string picture = string.Format("{0}Images\\创伤分析图片.png", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
                    sel.InlineShapes.AddPicture(picture);
                }
            }
            object obj_IsSave = true;
            doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);
        }
 

            if (File.Exists(wordTemplatePath))
            {
                Dictionary<string, string> wordLableList = new Dictionary<string, string>();
                wordLableList.Add("姓名", "钟飞");
                wordLableList.Add("性别", "男");
                wordLableList.Add("生日", "1997年4月1日");
                wordLableList.Add("病人编号", "1009");
                wordLableList.Add("放射编号", "8669");
                wordLableList.Add("检查时间", "2023/4/14 10:06:09");
                wordLableList.Add("报告时间", "2023/4/14 10:09:11");
                wordLableList.Add("检查部位", "左腕 前后位");
                wordLableList.Add("检查所见", "左手手腕骨质不连续,断端移位不明显,周围软组织肿胀;余骨质未见明确骨折现象。");
                wordLableList.Add("诊断意见", "左手手腕骨折。");
                Export(wordTemplatePath, newFileName, wordLableList);
                MessageBox.Show("导出成功!");
            }

后面添加转pdf操作

        public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList,string pdfpath)
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            string TemplateFile = wordTemplatePath;
            File.Copy(TemplateFile, newFileName);
            _Document doc = new Microsoft.Office.Interop.Word.Document();
            object obj_NewFileName = newFileName;
            object obj_Visible = false;
            object obj_ReadOnly = false;
            object obj_missing = System.Reflection.Missing.Value;

            doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
                ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
                ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
                ref obj_missing, ref obj_missing, ref obj_missing,
                ref obj_missing);
            doc.Activate();
            if (wordLableList.Count > 0)
            {
                object what = WdGoToItem.wdGoToBookmark;
                foreach (var item in wordLableList)
                {

                    object lableName = item.Key;
                    if (doc.Bookmarks.Exists(item.Key))
                    {
                        doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//光标移动书签的位置
                        doc.ActiveWindow.Selection.TypeText(item.Value);//在书签处插入的内容 
                        doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//设置插入内容的Alignment
                    }
                }
            }
            foreach (Microsoft.Office.Interop.Word.Bookmark bk in doc.Bookmarks)
            {
                 if (bk.Name == "创伤图片")
                {
                    bk.Select();
                    Selection sel = app.Selection;
                    string picture = string.Format("{0}Images\\创伤分析图片.png", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
                    sel.InlineShapes.AddPicture(picture);
                }
            }
            object obj_IsSave = true;

            doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);

            doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing,
            ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing,
            ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible,
            ref obj_missing, ref obj_missing, ref obj_missing,
            ref obj_missing);
            doc.Activate();
            doc.ExportAsFixedFormat(pdfpath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportFromTo);
            obj_IsSave = true;
            doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing);


        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值