【C# 在word文档中复制表格并粘帖到下一页中】
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//显示word文档
oWord.Visible = true;
//取得word文件模板
object fileName = System.Windows.Forms.Application.StartupPath + "\\word.doc";
//根据模板生成一个新文档,相当于另存为
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
//复制第一个表格
oDoc.Tables[1].Select();
oWord.Selection.Copy();
//在这里操作表格中的文本
oDoc.Tables[1].Cell(1, 1).Range.Text = "这是第一个表格";
//下一页
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWord.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWord.Selection.InsertBreak(ref pBreak);
//粘贴第一个表格
oWord.Selection.Paste();
oDoc.Tables[2].Cell(1, 1).Range.Text = "这是第二个表格";
【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>protectedvoidReplaceWordDocAndSave(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文档的知识。
分享到:
相关推荐
C#操作Word——C#向Word文档中写入文字
C#操作Word.doc C#操作Word表格.doc C#操作WORD辅助类.txt c#操作word模板插入文字、图片及表格.doc C#操作Word在Word文档中插入图片.docx C#用word模板做文档.doc 收集的C#操作Word的各种代码。直接复制粘贴到程序...
一段C#操作word,如何进行查找和替换
C#操作word
将C#操作word书签模板的每一步都写的非常详细,读者完全可以按照步骤一步步实践,文中并附上代码。
2、如何使用C#在Word文档中插入图片以及读取Word文档中的图片?3、提取word 文档中的图片信息;4、提取WORD中的所有InlineShape图片并保存成文件;5、C#编程实现动态生成Word文档{1、2};6、C#读取Word文档的详细...
c#操作word详细类。实用c#操作word的一个通用类,功能全面。
c# 操作word,包含word文档的打开,修改,删除,添加文本等
C#操作word文档 C#实现Word中表格信息读取
C#操作Word文件(排版_调整
C#操作Word文档(根据模板生成文档、替换内容、插入表格等)
C#得到word里面的书签,并向书签插入内容,将DateGridView内容作为表格插入到word书签中。
非常好用的一个C#操作word的类库。能在word文档中任意位子写文字以及段落和表格(带线和不带线以及虚线)、图片、页眉页脚。word能操作的,这个类库基本都可以完成。下载直接使用。
C#操作word,签入书签,图表,表格
这是一个完整的通过C#操作Word文档的例子,通过替换的方式可以实现报告的编写,涉及到了文档的合并。
C# 操作word ,主要是用来修改,可以字符串的替换修改,也可以键值对的替换修改
操作word 较详细的方法集合 如创建word,打开word,表格的操作等等,是用c#编写的
C#操作Word文件(排版,调整格式)[文].pdf
C#操作word详解 基本覆盖了C#所有关于word操作 楼主潜心积累了很久才整理出来的,对于操作word的C#程序员绝对有用