问题描述
你好,
我创建了一个使用Excel中后期绑定的应用程序.该应用程序在Office 2010中工作正常,并将文件另存为Excel 2003版本,但是当我在Office 2003中安装该文件时,会导致错误调用的目标抛出了异常.行:mscorlib"
当我尝试在Office 2003中调试应用程序时,在此行遇到错误: objBook_Late = objBooks_Late.GetType().InvokeMember("SaveAs",BindingFlags.InvokeMethod,null,objBook_Late,Parameters);
希望有人可以帮助我,非常感谢Advance
以下是我的示例代码
Hello,
I created an application that uses the late binding in Excel. The application is working fine in Office 2010 and it save the file as Excel 2003 version but when i install it in Office 2003 it causes an error "Exception has been thrown by the target of an invocation. Line: mscorlib"
when i tried to debug the application in Office 2003 the error is encountered in this line : objBook_Late = objBooks_Late.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook_Late, Parameters);
Hope someone can help me on this, Many thanks in Advance
below is my sample code
private void button3_Click(object sender, EventArgs e)
{
string sFilename;
//Excel Late Binding
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);
//*****SET Header******
//**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);
//Case# cell A1.
Parameters = new Object[1];
Parameters[0] = "TESTA";
objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
//ColumnWidth
Parameters = new Object[1];
Parameters[0] = 11;
objRange_Late.GetType().InvokeMember("ColumnWidth", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
//End Header
//START Set Data For each
string aa;
int ictr = 2;
//**Get a range object that contains cell A.
Parameters = new Object[2];
Parameters[0] = "A" + ictr.ToString();
Parameters[1] = Missing.Value;
objRange_Late = objSheet_Late.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objSheet_Late, Parameters);
//Value cell A
Parameters = new Object[1];
Parameters[0] = "TestAA"; //Case# (A)
objRange_Late.GetType().InvokeMember("Value", BindingFlags.SetProperty,
null, objRange_Late, Parameters);
//END Set Data
//Return control of Excel to the user.
Parameters = new Object[1];
Parameters[0] = false;
objApp_Late.GetType().InvokeMember("Visible", BindingFlags.SetProperty,
null, objApp_Late, Parameters);
objApp_Late.GetType().InvokeMember("UserControl", BindingFlags.SetProperty,
null, objApp_Late, Parameters);
//Save
sFilename = @"C:\TestA" + DateTime.Now.Date.ToString("ddMMyyyyHHNNSS") + ".xls";
Parameters = new Object[12];
Parameters[0] = sFilename;
Parameters[1] = 56;//Excel.XlFileFormat.xlExcel8;
Parameters[2] = Missing.Value;
Parameters[3] = Missing.Value;
Parameters[4] = false;
Parameters[5] = false;
//xlExclusive 3
//xlNoChange 1
//xlShared 2
Parameters[6] = 2;//Excel.XlSaveAsAccessMode.xlNoChange;
Parameters[7] = Missing.Value;
Parameters[8] = Missing.Value;
Parameters[9] = Missing.Value;
Parameters[10] = Missing.Value;
Parameters[11] = Missing.Value;
objBook_Late = objBooks_Late.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, objBook_Late, Parameters);
//Quit - NO longer needed when Application(xmIF is Close Excel.EXE will be closed
Parameters = new Object[1];
Parameters[0] = true;
objApp_Late.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, objApp_Late, null);
objApp_Late = null;
// ENSURE VARIABLES ARE DESTROYED & RELEASED AS IT GOES OUT OF SCOPE
objRange_Late = null;
objSheet_Late = null;
objSheets_Late = null;
objBook_Late = null;
objBooks_Late = null;
// CALL THE GARBAGE COLLECT METHOD
GC.Collect();
GC.WaitForPendingFinalizers();
objApp_Late = null;
}
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");
}
}
推荐答案
这篇关于C#中Excel错误SaveAs 2003版本中的后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!