Lettura cella Excel

di il
1 risposte

Lettura cella Excel

Salve,

con il seguente codice leggo una cella di un foglio Excel:

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonConverti.Click
        Dim MyExcel As New Excel.Application

        MyExcel.Workbooks.Open("C:\Users\Tony\Desktop\excel-vbnet\test.xlsx")

        Dim cr As String
        cr = "A37"
        Label1.Text = MyExcel.Range(cr).Value
        MyExcel.Visible = True
    End Sub
End Class

perché cosi mi da errore???

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonConverti.Click
        Dim MyExcel As New Excel.Application

        MyExcel.Workbooks.Open("C:\Users\Tony\Desktop\excel-vbnet\test.xlsx")

        Dim cr As String
        Dim r as integer
        'cr = "A37"
        r = 37
        cr= "A" + Str(r)
        Label1.Text = MyExcel.Range(cr).Value
        MyExcel.Visible = True
    End Sub
End Class

errore:

System.Runtime.InteropServices.COMException non è stata gestita
 ErrorCode=-2146827284
 HResult=-2146827284
 Message=Eccezione da HRESULT: 0x800A03EC
 Source=Microsoft.Office.Interop.Excel
 StackTrace:
      in Microsoft.Office.Interop.Excel.ApplicationClass.get_Range(Object Cell1, Object Cell2)
      in ferrari_excel.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Tony\Desktop\excel-vbnet\ferrari_excel\ferrari_excel\Form1.vb:riga 57
      in System.Windows.Forms.Control.OnClick(EventArgs e)
      in System.Windows.Forms.Button.OnClick(EventArgs e)
      in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 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.OnMessage(Message& m)
      in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
      in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
      in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
      in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
      in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
      in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
      in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
      in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
      in ferrari_excel.My.MyApplication.Main(String[] Args) in :riga 81
      in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
      in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
      in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
      in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      in System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      in System.Threading.ThreadHelper.ThreadStart()
 InnerException: 

1 Risposte

  • Re: Lettura cella Excel

    Controlla il risultato di questa

    cr= "A" + Str(r)

    Se controlli il contenuto della variabile cr sarà

    A 37

    con uno spazio che non ci dovrebbe essere. 

    Controlla come opera la funzione Str

Devi accedere o registrarti per scrivere nel forum
1 risposte