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

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

?? ??? ??????

??????

??? ????? ????? ?? ??? Microsoft Office ??????? ? ????????? ??? ??????? ????????? ?? Office ??? ?? ???? ?????? ?????? ????? ?????? ?? ??? ??? ????????. ???? ????? ??? ??????? ? ?????????? ??????? ??? ???????? ???? ???? ?????? ??? ??????? ? ???????? ????? ?????. ?? Visual C# ? ?? ???? ????? ???????? ????? ?????? "?" ????? ???????. ???? ?? ???? ??? ????? ???? ?????? ??? ????? ????? ?? ???????? ??? ?? ??? ?????? ????? maintainability.

???? ??? ??????? ??? ??????? ? ????? ????? ??????? ????????? ?????? Visual C# "???????" ??? ???? ????? ???????? ???????? ???? ???? ??? ??????? ?? ????? ".

????? ??????

?? ????? ?????? Visual C# ?????? ??? ????????? ???????? ??? ????? Office ??????? ????? ?????? ??? ?????? ?? ????? ????? ????????. ???? ????? ?????? ??????? ????? ?? ?????? ?? ???? ???? ?????? ?? ?? ??? ????? ????? ?????? ????? ?????? ??????? ?? ??????? ????? ?????? ???? ?? ??????? ?? ????? ???????. ??? ?????? ??? ??? ?? ??? ??????? ?????? ?????? ??? ????? ?? ?????, ????? ?????? ??????? ????; ??? ???? ?????? ?? ??? ?? ???? ????? ?????? ????, ?????????? ?????? ????????? ????? ??????? ??? ?????? ????.

??? ????? ?????? ???? ???????? ???? ????? ?????? ????? ??????? ????????. ??? ???? ??????? ????? ?? ???? ???? ????? ??? Microsoft Excel 2002 ????? ???? ?? ??????? ??? ????? ?? Excel 2000 ?? ?????? ????? ??? ??????? ?? ??????? ????????. ?? ????? ????? ????? ?????? ??? ????????? ?? ???? ?????? ?? ??????? Visual C# ???? ?????? ??????? ??? Excel 2002 ?????? Excel 2000. ????? ??? ??????? ?? ????? ?????? ???? ??? ???????? ??????? ??????? ??? ??????? ?????? ?? ????? Office ???? ???? ?? ???? ??? ????? ??????? ?????? ???????.

???? ??????? ??????? ????? ????? ???? "?????" ???? ?????? ????? ??????. ???? ?? ? ??? ???? ??????? ????? ?????? ????? ??? ???? ????? ????? ???? ???????.

????? ???? "?????" ???? ?????? ????? ??????

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

      ?????? ????? Office 2003 "????????? Interop ????????" (PIAs). ?? ????? Office XP PIAs ???? ???? ???????. ?????? ??? ??????? ?????? ??? Office XP PIAs ???? ??? ??? ??????? ?????? ?????? ?? "????? ??????? ?? Microsoft:
      328912Microsoft Office XP ??????? ??????? ??????? ???????? ????????? (PIAs) ???????? ???????
    3. ???? ??? ????? ?? ???? ?????? ????? ????? ????? ?????????. ??? ????? ???? ?????? wrappers ???????? ???? ?????? ???? ??? "???".
  3. ?? ??????? ???? ??? Toolbox ???? ???? ??????? ?? ????? ?? ??? Form1.
  4. ???? ????? ??????? ??? Button1. ???? ???? ????????? ???????? ???????.
  5. ?? ???? ????????? ???????? ??????? ???????? ???????? ???????
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    					
    ??:
    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. ??????? ??? ???? ???? ????????? ????????. ?? ?????? ????? ?????? ??? ????? ??????? ???????? ???????:
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;
    					

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

??? ??? ????? ?????? ????? ??????? ????? ??? ??? ??????? ???? ????? ?????? ????????? ??? ???????? ?????? ???. ?????? ????? ??? ?? ???? ?????? ????? ????? COM ????: IDispatch. ????? IDispatch::GetIDsOfNames ???? Visual C# interrogate ???? ??? ?? ?? ?????? ?????? ????? ????? ????? IDispatch::Invoke ?? Visual C# ??????? ?????? ?????? ???. ????? ??????? ?????? ??? ??? ???? ????? ??? ?????? ??????? ???? ??? ?????? ?? ????? ??????. ??? ???? ???? ?? ????? ????? ?????? ??? ??????? ??????? ??? ????? ????????? ???????? "??????? ???????? ??? ??? ?? ???? ????? Intellisense ???? ?? ???? ???? ?????? ????????? ??? ?????? ??????.

???????? ????? ??????? ?? "Visual C" # ?????? ????? System.Type.InvokeMember. ??????? ??? ??????? IDispatch::GetIDsOfNames ? IDispatch::Invoke ???? ?????? ?????? ???? ???????.

????? ???? "?????" ???? ?????? ????? ???????

  1. ??? ????? Microsoft Visual Studio .NET. ?? ??????? ???? ???? ??? ???? ?? ???? ??? ???????. ??? ????? Windows ?? ????? Visual C# "????????". ??? ????? Form1 ???? ???????.
  2. ?? ??????? ???? ??? Toolbox ???? ???? ??????? ?? ????? ?? ??? Form1.
  3. ???? ????? ??????? ??? Button1. ???? ???? ????????? ???????? ???????.
  4. ?? ???? ????????? ???????? ??????? ???????? ???????? ???????
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    						
    ??:
    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. ??????? ??? ???? ???? ????????? ????????. ?? ?????? ????? ?????? ??? ????? ??????? ???????? ???????:
    using System.Reflection; 
    					

?????

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

???????

???? ???????: 302902 - ????? ??? ??????: 08/???? ?????/1428 - ??????: 7.4
????? ???
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
????? ??????: 
kbmt kbpia kbautomation kbhowtomaster KB302902 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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