不引用word,excel的dll使用反射来在项目中进行vba操作
2008-08-22 14:44
561 查看
这个是项目中用到一个测试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
{
}
}
= =#,不过这玩意用起来忒麻烦呀忒麻烦,都是微软变态,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
{
}
}
相关文章推荐
- 解决使用MSBuild编译项目没有拷贝间接引用的dll问题
- 在服务器上使用第三方独立组件对Word/Excel进行编程
- 使用jacob对word.excel文件进行完美解析(缺点是不支持linux)
- 使用C#进行Word 2002和Excel 2002编程
- 对于项目只引用,但没有使用的情况下,dll版本不再更新的一点想法
- 在服务器上使用第三方独立组件对Word/Excel进行编程
- 解决使用MSBuild编译项目没有拷贝间接引用的dll问题
- 在JAVA项目中,使用JasperReports与iReport,直接连接打印机可打印(word excel xml pdf )文档
- Access 、Excel、Word中的VBA引用C#或VB.Net创建的DLL
- 通过lib对动态链接库DLL进行引用的项目,发布时DLL放置的位置
- 使用maven管理jacob.jar对excel、word文档进行转换为html细节描述
- .net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作
- 使用C#进行Word 2002和Excel 2002编程
- 使用C#进行Word 2002和Excel 2002编程
- 通过lib对动态链接库DLL进行引用的项目,发布时DLL放置的位置
- 在一个引入了dll的C#项目中,怎样进行数组引用啊
- C#项目引用完全相同dll文件的问题解决方法(反射)
- java使用poi导出excel的内容,同时可以利用反射进行动态获取信息
- 使用C#进行Word 2002和Excel 2002编程
- 使用C#进行Word 2002和Excel 2002编程