Allora ho guardato bene il tuo progetto...
Sto cercando di non copiare papale papale ma di imparare infatti se poi comincio a usare questa applicazione devo anche essere in grado di metterci le mani nel caso in cui si incastri qualcosa....
per questo motivo ho pensato di provare a fare la stessa cosa che fai nel progetto ma in C# e per il momento senza Early-late binding (una cosa per volta)...
Ora però ho due problemi che mi impediscono la compilazione (non so sto pensando di rifare da zero una macchina da tenere solo ed esclusivamente per lo sviluppo...in questa ci ho installato/disintallato troppe cose...)
codice:
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = new Excel.Workbook();
Excel.Worksheet ExcelWorksheet = new Excel.Worksheet();
//Excel.Range ExcelRange = new Excel.Range();
//Apro la finestra di dialogo per la selezione del file
OpenFileDialog ApriFile = new OpenFileDialog();
string fileName = ApriFile.FileName;
ExcelWorkbook = ExcelApp.Workbooks.Open(fileName);
string ExcelconnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0;HDR=No";
OleDbConnection ExcelConnection = new OleDbConnection(ExcelconnectionString);
//Apro la connessione
string strSheetname = "Dettaglio OSSIDI-LAMINATOI";
string strSheetRange = "C5";
string ExcelQuery = "SELECT * FROM [" + strSheetname + "$" + strSheetRange + ":" + strSheetRange + "]";
ExcelConnection.Open();
OleDbDataAdapter cmd = new OleDbDataAdapter(ExcelQuery,ExcelConnection);
//OleDbCommand ExcelCommand = new OleDbCommand(ExcelQuery, ExcelConnection);
//ExcelCommand.Connection.Open();
DataSet DS = new DataSet();
cmd.Fill(DS, "tabella");
ExcelConnection.Close();
Il primo problema è che se non commento la riga
Excel.Range ExcelRange = new Excel.Range();
non mi fa proprio compilare perchè mi da errore
"Impossibile creare un'istanza della classe o dell'interfaccia astratta 'Microsoft.Office.Interop.Excel.Range'
e non capisco perchè
Quindi dopo aver commentato compilo ma alla riga
Excel.Workbook ExcelWorkbook = new Excel.Workbook();
compare un errore
System.Runtime.InteropServices.COMException non è stata gestita
Message=Recupero della class factory COM per il componente con CLSID {00020819-0000-0000-C000-000000000046} non riuscito a causa del seguente errore: 80040154 Interfaccia non registrata. (Eccezione da HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Source=mscorlib
ErrorCode=-2147221164
StackTrace:
in System.Runtime.Remoting.RemotingServices.AllocateU ninitializedObject(RuntimeType objectType)
in System.Runtime.Remoting.Activation.ActivationServi ces.CreateInstance(RuntimeType serverType)
in System.Runtime.Remoting.Activation.ActivationServi ces.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
in System.RuntimeTypeHandle.CreateInstance(RuntimeTyp e type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
in System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
in System.RuntimeType.CreateInstanceDefaultCtor(Boole an publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
in System.Activator.CreateInstance(Type type, Boolean nonPublic)
in System.Activator.CreateInstance(Type type)
in ProvaForm.Form1.btnCarica_Click(Object sender, EventArgs e) in D:\Documents\Visual Studio 2010\ProvaForm\ProvaForm\Form1.cs:riga 84
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(Form mainForm)
in ProvaForm.Program.Main() in D:\Documents\Visual Studio 2010\ProvaForm\ProvaForm\Program.cs:riga 18
in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
in System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
in System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
in System.Runtime.Hosting.ManifestRunner.ExecuteAsAss embly()
in System.Runtime.Hosting.ApplicationActivator.Create Instance(ActivationContext activationContext, String[] activationCustomData)
in System.Runtime.Hosting.ApplicationActivator.Create Instance(ActivationContext activationContext)
in System.Activator.CreateInstance(ActivationContext activationContext)
in Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssemblyDebugInZone()
in System.Threading.ThreadHelper.ThreadStart_Context( Object state)
in System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
in System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart()
InnerException: