Visual C# .NET ?? ??? Office ??????? ??????? ?? ??? ????????

???? ?????? ???? ??????
???? ID: 302902 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ?? ???? ????????? ?? ???? Microsoft Office ???? ???????? ?????????, ??? ?? ??????? ?? Office ?? ??? ??? ??? ??? ?? ?????????? ?? ??? ????????? ?? ???????? ?? ?????? ???? ?????? ????????? ????????? ??? ?? ???????? ??? ?? ??????? ?? ?? ??????????? ???? ?? ??? ???? ??? ?? ????????? ?????? ??????????. ??? ????? C#, ?? ?????? ?? ???????? ?????? ???????? ????????, ????? ????????. ?????? ?? ?? ???????? ?? ???????????, ????????????? ?? maintainability ????, ???? ????????? ?? ?? ?????? ?? ???????? ?? ???? ????

?? ???? ????? ???? ?? ?? ????? C# ??????? ??????? ?? ??? ????????? ?? ??? ???????? ?? ????? ???? ?? ?? ????? ?????? ?? ???????? demonstrate ??? ????? ?????? ???? ???

????? ????????

????? ???????? ?? ??? ????? C# ????? Office ????????? ??? question ???? ?? ??? ?? ????? ???? ?? ??? ?? ??? ???? ????? ???? ?? ???? ??? ?????? ??????? ??????? ??????? ?????? ????? ?? ???? ??? ?? ?? ???? ?? ??? ?? ??? ?????? ?? ???????? ?????? ???? ???, ?? ?? ?? ??? ?? ??? ???????? ?????? ?? ???? ????? ?? ????????? ???? ?? ??? ???????? ?? ???? ???? ??? ??? ?? ?????? ?? ??? ???? ?? ??? ????? ??? ?? ??? ?????? ??, ??????? ????? ???????? ????? ???-??? ??; ???????, ????? ???????? ??? ?? ???? ??, ?????? ???????? ???? ??? ???????? ?? ????? ???? ??? ????? negligible ???

???????? early ?? disadvantage ?? ?? ?? ???? ??????? ?????? ???????? introduce ?? ???? ???? ?????? ?? ??? ?? ?? ?????? ?? ??? ?? ?? Excel 2000 ?? ??? ???????? ?? ?? ???? ?????? ??? ?? ???? ?? ??? ??? ???????? ???? ??, ???? Microsoft Excel 2002 ??????? ????? ??? introduces suppose. ?? ?????????? ?? ???????? ?? ?????? ????? ???????? ?? ?? ????? C# ????????? ?? ??? Excel 2000 ?? ???????? ???? ?? ??? Excel 2002 ?????? ?? ??????? ?? ????? ???? ?? ?? ???????? ?? ???? ?? ???? ??? ????? ???????? ??, ?? ?????? ?? ????, ?? ??? ?????????? ???????? ?? ?? ?? ?? ?????? ?? ???? ??????? ??????? ??????? ?? ?????? ???? ????? ??? ?? Office ????????? ?? ??????????? ??????? ?? ??? ?????? ?? ??????? ??????

????? ????? ?? ???? demonstrate ???? ????? ???????? ?? ????? ???? ?? ?? ??????? ??????? ?? ????? ?? ??? ??? ????? ??? ??, ?? ??? ??? ??? ????? illustrate, early ???????? ???? ?????? ??????? ??????? ?? ??? ?????? ????????? ?? ?????? ???

????? ???????? ?? ????? ???? ?? ?? ??????? ??????? ?????

  1. Microsoft Visual Studio .NET ???? ????.. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????. ??? ????Windows ??????????? ????? C# ????????? ??????? Form1 ???????? ??? ?? ???? ??..
  2. ???? ?????? ??????Microsoft Excel ???????? ?????????. ??? ???? ?? ???, ????? ????? ?? ???? ????::
    1. ????? ????????????????? ??,?????? ??????.
    2. ????? ????COM??? ??, ??????Microsoft Excel ???????? ??????????? ????? ??????? ????.

      ???:Office 2003 ??? ???????? Interop Assemblies (PIAs) ????? ??? Office XP PIAs ????? ???? ??, ????? ?????? ??????? ???? ?? ?????Office XP PIAs ?? ???? ??? ???????? ??????? ?? ??? Microsoft ???????? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
      328912Microsoft Office XP ???????? ?????? assemblies (PIAs) ??????? ???? ?? ??? ?????? ???
    3. ????? ????,OK??????????? ??????????? ????? ???? ??? ?? ??????? ???? ?? ??? ??? ??? ???? ?? ????????? ?? ?? ???? ?????? ????? ???? ?? ??? wrappers ????? ???? ?? ??? ????? ????, ?? ????? ???????.
  3. ????? ?????????????, ?? ??? ????????? ?????????? ????? ??? ????????? ????, ?? Form1 ?? ?? ??? ???????
  4. ???-????? ????Button1. ?????? ?? ??? ??? ????? ????? ????..
  5. ??? ????? ???, ????? ??? ?? ???????????? ????
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    					
    with::
    private void button1_Click(object sender, System.EventArgs e)
    {
    	Excel.Application objApp;
    	Excel._Workbook objBook;
    	Excel.Workbooks objBooks;
    	Excel.Sheets objSheets;
    	Excel._Worksheet objSheet;
    	Excel.Range range;
    
    	try
    	{
    		// Instantiate Excel and start a new workbook.
    		objApp = new Excel.Application();
    		objBooks = objApp.Workbooks;
    		objBook = objBooks.Add( Missing.Value );
    		objSheets = objBook.Worksheets;
    		objSheet = (Excel._Worksheet)objSheets.get_Item(1);
    
    		range = objSheet.get_Range("A1", Missing.Value);
    
    		range.set_Value(Missing.Value, "Hello, World!" );
    
    		//Return control of Excel to the user.
    		objApp.Visible = true;
    		objApp.UserControl = true;
    	}
    	catch( Exception theException ) 
    	{
    		String errorMessage;
    		errorMessage = "Error: ";
    		errorMessage = String.Concat( errorMessage, theException.Message );
    		errorMessage = String.Concat( errorMessage, " Line: " );
    		errorMessage = String.Concat( errorMessage, theException.Source );
    
    		MessageBox.Show( errorMessage, "Error" );
    	}
    }  
    					
  6. ??????? ?? ??? ????? ??? ??? ???????.. Add the following line to the end of the list of?? ????? ????directives:
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;
    					

Late binding

In contrast to early binding, late binding waits until run time to bind property and method calls to their objects. To do this, the target object must implement a special COM interface:IDispatch. TheIDispatch::GetIDsOfNamesmethod allows Visual C# to interrogate an object about what methods and properties it supports and theIDispatch::Invokemethod then allows Visual C# to call those methods and properties. ?? ??????? ??? ??? ???????? ?? ??? ????? ???????? ?? inherent ??? ?? ??????? ?????????? ??????? ?? ??? ??? ??? ??, ????? disadvantages ?? ??????? ??? ?? ??????? ???? ?? compile ??? ????? ??? ??, ??? ?? clues ??????? ?? ??? ?? ??? ?? ??? ???? ?? ??? ?????? ?? ???? ??? ?? Intellisense ???????? ?? ?????? ???? ???

????? ???? ?? ??? ??? ???????? ??? ????? C#, ?? ?????System.Type.InvokeMember?????? ??? ?? ???? ???IDispatch::GetIDsOfNames, ??IDispatch::Invoke??????? ????? ?? ??????? ?? ????? ?? ????? ???? ?? ????

??? ???????? ?? ????? ???? ?? ?? ??????? ??????? ?????

  1. Microsoft Visual Studio .NET ???? ????.. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????. ??? ????Windows ??????????? ????? C# ????????? ??????? Form1 ???????? ??? ?? ???? ??..
  2. ????? ?????????????, ?? ??? ????????? ?????????? ????? ??? ????????? ????, ?? Form1 ?? ?? ??? ???????
  3. ???-????? ????Button1. ?????? ?? ??? ??? ????? ????? ????..
  4. ??? ????? ???, ????? ??? ?? ???????????? ????
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    						
    with::
    private void button1_Click(object sender, System.EventArgs e)
    {
    	object objApp_Late;
    	object objBook_Late;
    	object objBooks_Late;
    	object objSheets_Late;
    	object objSheet_Late;
    	object objRange_Late;
    	object[] Parameters;
    
    	try
    	{
    		// Get the class type and instantiate Excel.
    		Type objClassType; 
    		objClassType = Type.GetTypeFromProgID("Excel.Application"); 
    		objApp_Late = Activator.CreateInstance(objClassType);
    
    		//Get the workbooks collection.
    		objBooks_Late = objApp_Late.GetType().InvokeMember( "Workbooks", 
    		BindingFlags.GetProperty, null, objApp_Late, null );
    
    		//Add a new workbook.
    		objBook_Late = objBooks_Late.GetType().InvokeMember( "Add", 
    			BindingFlags.InvokeMethod, null, objBooks_Late, null );
    
    		//Get the worksheets collection.
    		objSheets_Late = objBook_Late.GetType().InvokeMember( "Worksheets",
    			BindingFlags.GetProperty, null, objBook_Late, null );
    
    		//Get the first worksheet.
    		Parameters = new Object[1];
    		Parameters[0] = 1;
    		objSheet_Late = objSheets_Late.GetType().InvokeMember( "Item", 
    			BindingFlags.GetProperty, null, objSheets_Late, Parameters );
    
    		//Get a range object that contains cell A1.
    		Parameters = new Object[2];
    		Parameters[0] = "A1";
    		Parameters[1] = Missing.Value;
    		objRange_Late = objSheet_Late.GetType().InvokeMember( "Range",
    			BindingFlags.GetProperty, null, objSheet_Late, Parameters );
    
    		//Write "Hello, World!" in cell A1.
    		Parameters = new Object[1];
    		Parameters[0] = "Hello, World!";
    		objRange_Late.GetType().InvokeMember( "Value", BindingFlags.SetProperty, 
    			null, objRange_Late, Parameters );
    
    		//Return control of Excel to the user.
    		Parameters = new Object[1];
    		Parameters[0] = true;
    		objApp_Late.GetType().InvokeMember( "Visible", BindingFlags.SetProperty,
    			null, objApp_Late, Parameters );
    		objApp_Late.GetType().InvokeMember( "UserControl", BindingFlags.SetProperty,
    			null, objApp_Late, Parameters );
    	}
    	catch( Exception theException ) 
    	{
    		String errorMessage;
    		errorMessage = "Error: ";
    		errorMessage = String.Concat( errorMessage, theException.Message );
    		errorMessage = String.Concat( errorMessage, " Line: " );
    		errorMessage = String.Concat( errorMessage, theException.Source );
    
    		MessageBox.Show( errorMessage, "Error" );
    	}
    }
    					
  5. ??????? ?? ??? ????? ??? ??? ???????.. ????? ?????? ?? ???? ?? ??? ??? ??????? ????? ????directives:
    using System.Reflection; 
    					

??????

???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????::
Visual Studio ?? ??? Microsoft Office ??????????
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
???????? ?? ???? ??? ???????? ??????? ?? ??? ?????? ?? Microsoft ???????? ??? ????? ?? ??? ????? ???? ???????? ?? ????? ????:
245115??????? ??? ????? ???????? ?? ??? ???????? ?? ????? ????
244167??????? ??????? ?? ?? ?? ???? Office ?? ??????? ?? ??? ???? ?? ??? ???
247579?? ???? ?? ?? Office ??????????? ?? ???????? ???? ?? ??? DISPID ???????? ?? ????? ????

???

???? ID: 302902 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Word 2002
??????: 
kbpia kbautomation kbhowtomaster kbmt KB302902 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:302902

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com