文章目录

  • 1. 添加引用
  • 2.具体操作
    • 3.在页眉插入照片

1. 添加引用


实际程序集看当前电脑安装的软件不一样而有所差异。

2.具体操作

//获取应用路径
string appPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//获取模板路径
string templatePath = appPath + "Template\\Report.docx";//声明Word工作簿
Word.Application wordApp = new Word.Application();
Word.Document wordDoc =  null;//获取当前客户的项目记录
DataTable dtRecordItem = PGxMySqlDB.GetRecordItemListTable(customer.ID);//文档操作时是否可见
wordApp.Visible = false;for (int i = 0; i < dtRecordItem.Rows.Count; i++)
{//获取主项目记录ID
Int32 recordItemID = Convert.ToInt32(dtRecordItem.Rows[i]["ID"],FormatInfo.Number);
string itemSN = dtRecordItem.Rows[i]["ItemSN"].ToString();  //获取项目编号
Item item = PGxMySqlDB.GetItem(itemSN);     //读取项目信息try
{#region customer info//打开模板文件wordDoc = wordApp.Documents.Open(templatePath);//获取文件中的第一个表格,表格索引从1开始Word.Table baseInfoTable = wordDoc.Tables[1];//设置医院名称,将文本域第前四个字符替换掉wordDoc.Range(0, 4).Text = customer.Hospital;baseInfoTable.Cell(1, 2).Range.Text = customer.Name;                //客户名称baseInfoTable.Cell(1, 4).Range.Text = customer.Department;          //科室baseInfoTable.Cell(1, 6).Range.Text = customer.BedNumber;           //床号baseInfoTable.Cell(1, 8).Range.Text = customer.BarCode;             //条码if (customer.Sex == Gender.Female)                                  //性别{baseInfoTable.Cell(2, 2).Range.Text = Strings.Female;}else{baseInfoTable.Cell(2, 2).Range.Text = Strings.Male;}baseInfoTable.Cell(2, 4).Range.Text = customer.OutpatientOrInpatientNumber;         //门诊住院号baseInfoTable.Cell(2, 6).Range.Text = customer.Doctor;                              //医生baseInfoTable.Cell(2, 8).Range.Text = customer.DoctorPhone;                         //医生电话baseInfoTable.Cell(3, 2).Range.Text = customer.Age.ToString(FormatInfo.Number);     //年龄baseInfoTable.Cell(3, 4).Range.Text = customer.SampleType;                          //样本类型baseInfoTable.Cell(3, 6).Range.Text = customer.InspentionDepartment;                //送检单位baseInfoTable.Cell(4, 2).Range.Text = customer.SampleCondition;                     //样本情况baseInfoTable.Cell(4, 4).Range.Text = customer.Diagnosis;                           //临床诊断baseInfoTable.Cell(5, 2).Range.Text = customer.SamplingTime.Value.Date.ToString("dd/MM/yyyy", FormatInfo.DateTime);             //采样日期baseInfoTable.Cell(5, 4).Range.Text = customer.ReceiptTime.Value.Date.ToString("dd/MM/yyyy", FormatInfo.DateTime);              //接收日期baseInfoTable.Cell(5, 6).Range.Text = customer.Notes;                               //备注#endregion customer info <= ENDobject replaceAll = Word.WdReplace.wdReplaceAll;   //替换所有文本object missing = Type.Missing;  //空操作/*----------------更新主项目信息---------------------------*/wordApp.Selection.Find.Replacement.ClearFormatting();wordApp.Selection.Find.ClearFormatting();wordApp.Selection.Find.Text = "{ItemName}";wordApp.Selection.Find.Replacement.Text = item.SN + " " + item.ChineseName + "(" + item.EnglishName + ")";//执行替换,replace模式需要设置才能正常替换wordApp.Selection.Find.Execute(missing, missing, missing, missing,missing, missing, missing, missing,missing, missing, replaceAll, missing,missing, missing, missing);   /*----------------更新子项目检测结果---------------------------*/string subItemName = "";DataTable dtReportData = PGxMySqlDB.GetReportSubItemDataListTable(recordItemID); //获取报告数据for (int j = 0; j < dtReportData.Rows.Count; j++){wordDoc.Tables[2].Rows.Add();wordDoc.Tables[2].Cell(wordDoc.Tables[2].Rows.Count, 1).Range.Text = dtReportData.Rows[i]["SubitemName"].ToString();wordDoc.Tables[2].Cell(wordDoc.Tables[2].Rows.Count, 2).Range.Text = dtReportData.Rows[i]["MainGenotype"].ToString();wordDoc.Tables[2].Cell(wordDoc.Tables[2].Rows.Count, 3).Range.Text = "1A";wordDoc.Tables[2].Cell(wordDoc.Tables[2].Rows.Count, 4).Range.Text = dtReportData.Rows[i]["TestResult"].ToString();subItemName += dtReportData.Rows[i]["SubitemName"].ToString() + ";";  //子项目名称}/*----------------更新子项目(检测位点)---------------------------*/wordApp.Selection.Find.ClearFormatting();wordApp.Selection.Find.Text = "{Subitem}";wordApp.Selection.Find.Replacement.ClearFormatting();wordApp.Selection.Find.Replacement.Text = subItemName;wordApp.Selection.Find.Execute(missing, missing, missing, missing,missing, missing, missing, missing,missing, missing, replaceAll, missing,missing, missing, missing);   //执行替换string filePath = Settings.Default.ReportSavePath + "\\" + customer.Hospital + "_" + customer.BarCode + "(" + customer.Name + ")_" + item.SN + "(" + item.ChineseName + ")_" + DateTime.Now.ToString("yyyyMMdd HHmm", FormatInfo.DateTime);wordDoc.SaveAs2(filePath + ".docx");wordDoc.ExportAsFixedFormat(filePath + ".pdf", Word.WdExportFormat.wdExportFormatPDF);PGxMySqlDB.UpdateCustomerTestStatus(customer.ID, Status.Printed);   //客户状态更新为已打印Logs.AddRecordEdit(customer.ID, Strings.Print); //增加打印信息}
catch (Exception err)
{throw;
}
finally
{wordDoc.Close(); //关闭文件
}}//for (int i = 0; i < dtRecordItem.Rows.Count; i++)wordApp.Quit();   //退出应用

3.在页眉插入照片

            string logoFile = HospitalLogo.GetLogoFilePth();if (wordApp == null || string.IsNullOrEmpty(logoFile)) return;wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;   //编辑页眉wordApp.Selection.HeaderFooter.LinkToPrevious = false;Object _Range = wordApp.Selection.Range;  //要向Word文档中插入图片的位置Object isLinkToFile = false; // 定义该插入的图片是否为外部链接默认,这里貌似设置为bool类型更清晰一些  Object isSaveWithDcm = true; //定义要插入的图片是否随Word文档一起保存//插入照片,注意InlineShapes对应不应调用Docement的,否则会添加到正文中。wordApp.Selection.InlineShapes.AddPicture(logoFile,isLinkToFile,isSaveWithDcm,_Range);wordApp.Selection.HeaderFooter.Range.Paragraphs.Format.Borders.Enable = 0;      //无边框wordApp.Selection.HeaderFooter.Range.Paragraphs.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;  //段落左对其//获取照片对象,注意InlineShapes对应不应调用Docement的,否则会读取错误。//处理图片时需要单独获取图片后再进行编辑。Word.Shape logo = wordApp.Selection.HeaderFooter.Range.InlineShapes[1].ConvertToShape();  logo.WrapFormat.Type = Word.WdWrapType.wdWrapNone;      //衬于文字下方logo.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoCTrue;  //锁定长宽比logo.Height = 40;   //照片缩小,固定高度为40磅logo.Select();logo.Top = -30;logo.Left = 0;wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;   //编辑主文档

C# 通过调用Microsoft Office编辑Word文件相关推荐

  1. 通过C#Microsoft.Office.Interop.Word理解互操作性

    目录 一.概念 二.应用 三.Word操作示例 四.总结 五.拓展 一.概念 C# 中的互操作性指的是C#托管代码和非托管代码之间实现互操作性的方法.(参考微软文档:互操作性概述) 参考:百度百科-- ...

  2. C#,使用office组件Microsoft.Office.Interop.Word,将网页内容下载为word的demo及权限配置要点。

    在做网页开发的时候,经常会遇到下载需求,以下这种方式是保存为原生的word格式.非直接将HTML另存为. 1. 首先是下载为word 的代码. 引用: using MSWord = Microsoft ...

  3. 无法将为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型

    文章目录 1.错误原因 2. 解决方式 2.1 office多版本 2.1.1 删除多余版本 2.1.2 修复当前版本 2.1.3 其他操作(可选1) 2.1.4 其他操作(可选2) 2.2 wps卸 ...

  4. C# 关于Microsoft.Office.Interop.Word中app.Documents.Add/Open报错

    最近做Winform程序中需要实现按照模板输出word并显示在WebBrowser中. 在调用app.Documents.Open()时出现以下错误: System.Runtime.InteropSe ...

  5. C#制作Office插件时引用Microsoft.Office.Interop.Word后获取运行实例

    自动化 Office 的客户端程序既可以创建 Office 程序的新实例,也可以获取对已在运行的实例的引用. Microsoft 通常建议您创建一个新实例,而不是附加到正在运行的实例. 但是,在某些情 ...

  6. Microsoft office interop word下打印机的运用

    今天使用.net做一个票据打印模块时,学习几点,分享一下. 第一点,在.net下使用基于word模板生出新的word文档 using System; using System.Collections. ...

  7. 错误类型:“系统找不到 Microsoft.Office.Interop.Word(转)

    错误类型:"系统找不到 Microsoft.Office.Interop.Word" 编译出现错误: "Could not load file or assembly ' ...

  8. 关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...

    控制word表格单元格内部文字样式.我要将数据导出到word当中,对于word表格一个单元格中的一段文字,要设置不同的样式,比如第一行文字作为标题要居中,加粗,第二行为正常的正文. 代码如下 publ ...

  9. 把Microsoft Office Excel/Word遇到问题

    现在把Microsoft Office  Excel/Word遇到问题需要关闭的解决方法分享给大家: 1.找到这个模板文件所在位置C:\Documents and Settings\[Username ...

  10. html与word字体大小转换,字体大小不一样,当html转换为Word使用Microsoft.Office.Interop.Word...

    我试图将html文件转换为MS word(docx)文件. 使用Microsoft.Office.Interop.Word我能够从HTML转换为Word,但字体大小不一样的文件的HTML和Word格式 ...

最新文章

  1. 遍历二叉树的神级方法(Morris遍历)
  2. 2015年去哪儿网Qunar面试题
  3. 只知道java有反射可以说是动态语言,动态链接,早期晚期绑定、虚方法这些概念你知道吗
  4. 信息化教学与计算机的关联性,信息化教学方法应用探索——以计算机基础课程为例.pdf...
  5. 利用虚拟硬盘(把内存当作硬盘)来提高数据库的效率(目前只针对SQL Server 2000)可以提高很多...
  6. 输出一串小于100的随机数(rand())
  7. 太赫兹芯片是什么原理_太赫兹微芯片速度可提升100倍 却遭遇研发两大挑战
  8. 微星主板黑苹果_记一次黑苹果PC装机全过程
  9. html鼠标移动图片自动放大,当鼠标移动到图片上时跟随鼠标显示放大的图片效果...
  10. 通过Cadence学拉扎维的第0天-工艺库的导入+共源电路仿真示例
  11. 【转】框架(蔡学镛)
  12. 计算机启动黑屏一段时间,电脑开机为什么黑屏一段时间就好了
  13. MIPI 系列之 DSI
  14. php 中大括号,PHP中大括号#39;{}#39;用法实例总结
  15. Java小游戏练习---超级玛丽代码实现
  16. 【CS229笔记】协方差矩阵概念快速回顾
  17. eagleeye_EagleEye简介:户外视频监控分析和面部识别软件
  18. python-基础语法-glob.glob()
  19. java电商商品搜索_Java生鲜电商平台-搜索模块的设计与架构
  20. mac播客播放器:Moon FM for Mac

热门文章

  1. 有道词典(android)v4.3.3去广告清爽版+离线词,有道词典4.3.3安
  2. Zeppelin0.8.1上操作hive(使用jdbc解释器)
  3. 软件调试方法及调试原则
  4. 路由器下一跳地址怎么判断_Tracert命令详解,路由跟踪命令tracert命令怎么用?...
  5. python运动目标检测_运动目标检测(3)—光流法
  6. 【微信小程序跳一跳】尝试用github开源机器学习玩跳一跳小游戏步骤
  7. 韩国服务器性能排行榜,2019韩国云服务器排名
  8. 基于opencv的重叠图像的凹点分割(C++)
  9. 【存储】存储基本概念(lun,volume,HBA,DAS,NAS,SAN,iSCSI,IPSAN、存储池和存储卷)
  10. 论文记录-2018-A survey on image tampering and its detection in real-world photos