Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
App. WinForms / WPF .NET
Utilizzo di excel in LateBinding
martedì 05 aprile 2005 - 12.28
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
marco13
Profilo
| Newbie
5
messaggi | Data Invio:
mar 5 apr 2005 - 12:28
Ho la necessita di utilizzare excel in latebinding per non avere problemi con le differenti versioni (da excel 97 e superiore), però mi sono accorto che con excel 2003 il codice di esempio riportato da MS in MSDN "KB302902" non funziona correttamente, genera un errore sulla riga di codice:
//Add a new workbook.
objBook = objBooks.GetType().InvokeMember( "Add", BindingFlags.InvokeMethod, null, objBooks, null );
Lo stesso codice con officeXP non da problemi.
Qualcuno ha qualche informazione in più? e/o sa come aggirare il problema? (sempre latebinding)
Grazie, Marco
Cteniza
Profilo
| Guru
1.509
messaggi | Data Invio:
mar 5 apr 2005 - 17:55
Questo è l'esempio su cui ho fatto delle prove
// created on 14/04/2004 at 23.36
using System;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
public class ExcelClass
{
public ExcelClass()
{
}
public void ScriviFoglio()
{
object objApp;
object objBook;
object objBooks;
object objSheets;
object objSheet;
object objRange;
object[] Parameters;
try
{
// Istanziamo Excel.
objApp = (object)new Microsoft.Office.Interop.Excel.Application();
// Istanziamo i Workbooks
objBooks = objApp.GetType().InvokeMember( "Workbooks",
BindingFlags.GetProperty, null, objApp, null );
// Aggiunta di workbook
objBook = objBooks.GetType().InvokeMember( "Add",
BindingFlags.InvokeMethod, null, objBooks, null );
// Istanziamo i fogli
objSheets = objBook.GetType().InvokeMember( "Worksheets",
BindingFlags.GetProperty, null, objBook, null );
// Prendiamo la prima cartella ed il primo foglio
Parameters = new Object[1];
Parameters[0] = 1;
objSheet = objSheets.GetType().InvokeMember( "Item",
BindingFlags.GetProperty, null, objSheets, Parameters );
// Il range contenuto nella cella A1
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objRange = objSheet.GetType().InvokeMember( "Range",
BindingFlags.GetProperty, null, objSheet, Parameters );
// Scriviamo il valore sulla cella A1
Parameters = new Object[1];
Parameters[0] = "Ho scritto sulla cella";
objRange.GetType().InvokeMember( "Value", BindingFlags.SetProperty,
null, objRange, Parameters );
// Ritorniamo il controllo all'utente
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 = "Errore: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Numero: " );
errorMessage = String.Concat( errorMessage, theException.Source );
MessageBox.Show( errorMessage, "Errore" );
}
}
}
marco13
Profilo
| Newbie
5
messaggi | Data Invio:
mar 5 apr 2005 - 18:03
E...? Non ho capito il senso della tua risposta... hai riportato solo il codice ma nessun commento ulteriore. L'errore ti si replica o no?
Dovresti togliere anche lo "using Excel = Microsoft.Office.Interop.Excel;" per avere la situazione corretta, in questo caso non è necessario usare le PIA perchè si usa l'associazione tardiva.
Grazie, marco.
marco13
Profilo
| Newbie
5
messaggi | Data Invio:
mar 5 apr 2005 - 18:07
Scusami ancora... ho rivisto meglio l'esempio che riporti, ma in questo modo fai riferimento alle PIA di excel 2003, in pratica usi l'early binding e non il late binding. In questo modo l'applicazione compilata funziona solo con office 2003 e non con le versioni precedenti (o sbaglio?), perchè per ogni versione di office ci sarebbe da inserire il riferimento alla libreria corretta.
Grazie Marco
marco13
Profilo
| Newbie
5
messaggi | Data Invio:
mar 5 apr 2005 - 18:09
Riporto per semplicità un estratto del KB302902:
Late binding
In 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. Late binding in this fashion has the advantage of removing some of the version dependencies that are inherent with early binding. However, it has the disadvantages of removing compile-time checks on the integrity of automation code, as well as not providing Intellisense features that can provide clues to correct calls to methods and properties.
To use late binding in Visual C#, use the System.Type.InvokeMember method. This method calls IDispatch::GetIDsOfNames and IDispatch::Invoke to bind to the Automation server's methods and properties.
back to the top
Create an Automation client that uses late binding
Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Select Windows Application from the Visual C# Projects types. Form1 is created by default.
On the View menu, select Toolbox to display the Toolbox, and add a button to Form1.
Double-click Button1. The code window for the Form appears.
In the code window, replace the following code
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" );
}
}
Scroll to the top of the code window. Add the following line to the end of the
marco13
Profilo
| Newbie
5
messaggi | Data Invio:
lun 11 apr 2005 - 15:09
RISOLTO!!!
"Excel automation code fails in non-English Standard and Formats"
Per risolvere il problema è sufficiente aggiungere l'ultimo parametro "cultureinfo" al metodo InvokeMember.
Esempio codice in VB.Net
Dim objXlType As Type
Dim objXlObj As Object
Dim objXlWbks As Object
Dim objXlNewWbk As Object
objXlType = Type.GetTypeFromProgID("Excel.Application")
objXlObj = Activator.CreateInstance(objXlType)
objXlWbks = objXlObj.GetType.InvokeMember("Workbooks", System.Reflection.BindingFlags.GetProperty, Nothing, objXlObj, Nothing, _
New System.Globalization.CultureInfo(1033))
objXlNewWbk = objXlWbks.GetType.InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, Nothing, objXlWbks, Nothing, _
New System.Globalization.CultureInfo(1033))
etc... etc...
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !