C#实现简单合并word文档的方法

2019-12-26 13:20:33王旭

易采站长站为您分析C#实现简单合并word文档的方法,涉及C#针对word文档的读取、插入、保存等技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#实现简单合并word文档的方法。。具体如下:

 

 
  1. using System;  using System.Collections.Generic; 
  2. using System.ComponentModel;  using System.Data; 
  3. using System.Drawing;  using System.Text; 
  4. using System.Windows.Forms;  using System.Reflection; 
  5. namespace Demo  { 
  6. public partial class Form2 : Form  { 
  7. public Form2()  { 
  8. InitializeComponent();  } 
  9. string path = @"C:Documents and SettingsAdministrator桌面output.doc";  string add = @"C:Documents and SettingsAdministrator桌面file"; 
  10. private Microsoft.Office.Interop.Word.ApplicationClass applicationClass;  private Microsoft.Office.Interop.Word.Document doc; 
  11. private void button1_Click(object sender, EventArgs e)  { 
  12. Ex();  } 
  13. void Ex()  { 
  14. Open(path);  string[] files = System.IO.Directory.GetFiles(add); 
  15. foreach (string s in files)  { 
  16. InsertFile(s);  } 
  17. SaveAs(path);  } 
  18. /// <summary>  /// 打开输出word文档 
  19. /// </summary>  /// <param name="strFileName"></param> 
  20. public void Open(string strFileName)  { 
  21. applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();  object fileName = strFileName; 
  22. object readOnly = false;  object isVisible = true; 
  23. object missing = System.Reflection.Missing.Value;  doc = applicationClass.Documents.Open(ref fileName, ref missing, ref missing, 
  24. ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);