Excel版本问题?
winform程序, 开发端和客户端用的Excel版本都是2003, 为什么客户端一运行就报错找不到类库 Microsoft Excel 11.0 之类的错误?
------解决方案--------------------你可以参考这两不同方式下的使用Office的方法:
private void btnLater_Click(object sender, EventArgs e)
{
object objApp;
object objBook;
object objBooks;
object objSheets;
object objSheet;
object objRange;
object[] Parameters;
try
{
// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID( "Excel.Application ");
objApp = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
objBooks = objApp.GetType().InvokeMember( "Workbooks ",
BindingFlags.GetProperty, null, objApp, null);
//Add a new workbook.
objBook = objBooks.GetType().InvokeMember( "Add ",
BindingFlags.InvokeMethod, null, objBooks, null);
//Get the worksheets collection.
objSheets = objBook.GetType().InvokeMember( "Worksheets ",
BindingFlags.GetProperty, null, objBook, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = 1;
objSheet = objSheets.GetType().InvokeMember( "Item ",
BindingFlags.GetProperty, null, objSheets, Parameters);
//Get a range object that contains cell A1.
Parameters = new Object[2];
Parameters[0] = "A1 ";
Parameters[1] = Missing.Value;
objRange = objSheet.GetType().InvokeMember( "Range ",
BindingFlags.GetProperty, null, objSheet, Parameters);
//Write "Hello, World! " in cell A1.
Parameters = new Object[1];
Parameters[0] = "Hello, World! ";
objRange.GetType().InvokeMember( "Value ", BindingFlags.SetProperty,
null, objRange, Parameters);
//Return control of Excel to the user.
Parameters = new Object[1];
Parameters[0] = true;
objApp.GetType().InvokeMember( "Visible ", BindingFlags.SetProperty,
null, objApp, Parameters);
objApp.GetType().InvokeMember( "UserControl ", BindingFlags.SetProperty,
null, objApp, 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 ");
}
}
private void btnEarly_Click(object sender, 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(erro