当前位置: 首页 > news >正文

C#实现WORD文档的内容复制和替换

最近一个项目的需求是要根据一个Word文档的模板,用记录集的具体内容替换掉里面的标识字符的内容,生成不同的文档。
分两步:
第一:复制模板的内容到一个Document对象里
从源DOC文档复制内容返回一个Document类 #region从源DOC文档复制内容返回一个Document类
/// <summary>
///从源DOC文档复制内容返回一个Document类
/// </summary>
/// <param name="sorceDocPath">源DOC文档路径</param>
/// <returns>Document</returns>
protectedDocument copyWordDoc( objectsorceDocPath)
{
objectobjDocType = WdDocumentType.wdTypeDocument;
objecttype = WdBreakType.wdSectionBreakContinuous;

//Word应用程序变量
Application wordApp;
//Word文档变量
Document newWordDoc;

objectreadOnly = false;
objectisVisible = false;

//初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
wordApp = newApplicationClass();

Object Nothing = System.Reflection.Missing.Value;

//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add( refNothing, refNothing, refNothing, refNothing);

Document openWord;
openWord = wordApp.Documents.Open( refsorceDocPath, refNothing, refreadOnly, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refisVisible, refNothing, refNothing, refNothing, refNothing);
openWord.Select();
openWord.Sections[1].Range.Copy();

objectstart = 0;
Range newRang = newWordDoc.Range( refstart, refstart);

//插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
openWord.Close( refNothing, refNothing, refNothing);
returnnewWordDoc;
}
#endregion
第二:替换复制好内容的Document的标识字符
替换指定Document的内容,并保存到指定的路径 #region替换指定Document的内容,并保存到指定的路径
/// <summary>
///替换指定Document的内容,并保存到指定的路径
/// </summary>
/// <param name="docObject">Document</param>
/// <param name="savePath">保存到指定的路径</param>
protected voidReplaceWordDocAndSave(Document docObject, objectsavePath)
{
objectformat = WdSaveFormat.wdFormatDocument;
objectreadOnly = false;
objectisVisible = false;

stringstrOldText = "{WORD}";
stringstrNewText = "{替换后的文本}";

List< string> IListOldStr = newList< string>();
IListOldStr.Add( "{WORD1}");
IListOldStr.Add( "{WORD2}");

Object Nothing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application wordApp = newApplicationClass();
//Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Microsoft.Office.Interop.Word.Document oDoc = docObject;

objectFindText, ReplaceWith, Replace;
objectMissingValue = Type.Missing;

foreach( stringstr inIListOldStr)
{

oDoc.Content.Find.Text = str;
//要查找的文本
FindText = str;
//替换文本
ReplaceWith = strNewText;

//wdReplaceAll - 替换找到的所有项。
//wdReplaceNone - 不替换找到的任何项。
//wdReplaceOne - 替换找到的第一项。
Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

//移除Find的搜索文本和段落格式设置
oDoc.Content.Find.ClearFormatting();

if(oDoc.Content.Find.Execute( refFindText, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refMissingValue, refReplaceWith, refReplace, refMissingValue, refMissingValue, refMissingValue, refMissingValue))
{
Response.Write( "替换成功!");
Response.Write( "<br>");
}
else
{
Response.Write( "没有相关要替换的:("+ str + ")字符");
Response.Write( "<br>");
}
}

oDoc.SaveAs( refsavePath, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing, refNothing);

//关闭wordDoc文档对象
oDoc.Close( refNothing, refNothing, refNothing);
//关闭wordApp组件对象
wordApp.Quit( refNothing, refNothing, refNothing);
}
#endregion
用到了C#操作WORD的复制,替换,和创建WORD文档的知识。

本文出自 “摇滚互联网” 博客,请务必保留此出处http://tobetobe.blog.51cto.com/1392243/354420

相关文章:

  • Java与C#开发上的一些差异与转换方法
  • 浅谈我心中的软件开发
  • 十三款著名跨平台游戏引擎(原文翻译部分转自游侠网)
  • 发个XNA中文字体生成的辅助小工具
  • 教你如何迅速秒杀99%的海量数据处理面试题
  • Android开发性能提升
  • Java游戏源码大礼包(清明有爱强化版)
  • 浅谈STG游戏的开发(4月8日更新,已补全内容)
  • J2me游戏引擎的基本构成
  • 感知世界触景生情——增强现实技术
  • Android游戏源码合集(主要是AndEngine和Libgdx的)
  • 继续群发Android游戏源码(再发15款)
  • (NSDate) 时间 (time )比较
  • CSDN的博客系统比以前快多了
  • Android之AndroidManifest.xml文件解析
  • @jsonView过滤属性
  • es6--symbol
  • happypack两次报错的问题
  • Idea+maven+scala构建包并在spark on yarn 运行
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 初探 Vue 生命周期和钩子函数
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 讲清楚之javascript作用域
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 使用 Docker 部署 Spring Boot项目
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 主流的CSS水平和垂直居中技术大全
  • ionic异常记录
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​批处理文件中的errorlevel用法
  • #WEB前端(HTML属性)
  • (办公)springboot配置aop处理请求.
  • (三)docker:Dockerfile构建容器运行jar包
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转载)OpenStack Hacker养成指南
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .Net(C#)自定义WinForm控件之小结篇
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • /boot 内存空间不够
  • /etc/fstab和/etc/mtab的区别
  • /proc/vmstat 详解
  • /var/lib/dpkg/lock 锁定问题
  • @ConditionalOnProperty注解使用说明
  • @Conditional注解详解
  • @SpringBootApplication 包含的三个注解及其含义
  • [ CTF ] WriteUp-2022年春秋杯网络安全联赛-冬季赛
  • [20180129]bash显示path环境变量.txt
  • [2019.3.20]BZOJ4573 [Zjoi2016]大森林
  • [ACM独立出版]2024年虚拟现实、图像和信号处理国际学术会议(ICVISP 2024)
  • [AIGC] Spring Interceptor 拦截器详解
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [Angularjs]ng-select和ng-options