???? ID: 302902 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
Visual C# .NET ?? ??? Office ??????? ??????? ?? ??? ????????
?????? ?????? This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ?? ???? ????????? ?? ???? 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 ???????? ???? ?????? ??????? ??????? ?? ??? ?????? ????????? ?? ?????? ???
????? ???????? ?? ????? ???? ?? ?? ??????? ??????? ?????Microsoft Visual Studio .NET ???? ????.. ????? ???????? ???? ??,??? ?? ????-????? ????, ?? ???? ???????????? . ??? ????Windows ????????? ?? ????? C# ????????? ??????? Form1 ???????? ??? ?? ???? ??.. ???? ?????? ??????Microsoft Excel ???????? ????????? . ??? ???? ?? ???, ????? ????? ?? ???? ????::????? ????????????? ???? ??,?????? ?????? . ????? ????COM ??? ??, ??????Microsoft Excel ???????? ????????? ?? ????? ??????? ???? .???: Office 2003 ??? ???????? Interop Assemblies (PIAs) ????? ??? Office XP PIAs ????? ???? ??, ????? ?????? ??????? ???? ?? ?????Office XP PIAs ?? ???? ??? ???????? ??????? ?? ??? Microsoft ???????? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:328912
(http://support.microsoft.com/kb/328912/
)
Microsoft Office XP ???????? ?????? assemblies (PIAs) ??????? ???? ?? ??? ?????? ???
????? ????,OK ??????????? ?????? ????? ????? ???? ??? ?? ??????? ???? ?? ??? ??? ??? ???? ?? ????????? ?? ?? ???? ?????? ????? ???? ?? ??? wrappers ????? ???? ?? ??? ????? ????, ?? ????? ??????? . ????? ????????? ????, ?? ??? ????????? ????? ????? ????? ??? ????????? ????, ?? Form1 ?? ?? ??? ??????? ???-????? ????Button1 . ?????? ?? ??? ??? ????? ????? ????.. ??? ????? ???, ????? ??? ?? ???????????? ????
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" );
}
}
??????? ?? ??? ????? ??? ??? ???????.. Add the following
line to the end of the list of?? ????? ???? directives:
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
Late bindingIn 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 . The
IDispatch::GetIDsOfNames method allows Visual C# to interrogate an object about what
methods and properties it supports and the
IDispatch::Invoke method then allows Visual C# to call those methods and
properties. ?? ??????? ??? ??? ???????? ?? ??? ????? ???????? ?? inherent ??? ?? ??????? ?????????? ??????? ?? ??? ??? ??? ??, ????? disadvantages ?? ??????? ??? ?? ??????? ???? ?? compile ??? ????? ??? ??, ??? ?? clues ??????? ?? ??? ?? ??? ?? ??? ???? ?? ??? ?????? ?? ???? ??? ?? Intellisense ???????? ?? ?????? ???? ???
????? ???? ?? ??? ??? ???????? ??? ????? C#, ?? ?????
System.Type.InvokeMember ?????? ??? ?? ???? ???
IDispatch::GetIDsOfNames , ??
IDispatch::Invoke ??????? ????? ?? ??????? ?? ????? ?? ????? ???? ?? ????
??? ???????? ?? ????? ???? ?? ?? ??????? ??????? ?????Microsoft Visual Studio .NET ???? ????.. ????? ???????? ???? ??,??? ?? ????-????? ????, ?? ???? ???????????? . ??? ????Windows ????????? ?? ????? C# ????????? ??????? Form1 ???????? ??? ?? ???? ??.. ????? ????????? ????, ?? ??? ????????? ????? ????? ????? ??? ????????? ????, ?? Form1 ?? ?? ??? ??????? ???-????? ????Button1 . ?????? ?? ??? ??? ????? ????? ????.. ??? ????? ???, ????? ??? ?? ???????????? ????
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" );
}
}
??????? ?? ??? ????? ??? ??? ???????.. ????? ?????? ?? ???? ?? ??? ??? ??????? ????? ???? directives: ???? ??????? ?? ???, ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????::
???????? ?? ???? ??? ???????? ??????? ?? ??? ?????? ?? Microsoft ???????? ??? ????? ?? ??? ????? ???? ???????? ?? ????? ????:
245115
(http://support.microsoft.com/kb/245115/
)
??????? ??? ????? ???????? ?? ??? ???????? ?? ????? ????
244167
(http://support.microsoft.com/kb/244167/
)
??????? ??????? ?? ?? ?? ???? Office ?? ??????? ?? ??? ???? ?? ??? ???
247579
(http://support.microsoft.com/kb/247579/
)
?? ???? ?? ?? Office ??????????? ?? ???????? ???? ?? ??? DISPID ???????? ?? ????? ????
???? ???? ???? ??: 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
(http://support.microsoft.com/kb/302902/en-us/
)
Was this information helpful?
How much effort did you personally put forth to use this article?
Tell us why and what can we do to improve this information
Thank you! Your feedback is used to help us improve our support content. For more assistance options, please visit the
Help and Support Home Page .
???? ?????? ???? ?????? ??????
??????
??? ?????? ??????? ????
???? ??????