T
thejamie
I would like to add a late bound Excel version 5 object to a button on an asp
web page written in C#. It must be late bound because a late bound call to
excel 5 will correctly maintain compatibility with all versions of Microsoft
Word starting with version 5 going forward. C# apparently does not late
bind. I am able to get just so far with it then it refuses to recognize a
command:
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" & strToExcel,
objExcel.Cells(1, 1), sql)
where strToExcel is the connection string and sql is the sql command string.
HOW IS THE LINE ABOVE CODED IN C#? If it cannot be properly coded then how
can I insert a vbscript into the web page and have the button_click event
access the vbscript in a C# application?
object objExcelApp;
object objExcelWKB;
object objExcelWKBs;
object objExcelSheets;
object objExcelSheet;
object objExcelRange;
object[] Parameters;
try
{
// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
objExcelApp = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
objExcelWKBs = objExcelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, objExcelApp, null);
//Add a new workbook.
objExcelWKB = objExcelWKBs.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod, null, objExcelWKBs, null);
//Get the worksheets collection.
objExcelSheets =
objExcelWKB.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, objBook_Late, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = 1;
objExcelSheet = objExcelSheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, objExcelSheets,
Parameters);
//Get a range object that contains cell A1.
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objExcelRange = objExcelSheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objExcelSheet,
Parameters);
FROM HERE - Will not recognize:
objExcelRange.GetType().InvokeMember("ActiveSheet",
BindingFlags.SetProperty,
null, objExcelSheet, Parameters);
web page written in C#. It must be late bound because a late bound call to
excel 5 will correctly maintain compatibility with all versions of Microsoft
Word starting with version 5 going forward. C# apparently does not late
bind. I am able to get just so far with it then it refuses to recognize a
command:
objExcel.ActiveSheet.QueryTables.Add("OLEDB;" & strToExcel,
objExcel.Cells(1, 1), sql)
where strToExcel is the connection string and sql is the sql command string.
HOW IS THE LINE ABOVE CODED IN C#? If it cannot be properly coded then how
can I insert a vbscript into the web page and have the button_click event
access the vbscript in a C# application?
object objExcelApp;
object objExcelWKB;
object objExcelWKBs;
object objExcelSheets;
object objExcelSheet;
object objExcelRange;
object[] Parameters;
try
{
// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
objExcelApp = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
objExcelWKBs = objExcelApp.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, objExcelApp, null);
//Add a new workbook.
objExcelWKB = objExcelWKBs.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod, null, objExcelWKBs, null);
//Get the worksheets collection.
objExcelSheets =
objExcelWKB.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, objBook_Late, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = 1;
objExcelSheet = objExcelSheets.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, objExcelSheets,
Parameters);
//Get a range object that contains cell A1.
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objExcelRange = objExcelSheet.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objExcelSheet,
Parameters);
FROM HERE - Will not recognize:
objExcelRange.GetType().InvokeMember("ActiveSheet",
BindingFlags.SetProperty,
null, objExcelSheet, Parameters);