黄聪:C#操作合并多个Word文档
2015-02-02 09:09
525 查看
黄聪:C#操作合并多个Word文档
首先将word文档路径保存在List<string>中,新建一个word文档对象并循环List打开对应的word,之后拷贝文档,在新文档中粘贴并且粘贴前先插入换行符。最后保存新文档,并销毁进程。
protected void Button1_Click(object sender,
EventArgs e)
{
// object worPath = @"C:\";
object path = @"C:\Test合并word.doc";
List<string> pathList = new List<string>();
pathList.Add(@"C:\word1.doc");
pathList.Add(@"C:\word2.doc");
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
Application wordApp; //Word应用程序变量
Document newWordDoc; //Word文档变量
object readOnly = false;
object isVisible = false;
wordApp = new ApplicationClass(); //初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
for (int i = 0; i < pathList.Count;
i++)
{
Document openWord;
object obj = pathList[i];
openWord = 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);
openWord.Select();
openWord.Sections[1].Range.Copy();
//string str= Clipboard.GetText(TextDataFormat.Text);
// str+="\n\n";
// Clipboard.SetData(DataFormats.Text, str);
object start = 0;
Range newRang = newWordDoc.Range(ref start, ref start);
newWordDoc.Sections[1].Range.InsertBreak(ref type);//插入换行符
newWordDoc.Sections[1].Range.PasteAndFormat(WdReco veryType.wdPasteDefault);
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
}
object format = WdSaveFormat.wdFormatDocument;//.wdFormatDocumentD
efault;
//将wordDoc文档对象的内容保存为DOCX文档
newWordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
newWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 创建完毕!");
Console.ReadLine();
}
首先将word文档路径保存在List<string>中,新建一个word文档对象并循环List打开对应的word,之后拷贝文档,在新文档中粘贴并且粘贴前先插入换行符。最后保存新文档,并销毁进程。
protected void Button1_Click(object sender,
EventArgs e)
{
// object worPath = @"C:\";
object path = @"C:\Test合并word.doc";
List<string> pathList = new List<string>();
pathList.Add(@"C:\word1.doc");
pathList.Add(@"C:\word2.doc");
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
Application wordApp; //Word应用程序变量
Document newWordDoc; //Word文档变量
object readOnly = false;
object isVisible = false;
wordApp = new ApplicationClass(); //初始化
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
Object Nothing = Missing.Value;
//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
for (int i = 0; i < pathList.Count;
i++)
{
Document openWord;
object obj = pathList[i];
openWord = 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);
openWord.Select();
openWord.Sections[1].Range.Copy();
//string str= Clipboard.GetText(TextDataFormat.Text);
// str+="\n\n";
// Clipboard.SetData(DataFormats.Text, str);
object start = 0;
Range newRang = newWordDoc.Range(ref start, ref start);
newWordDoc.Sections[1].Range.InsertBreak(ref type);//插入换行符
newWordDoc.Sections[1].Range.PasteAndFormat(WdReco veryType.wdPasteDefault);
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
}
object format = WdSaveFormat.wdFormatDocument;//.wdFormatDocumentD
efault;
//将wordDoc文档对象的内容保存为DOCX文档
newWordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档对象
newWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Console.WriteLine(path + " 创建完毕!");
Console.ReadLine();
}
相关文章推荐
- 黄聪:C#操作合并多个Word文档
- C#操作合并多个Word文档
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- C#操作Word文档(Office 2007)
- 用C#实现word文档合并
- C#操作Word文档(Office 2007)
- C#操作word文档(一)
- C# 操作Word文档(转)
- C#操作Word文档(Office 2007)
- C#操作word文档
- C#操作word文档(二)
- C# 操作word文档
- C# 操作word文档
- C#操作Word文档
- C#操作Word文档(Office 2007)
- C#操作Word文档(Office 2007)
- C#应用程序控制Word文档操作-基本
- c#操作word文档(转自小排_流浪狗)
- C#操作word文档