利用占位符替换word中的字符串和添加图片
///<summary>
/// 替换word模板文件内容,包括表格中内容
/// 调用如下:WordStringsReplace("D:/CNSI/CNSI_1.doc", new ArrayList() { "old1", "old2" }, new ArrayList() { "new1", "new2" });
/// </summary>
/// <param name="filePath">文件全路径</param>
/// <param name="arr_Old">占位符数组</param>
/// <param name="arr_New">替换字符串数组</param>
public void WordStringsReplace(string filePath, ArrayList arr_Old, ArrayList arr_New)
{
if (!File.Exists(filePath))
{
MessageBox.Show("模板文件不存在!");
return;
}
if (arr_Old.Count != arr_New.Count)
{
MessageBox.Show("占位符和替换内容不一致!");
return;
}
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
object file = filePath;
Microsoft.Office.Interop.Word._Document doc = app.Documents.Open(ref file,
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);
for (int i = 0; i < arr_Old.Count; i++)
{
app.Selection.Find.ClearFormatting();
app.Selection.Find.Replacement.ClearFormatting();
app.Selection.Find.Text = arr_Old[i].ToString();
app.Selection.Find.Replacement.Text = arr_New[i].ToString();
object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
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 objReplace, ref oMissing,
ref oMissing, ref oMissing, ref oMissing);
}
//保存
doc.Save();
doc.Close(ref oMissing, ref oMissing, ref oMissing);
app.Quit(ref oMissing, ref oMissing, ref oMissing);
}
/// <summary>
/// 替换word模板文件中图片,这个只能替换一个图片,多个测试有点问题
/// </summary>
/// <param name="filePath">文件全路径</param>
/// <param name="str_Old">占位符字符串</param>
/// <param name="str_Pics">替换图片路径</param>
/// <param name="x">x点位置,(0,0)在该占位符所在行和列的原点</param>
/// <param name="y">y点位置</param>
/// <param name="width">图片宽度</param>
/// <param name="height">图片高度</param>
public void WordStringsReplace(string filePath, String str_Old, String str_Pics, int x, int y, int w, int h)
{
if (!File.Exists(filePath))
{
MessageBox.Show("模板文件不存在!");
return;
}
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
object file = filePath;
Microsoft.Office.Interop.Word._Document doc = app.Documents.Open(ref file,
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);
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object LinkToFile = false;
object SaveWithDocument = true;
app.Selection.Find.ClearFormatting();
app.Selection.Find.Text = str_Old;
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 oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object Anchor = app.Selection.Range;
//oDoc.InlineShapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
object left = x;
object top = y;
object width = w;
object height = h;
Anchor = app.Selection.Range;
doc.Shapes.AddPicture(str_Pics, ref LinkToFile, ref SaveWithDocument, ref left, ref top, ref width, ref height, ref Anchor);
//保存
doc.Save();
doc.Close(ref oMissing, ref oMissing, ref oMissing);
app.Quit(ref oMissing, ref oMissing, ref oMissing);
//清除占位符
WordStringsReplace("D:/CNSI/CNSI_1.doc", new ArrayList() { str_Old }, new ArrayList() { " " });
}
- C# 替换Word文本—— 用文档、图片、表格替换文本
编辑文档时,对一些需要修改的字符或段落可以通过查找替换的方式,快速地更改.在C# 在word中查找及替换文本一文中,主要介绍了在Word中以文本替换文本的方法,在本篇文章中,将介绍如何用一篇Word文 ...
- word模板导出的几种方式:第一种:占位符替换模板导出(只适用于word中含有表格形式的)
1.占位符替换模板导出(只适用于word中含有表格形式的): /// <summary> /// 使用替换模板进行到处word文件 /// </summary> public ...
- 关于.net导出数据到excel/word【占位符替换】
1]excel的占位符替换 效果如图 关键代码: ///savedFilePath需要保存的路径 templateDocPath模板路径 替换的关键字和值 格式 [姓名]$%$小王 public st ...
- 【占位符替换】替换String中的占位符标志位{placeholder}
概述 占位符替换, 占位符表示为:{placeholder}; 示例:替换如下{xxx}占位符中的内容 "名字:{name},年龄:{age},学校:{school}" 提供了两种 ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- Java占位符替换工具类
import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFact ...
- js使用占位符替换字符串
js使用占位符替换字符串是一个ES6中的模版字符串语法. 在``中使用 ${} var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and ...
- 使用POI替换word中的特定字符/文字改进版
package com.xfzx.test.POI.main; import java.io.File; import java.io.FileInputStream; import java.io. ...
- Java{0}占位符替换字符串
Java{0}占位符替换字符串 public class Test { public static void main(String[] args) { System.out.println(Stri ...
随机推荐
- Cenots7编译Opencv3.1错误:下载ippicv,解决方案
在centos7上安装Opencv的时候,停在了下载ippicv的地方,一直都下载不下来. ippicv是一个并行计算库,其实可以不用的. 如果不想用这个并行计算库,在做Cmake的时候用参数关闭即可 ...
- Bootstrap Modal 垂直方向加滚动条
原文链接:http://blog.csdn.net/cyh1111/article/details/52960747 背景 使用Bootstrap Modal实现用户资料修改,由于用户信息过多,默认M ...
- Android混淆打包
一.理论知识 ProGuard是一款免费的Java类文件压缩器.优化器和混淆器.它能发现并删除无用类.字段(field).方法和属性值(attribute).它也能优化字节码并删除无用的指令.最后 ...
- sqlalchemy中文乱码问题解决方案
本文参考http://firefish.blog.51cto.com/298258/112794/的解决方案 问题: 本文在Ubuntu上利用scrapy抓取数据写入mysql数据库时,用到sqlal ...
- yum安装nginx
1.在/etc/yum.repos.d/目录下创建一个源配置文件ngxin.repo: cd /etc/yum.repos.d/ vim nginx.repo 填写如下内容: [nginx] name ...
- 如何使用JDBC实现数据访问对象层(DAO)
JAVA是面向对象的语言,开发者在操作数据的时候,通常更习惯面对一个特定类型的对象,如一个用户就是一个User类的对象.DAO层需要做的,就是为上层提供充分的对象支持,让上层再也看不到具体的数据,而是 ...
- Swift_1基础
// swift中导入类库使用import,不再使用<>和""import Foundation // 输出print("Hello, World!" ...
- My first python script for work
I write it yesterday to watch the NE process(rcpfd,cfgd) automatically, then i will write a window t ...
- 配置log4j
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 《C++ Primer》学习笔记【第三部分 类设计者的工具】
第13章 拷贝控制 使用default:=defult只能修饰默认构造函数或拷贝控制成员,显式地要去编译器生成合成的版本. 使用delete:=delete通知编译器不希望定义这些成员,禁止试图使用它 ...