【C#】C#使用Microsoft.Office.Interop.Word操作Word文档,向表格插入图片
上篇:【C#】C#使用Microsoft.Office.Interop.Word操作Word文档,替换文本
Word模板:
代码:
try
{Application app = new Application();string templetFileName = @"E:\BT\Test3.docx";string newFileName = $@"E:\BT\Test{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";//File.Copy(templetFileName, newFileName);object oMissing = System.Reflection.Missing.Value;object replace = WdReplace.wdReplaceAll;//object objNewFileName = newFileName;//Document doc = app.Documents.Open(ref objNewFileName,// 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);Document doc = app.Documents.Open(templetFileName);Dictionary<string, string> dicDatas = new Dictionary<string, string>(){{ "{姓名}","Bob" },{ "{年龄}","18" },{ "{身份}","学生" }};foreach (var item in dicDatas){app.Selection.Find.ClearFormatting();app.Selection.Find.Replacement.ClearFormatting();app.Selection.Find.Text = item.Key;app.Selection.Find.Replacement.Text = item.Value;app.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref oMissing, ref oMissing,ref oMissing, ref oMissing, ref replace, ref oMissing,ref oMissing, ref oMissing, ref oMissing);}//获取表格,索引从1开始Table table = doc.Tables[1];object Range = table.Cell(4, 1).Range;//居中显示table.Cell(4, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;string picFileName = @"E:\BT\pic.png";object LinkToFile = false;object SaveWithDocument = true;doc.Application.ActiveDocument.InlineShapes.AddPicture(picFileName, ref LinkToFile, ref SaveWithDocument, ref Range);doc.Application.ActiveDocument.InlineShapes[1].Width = 300;//图片宽度doc.Application.ActiveDocument.InlineShapes[1].Height = 180;//图片高度//doc.Save();doc.SaveAs2(newFileName);//doc.Close(ref oMissing, ref oMissing, ref oMissing);//app.Quit(ref oMissing, ref oMissing, ref oMissing);doc.Close();app.Quit();
}
catch (Exception ex)
{Console.WriteLine(ex.ToString());
}Console.WriteLine("Success");
Console.ReadKey();
效果:
参考:
https://www.cnblogs.com/superfeeling/p/12302335.html
【C#】C#使用Microsoft.Office.Interop.Word操作Word文档,向表格插入图片相关推荐
- 在word文档表中插入图片不变形
在word文档表中插入图片不变形 目录 在word文档表中插入图片不变形 1.点击左上角[全选图标 ]选中表格,鼠标右键点击[表格属性] 2.点击[选项]点击 取消勾选[自动重调尺寸以适应内容],最后 ...
- spring使用freemarker生成word文档包含表格、图片(循环插入)
spring使用freemarker生成word文档包含表格.图片(循环插入) 效果图 因为测试数据是重复的,所以显示都是重复的数据,替换导入map中的数据可以显示不重复的数据. 操作步骤 1,创建一 ...
- 用freemarker生成word文档,并插入图片
用freemarker生成word文档,并插入图片 最近需要做一个问卷功能,要求用户填写完问卷后,后台会生成一个word文档,将用户提交的数据插入到word中. 创建word模板 新建一个word文档 ...
- 【PDFBox】PDFBox操作PDF文档之添加本地图片、添加网络图片、图片宽高自适应、图片水平垂直居中对齐
这篇文章,主要介绍PDFBox操作PDF文档之添加本地图片.添加网络图片.图片宽高自适应.图片水平垂直居中对齐. 目录 一.PDFBox操作图片 1.1.添加本地图片 (1)案例代码 (2)运行效果 ...
- 【python自动化办公01】word操作-新建文档
点击上方"AI搞事情"关注我们 Python操作Word(Win32com)https://zhuanlan.zhihu.com/p/67543981 Office VBA 参考h ...
- 使用poi根据模版生成word文档,支持插入图片,复制表格,插入、循环插入数据,继承模板大部分格式及样式(优化版)
一.制作word模版,${xxxx}是一会要替换的内容,最下面的表格是要插入数据,根据是否以$开头来判断是需要替换还是插入数据, 注意如果是需要循环插入数据,制作的表格模版需要一行全部输入井号#,格式 ...
- C# 用Microsoft.Office.Interop.PowerPoint类库操作PPT
前言:最近由于项目需求,需要使用此类库对PPT进行操作 1.引用 Microsoft.Office.Interop.PowerPoint和 Microsoft.Office.Core 2.PPT操作 ...
- C#制作Office插件时引用Microsoft.Office.Interop.Word后获取运行实例
自动化 Office 的客户端程序既可以创建 Office 程序的新实例,也可以获取对已在运行的实例的引用. Microsoft 通常建议您创建一个新实例,而不是附加到正在运行的实例. 但是,在某些情 ...
- 使用Microsoft.Office.Interop.PowerPoin遥控PPT
Microsoft.Office.Interop.PowerPoin操作PPT 主窗体,填写ppt路径,打开ppt 打开ppt后,可用代码操作ppt 可获取每页PPT截图,并获取对应小节名称,备注等 ...
最新文章
- I/0口输入输出实验 将P1口的某一位作为输入使用,连接一个按键,当按键按下时使发光二极管亮,否则发光二极管熄灭
- (30)System Verilog设计SPI发送
- Bitmap详解(上)常用概念和常用API
- crm客户管理系统源码_公司crm客户关系管理系统的功能
- msyql之查询某字段值重复的数据个数
- Hbase-1.1.1-java API
- 全球最厉害的14位程序员!你知道有哪几位?
- 【第一部分】04Leetcode刷题
- 安卓自动化测试(2)Robotium环境搭建与新手入门教程
- uploader java_java-webuploader+Java如何实现分片+断点续传
- WEB标准的学习和理解(一)何为WEB标准
- koa利用koa-views通过路由返回html页面
- nodejs 读取写入 plist 文件,使用 plist npm 包
- 程序员一些有意思的智力题整理
- 功不唐捐 玉汝于成---贪心算法终结
- 电子取证volatility
- 为什么我没圣诞帽!?微信一键生成圣诞帽方法
- 20-50人,拓展基地_拓展训练_拓展基地_拓展公司推荐_嗨牛团建
- ug java环境变量设置_关于UG环境变量
- Android入门视频教程,抖音音视频开发