Word复制和替换实例
public string Path
{
get
{
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
return info.Parent.Parent.FullName + "\\WordData\\Table.docx";
}
}
//【C# 在word文档中复制表格并粘帖到下一页中】
private void button1_Click(object sender, EventArgs e)
{
object missing = Missing.Value;
Word.Application app = new Word.Application();
app.Visible = true;
//导入模板
object filename = Path;
Word.Document doc = app.Documents.Add(ref filename, missing, missing, missing); //复制第一个表格
doc.Tables[].Select();
app.Selection.Copy();
//在这里操作表格的文本
Word.Cell cellOne = doc.Tables[].Cell(, );
cellOne.Range.Text = "这是第一个表格";
cellOne.Range.Bold = ;
cellOne.Range.Font.ColorIndex = Word.WdColorIndex.wdRed; //下一页
object myunit = Word.WdUnits.wdStory;
app.Selection.EndKey(ref myunit, ref missing);
object pBreak = (int)Word.WdBreakType.wdPageBreak;
app.Selection.InsertBreak(ref pBreak); //粘贴第一个表格
app.Selection.Paste(); //操作第二个表格单元格
Word.Cell cellTwo = doc.Tables[].Cell(, );
cellTwo.Range.Text = "这是第二个表格";
cellTwo.Range.Underline = Word.WdUnderline.wdUnderlineDash;
} //【C#实现WORD文档的内容复制和替换】
private void button2_Click(object sender, EventArgs e)
{
LocalPathHelper pathHelper = new LocalPathHelper();
string sourceWord = WordPath.GetWordDataFullFileName("copy.docx");
string targetWord = pathHelper.DesktopPath() + "\\target.docx";
//复制文件
Word.Document doc = copyWord(sourceWord);
//查找替换 ReplaceAndSave(doc, targetWord);
} //复制word内容到Document对象
public Word.Document copyWord(object sourcePath)
{
object objDocType = Word.WdDocumentType.wdTypeDocument;
object type = Word.WdBreakType.wdSectionBreakContinuous;
object missing = Missing.Value; Word.Application app = new Word.Application();
Word.Document doc; object readOnly = false;
object isVisible = false; doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Word.Document opendWord = app.Documents.Open(ref sourcePath, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref isVisible, ref missing, ref missing, ref missing, ref missing);
opendWord.Select();
opendWord.Sections[].Range.Copy(); object start = ;
Word.Range newRange = doc.Range(ref start, ref start); //插入换行符
//newWordDoc.Sections[1].Range.InsertBreak(ref type);
doc.Sections[].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
opendWord.Close(ref missing, ref missing, ref missing);
return doc;
} //替换复制好的内容
public void ReplaceAndSave(Word.Document doc, object savePath)
{
object format = Word.WdSaveFormat.wdFormatDocument;
object missing = Missing.Value;
object readOnly = false;
object isVisible = false; string strOldText = "{Word}";
string strNewText = "{提花后的文本}";
List<string> listStr = new List<string>();
listStr.Add("{Word1}");
listStr.Add("{Word2}"); Word.Application app = new Word.Application();
//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);
Word.Document newDoc = doc; object FindText, ReplaceText, ReplaceAll;
foreach (string str in listStr)
{
newDoc.Content.Find.Text = str;
//要找的文本
FindText = str;
//替换文本
ReplaceText = strNewText;
//wdReplaceAll - 替换找到的所有项。
//wdReplaceNone - 不替换找到的任何项。
//wdReplaceOne - 替换找到的第一项。
ReplaceAll = Word.WdReplace.wdReplaceAll;
//移除Find的搜索文本和段落格式设置
newDoc.Content.Find.ClearFormatting(); if (newDoc.Content.Find.Execute(ref FindText, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref ReplaceText, ref ReplaceAll, ref missing, ref missing, ref missing, ref missing))
{
MessageBox.Show("替换成功");
}
else
{
MessageBox.Show("没有相关替换的:'" + str + "'字符");
}
} newDoc.SaveAs2(ref savePath);
//关闭文档对象,关闭组件对象
newDoc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);
}
Word复制和替换实例的更多相关文章
- [No000051]如何去掉word复制过来的文字背景色?
我们经常从网上copy一些自己需要的材料到word里.不过常常会把别人的背景色一起拷贝过来.那么如何去掉word复制过来的文字背景色? 方法/步骤 第一步选ctrl+A(全选),找到页面布局→页面边框 ...
- ZeroclipboardJS+flash实现将内容复制到剪贴板实例
Zeroclipboard 的实现原理 Zeroclipboard 利用 Flash 进行复制,之前有 Clipboard Copy 解决方案,其利用的是一个隐藏的 Flash.但最新的 Flash ...
- 烂泥:【解决】word复制windows live writer没有图片
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 在使用windows live writer发表博客,博客先是在是word2013中进行编辑,编辑完毕后我会复制到windows live writer ...
- Word中批量替换软回车
在平时工作中,有时候需要拷贝一些截取自网页上的文字,当选中后拷贝到Word中时,有时候在每行的结尾出现如下的符号,,这给后期文字的整理带来了很多不便,在此记录从网上获取的解决方法,以免遗忘和便于查找. ...
- jquery插入,复制、替换和删除节点
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Python正则表达式如何进行字符串替换实例
Python正则表达式如何进行字符串替换实例 Python正则表达式在使用中会经常应用到字符串替换的代码.有很多人都不知道如何解决这个问题,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获. ...
- Python之word文档替换字符串(也可以用于短模板套用)
Python之word文档替换字符串(也可以用于短模板套用),代码如下: 1 ''' 2 #word模板套用1:创建模板,适合比较短的文档 3 ''' 4 5 #导入所需库 6 from docx i ...
- c#调用word com组件 替换书签套打
安装office2007,添加com引用Microsoft Word12.0 Object Library和Microsoft Office12.0 Object Library using Syst ...
- 富文本框编辑器实现:a、支持图片复制粘贴;b、支持word复制粘贴图文。
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
随机推荐
- [BZOJ 1188] [HNOI2007] 分裂游戏 【博弈论|SG函数】
题目链接:BZOJ - 1188 题目分析 我们把每一颗石子看做一个单个的游戏,它的 SG 值取决于它的位置. 对于一颗在 i 位置的石子,根据游戏规则,它的后继状态就是枚举符合条件的 j, k.然后 ...
- STM32F10x 学习笔记5(USART实现串口通讯 1)
STM32F10x 系列单片机中都包含了USART 模块,所谓USART,就是通用同步异步收发器.通用同步异步收发器(USART)提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间 ...
- Json传递后台数据的问题
在后台我有两个类: public Class Person { private String name; private Address address;//一个自定义的类 //getter和sett ...
- Maven实战四
转载:http://www.iteye.com/topic/1123231 为什么要定义Maven坐标 在我们开发Maven项目的时候,需要为其定义适当的坐标,这是Maven强制要求的.在这 ...
- 根据标点符号分行,StringBuilder的使用;将字符串的每个字符颠倒输出,Reverse的使用
一:根据标点符号分行,上图,代码很简单 二:代码 using System; using System.Collections.Generic; using System.ComponentModel ...
- iOS 用CocoaPods做iOS程序的依赖管理
文档更新说明 2012-12-02 v1.0 初稿 2014-01-08 v1.1 增加设置 ruby 淘宝源相关内容 2014-05-25 v2.0 增加国内 spec 镜像.使用私有 pod.po ...
- bzoj 3876 [Ahoi2014]支线剧情(有上下界的最小费用流)
3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 484 Solved: 296[Submit][Status ...
- MySQL客户端执行外部sql文件命令
客户端 source d:\bbs.sql 或者 \. d:\bbs.sql
- 跑步进入全站 HTTPS ,这些经验值得你看看
随着国内网络环境的持续恶化,各种篡改和劫持层出不穷,越来越多的网站选择了全站 HTTPS.就在前几天,免费提供证书服务的 Let’s Encrypt 项目也正式开放测试,HTTPS 很快就会成为 WE ...
- 函数调用和给对象发消息(Runtime理解)
在写代码的时候这个差距其实是不打看的出得,很多时候也就无所谓叫什么,很多人为了便于理解,干脆就叫函数调用.这个其实应该是oc的一个特色,消息发送.具体的类typedef struct objc_cla ...