[讨论]使用多版本Excel和CAD的应用程序
<p>由于用户的Excel版本可能是2003、2007</p><p>CAD版本可能是2000到2010,如何比较好的实现多版本呢?并且不需要修改程序代码</p><p>以下是一个简单的利用反射实现的例子,希望大家多提意见,谢谢</p><p>(VS2008,.net framework3.5,添加了System.Configuration引用,实现2个按钮事件)</p><p></p><p>using System;<br/>using System.Collections.Generic;<br/>using System.ComponentModel;<br/>using System.Data;<br/>using System.Drawing;<br/>using System.Linq;<br/>using System.Text;<br/>using System.Windows.Forms;<br/>using System.Runtime.InteropServices;//使用Marshal获得进程<br/>using System.Reflection;//用于反射<br/>using System.Configuration;//操作app.config文件</p><p>namespace SeedFileReport<br/>{<br/> public partial class Main : Form<br/> {</p><p> //string excelAssPath;<br/> //string CADAssPath;<br/> string ExcelVer;<br/> string CADVer;<br/> //Assembly excelAss;<br/> //Assembly CADAss;<br/> Type excelType;<br/> Type CADType;<br/> object excelApp;<br/> object CADApp;<br/> string AppPath;<br/> string ExcelAppProID;<br/> string CADAppProID;</p><p> public Main()<br/> {<br/> InitializeComponent();<br/> AppPath = System.Windows.Forms.Application.StartupPath;<br/> //Debug文件的上一级目录,不包含斜杠\<br/> AppPath = AppPath.Substring(0, AppPath.LastIndexOf("\\"));<br/> }</p><p> //Excel<br/> private void button1_Click(object sender, EventArgs e)<br/> {<br/> //修改为通过注册表来创建进程Excel.Application.11/Excel.Application.12<br/> ExcelVer = System.Configuration.ConfigurationManager.AppSettings["ExcelVer"].ToString();<br/> if (ExcelVer == "2003")<br/> {<br/> //excelAssPath = AppPath + @"\Dll\Excel\2003\Microsoft.Office.Interop.Excel.dll";<br/> ExcelAppProID="Excel.Application.11";<br/> //excelAssPath = @"C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll";<br/> }<br/> if (ExcelVer == "2007")<br/> {<br/> //excelAssPath = AppPath + @"\Dll\Excel\2007\Microsoft.Office.Interop.Excel.dll";<br/> ExcelAppProID="Excel.Application.12";<br/> //excelAssPath = @"C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll";<br/> }<br/> //excelAss = Assembly.LoadFile(excelAssPath);<br/> //excelType = excelAss.GetType("Microsoft.Office.Interop.Excel.ApplicationClass");<br/> excelType = System.Type.GetTypeFromProgID(ExcelAppProID);<br/> //excelApp = excelAss.CreateInstance("Microsoft.Office.Interop.Excel.ApplicationClass");<br/> excelApp = Activator.CreateInstance(excelType);<br/> object[] par = new object;<br/> par = true;<br/> excelType.InvokeMember("Visible", BindingFlags.SetProperty, null, excelApp, new object[] { true });<br/> excelType.InvokeMember("UserControl", BindingFlags.SetProperty, null, excelApp, par);</p><p> //获取Workbook集 <br/> object objBooks = excelApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, excelApp, null);<br/> //添加一个新的Workbook <br/> object objBook = objBooks.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objBooks, null);<br/> //获取Sheet集<br/> object objSheets = objBook.GetType().InvokeMember("Worksheets", BindingFlags.GetProperty, null, objBook, null);<br/> //获取第一个Sheet对象 <br/> object[] Parameters = new Object { 3 };<br/> object objSheet = objSheets.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, objSheets, Parameters);<br/> objSheet.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod , null, objSheet, null);<br/> <br/> object Cells1 = objSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, objSheet, new object[] { 1, 1 });<br/> Cells1.GetType().InvokeMember("NumberFormatLocal", BindingFlags.SetProperty, null, Cells1, new object[] { "@" });<br/> Cells1.GetType().InvokeMember("FormulaR1C1", BindingFlags.SetProperty, null, Cells1, new object[] { "反射测试" });</p><p> //foreach (Module aaa in excelAss.GetModules())<br/> //{<br/> // Console.WriteLine(aaa.Name); // 显示该dll下所有的类 <br/> //}<br/> <br/> object range = objSheet.GetType().InvokeMember("Range", BindingFlags.GetProperty, null, objSheet, new object[] { "B2:G8"});<br/> range.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range, new object[] { "反射Excel.Range成功啦!" });<br/> //反射枚举型<br/> //System.Type t1 = excelAss.GetType("Microsoft.Office.Interop.Excel.XlRangeValueDataType");<br/> //object[] o1s = t1.GetMembers();<br/> //object o1 = t1.GetMember("xlRangeValueDefault");<br/> //MessageBox.Show(o1.GetType().ToString());<br/> //range.GetType().InvokeMember("set_Value", BindingFlags.InvokeMethod, null, range, new object[] {o1, "反射B" });<br/> //object res = range.GetType().InvokeMember("Value", BindingFlags.GetProperty, null, range, null);<br/> }</p><p> //CAD<br/> private void button2_Click(object sender, EventArgs e)<br/> {<br/> //获得解决方案的所有Assembly<br/> //Assembly[] AX = AppDomain.CurrentDomain.GetAssemblies();<br/> //遍历显示每个Assembly的名字<br/> //foreach (object var in AX)<br/> //{<br/> // Console.WriteLine("Assembly的名字:" + var.ToString());<br/> //} </p><p><br/> CADVer = System.Configuration.ConfigurationManager.AppSettings["AutoCADVer"].ToString();<br/> //修改为通过注册表来创建进程AutoCAD.Application.16/AutoCAD.Application.17<br/> if (CADVer == "2004")<br/> {<br/> //CADAssPath = AppPath + @"\Dll\AutoCAD\2004\Interop.AutoCAD.dll";<br/> //CADAssPath = AppPath + @"\Dll\AutoCAD\2006\Autodesk.AutoCAD.Interop.dll";<br/> CADAppProID = "AutoCAD.Application.16";<br/> //CADAssPath = @"C:\WINDOWS\assembly\GAC\Autodesk.AutoCAD.Interop\16.2.54.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.dll";<br/> }<br/> if (CADVer == "2006")<br/> {<br/> //CADAssPath = AppPath + @"\Dll\AutoCAD\2006\Autodesk.AutoCAD.Interop.dll";<br/> CADAppProID = "AutoCAD.Application.16";<br/> //CADAssPath = @"C:\WINDOWS\assembly\GAC\Autodesk.AutoCAD.Interop\16.2.54.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.dll";<br/> }<br/> if (CADVer == "2007")<br/> {<br/> //CADAssPath = AppPath + @"\Dll\AutoCAD\2007\Autodesk.AutoCAD.Interop.dll";<br/> CADAppProID = "AutoCAD.Application.17";<br/> //CADAssPath = @"C:\WINDOWS\assembly\GAC_MSIL\Autodesk.AutoCAD.Interop\17.2.0.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.dll";<br/> }<br/> //CADAss = Assembly.LoadFile(CADAssPath);<br/> //if (CADVer == "2004")<br/> //{<br/> // CADType = CADAss.GetType("AutoCAD.AcadApplicationClass");<br/> //}<br/> //if (CADVer != "2004")<br/> //{<br/> // CADType = CADAss.GetType("Autodesk.AutoCAD.Interop.AcadApplicationClass");<br/> //}<br/> CADType = System.Type.GetTypeFromProgID(CADAppProID);<br/> CADApp = Activator.CreateInstance(CADType);<br/> object[] par = new object;<br/> par = true; <br/> CADType.InvokeMember("Visible", BindingFlags.SetProperty, null, CADApp, par);</p><p> //获取Documents集 <br/> object objDocs = CADApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, CADApp, null);<br/> //添加一个新的Document <br/> object objDoc = objDocs.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objDocs, new object[]{Missing.Value});<br/> //获得模型空间<br/> object objModel = objDoc.GetType().InvokeMember("ModelSpace", BindingFlags.GetProperty, null, objDoc, null);<br/> object[] LinePar = new object;<br/> LinePar = new double[] { 10, 20, 0 };<br/> LinePar = new double[] { 120, 190, 0 };<br/> object objLine = objModel.GetType().InvokeMember("AddLine", BindingFlags.InvokeMethod, null, objModel, LinePar);<br/> System.Type typeLine = objLine.GetType();<br/> object length = objLine.GetType().InvokeMember("Length", BindingFlags.GetProperty, null, objLine, null);<br/> object[] CirPar = new object;<br/> CirPar = new double[] { 3, 70, 0 };<br/> CirPar = 150;<br/> object objCir = objModel.GetType().InvokeMember("AddCircle", BindingFlags.InvokeMethod, null, objModel, CirPar);<br/> object center = objLine.GetType().InvokeMember("Center", BindingFlags.GetProperty, null, objCir, null);<br/> object[] TxtPar = new object;<br/> TxtPar = "CAD" + CADVer + "Reflect Success!";<br/> TxtPar = new double[] { 0, 0, 0 };<br/> TxtPar = 23;<br/> object objTxt = objModel.GetType().InvokeMember("AddText", BindingFlags.InvokeMethod, null, objModel, TxtPar);<br/> object layer = objLine.GetType().InvokeMember("Layer", BindingFlags.GetProperty, null, objTxt, null);<br/> }<br/> }<br/>}<br/></p> <p>另外还有配置文件App.Config</p><p><?xml version="1.0" encoding="utf-8" ?><br/><configuration><br/> <appSettings></p><p> <!--Excel版本--><br/> <!--注意:value=后面填写2003或2007>--><br/> <add key="ExcelVer" value="2003" /></p><p><br/> <!--AutoCAD版本--><br/> <!--注意:value=后面填写2004至2009的其中一个--><br/> <add key="AutoCADVer" value="2006" /><br/> <br/> <br/> </appSettings><br/></configuration></p> Com的反射C#很痛苦,呵呵,建议用VB.Net或C#4.0 C#4.0怎么设置呢?在VS2008里面可以选择吗?还是要用VS2010呢? <p>只能用VS2010,:)</p><p>C#4.0的自动反射,代码要简单多了,呵呵</p>
页:
[1]