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字符串的功能.但是无法 ...
随机推荐
- Apache虚拟站点配置
简单虚拟站点配置: <VirtualHost 127.0.0.2:80> DocumentRoot E:/wamp/www/yue ServerName 127.0.0.2:80</ ...
- 开源java
http://code.google.com/p/nullpomino/source/checkout http://www.open-open.com/open254058.htm http://w ...
- RR 插入不影响
| test100 | CREATE TABLE `test100` ( `sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号', `phoneNo` ...
- 分页SQL取下一页
20条记录一页,扫描第2页就需要访问40条记录. SQL> select * from ( select * from ( select /*+ index_desc(a idx_page_3) ...
- swiper 多个循环的实现
swiper 最好要一一对应,最好与id关联. new Swiper('#guess .swiper-container', { pagination: '#guess .swiper-paginat ...
- 动态规划(背包问题):POJ 1742 Coins
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32955 Accepted: 11199 Descripti ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- HDU1247 Hat’s Words(Trie的动态内存版本)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- poj1042
题目大意:去捕鱼 约翰去参加一个垂钓旅行,他有h小时可以使用在该地区有n (2 <= n <= 25) 个湖泊可以沿着一个单一的路到达,约翰从湖泊1开始,但是它可以在任何湖泊结束他如果想, ...
- UIView和CALayer有什么关系
view是对layer的一种封装,你对view的很多操作事实上是对layer的操作,之所以会出现这两个东西是因为1.view支持很多手势的交互,你所操作iphone的各种点击,拖动等等.2.layer ...