这个是项目中用到一个测试demo,写完以后运行了下,感觉还可以,丢上来了,因为office2003 2007的问题头疼半天,用反射搞定的
= =#,不过这玩意用起来忒麻烦呀忒麻烦,都是微软变态,office兼容性一点都不好
- private void button2_Click(object sender, EventArgs e)
- {
- //System.Data.DataRowView dr = (System.Data.DataRowView)this.comboBox1.SelectedItem;
- //this.button2.Text = dr.Row.ItemArray[0].ToString();
- return;
- string x = @"d:/1.rtf";
- string y = @"d:/2.rtf";
- //开启一个word appliction对象
- object wordapp;
- Type wordType = Type.GetTypeFromProgID("Word.Application");
- wordapp = Activator.CreateInstance(wordType);
- funMergeRtf(x, y, wordapp);
- wordapp.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, wordapp, null);
- }
- /// <summary>
- /// 将第二个参数中的文件内容放入第一个参数里面
- /// </summary>
- /// <param name="strSource">存放的文件路径</param>
- /// <param name="strTag">取内容的对象路径</param>
- /// <param name="wordapp">已经打开的wordapplication对象</param>
- private void funMergeRtf(string strSource, string strTag, object wordapp)
- {
- //由于项目中不引用word对象,同时为了使2003与2007的文档都能读取,而不因客户机的office版本影响程序操作word
- //使用发射进行调用
- //定义将要使用到的对象
- object docSource;//源文档对象
- object docTag;//目标文档对象
- object documents;//appliction的文档集合
- object range;//文本的范围对象
- object paragraph;//文档的段落对象集合
- object newparagraph;
- //object savechanges//保存对象的枚举参数
- object result;//结果
- Type typeWord = wordapp.GetType();//设定对象类型
- try
- {
- documents = typeWord.InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, System.Type.DefaultBinder, wordapp, null);//获取文档集合对象
- docSource = documents.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, documents, new object[] { strSource });//打开指定源文件并且获取对象
- //docSource = typeDocuments.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, documents, new object[] { strSource });//打开指定源文件并且获取对象
- docTag = documents.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, documents, new object[] { strTag });//打开指定源文件并且获取对象
- range = docTag.GetType().InvokeMember("Content", System.Reflection.BindingFlags.GetProperty, System.Type.DefaultBinder, docTag, null);//获取目标rtf中的所有内容存放入range对象
- paragraph = docSource.GetType().InvokeMember("Paragraphs", System.Reflection.BindingFlags.GetProperty, System.Type.DefaultBinder, docSource, null);//获取源rtf中的所有段落集合对象等待插入新段落
- newparagraph = paragraph.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, paragraph, null);//新建一个段落
- paragraph.GetType().InvokeMember("Range", System.Reflection.BindingFlags.SetProperty, System.Type.DefaultBinder, newparagraph, new object[] { range });//将目标rtf中的内容放入源文档
- docTag.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, docTag, null);//关闭目标文档
- docSource.GetType().InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, docSource, new object[] { strSource });//在保存以后关闭源文档
- docSource.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder, docSource, null);//关闭目标文档
- }
- catch
- {
- }
- }