Read Excel File
-
I want to read excel sheet cells. I have written the following code in Visual Basic 2005 in single desktop environment with no net connectivity:
Public Class Form1
Public xl As New Excel.Application Public xlsheet As Excel.Worksheet Public xlwbook As Excel.Workbook Public j As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Hello") xlwbook = xl.Workbooks.Open("D:\\sample.xls") xlsheet = xlwbook.Sheets.Item(1) TextBox1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 TextBox2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 xl.ActiveWorkbook.Close(False, "D:\\sample.xls") xl.Quit() End Sub
End Class
I am getting the errors as: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Drawing,System.Diagnostics,System.Windows.Forms /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /rootnamespace:VBCheck /doc:obj\Debug\VBCheck.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"WindowsForms\",PLATFORM=\"AnyCPU\"" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /main:VBCheck.My.MyApplication /debug+ /debug:full /out:obj\Debug\VBCheck.exe /resource:obj\Debug\VBCheck.Form1.resources /resource:obj\Debug\VBCheck.Resources.resources /target:winexe Form1.vb Form1.Designer.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb" error BC30002: Type 'Excel.Application' is not defined. error BC30002: Type 'Excel.Worksheet' is not defined. error BC30002: Type 'Excel.Workbook' is not defined. How to get rid of this error?
-
I want to read excel sheet cells. I have written the following code in Visual Basic 2005 in single desktop environment with no net connectivity:
Public Class Form1
Public xl As New Excel.Application Public xlsheet As Excel.Worksheet Public xlwbook As Excel.Workbook Public j As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Hello") xlwbook = xl.Workbooks.Open("D:\\sample.xls") xlsheet = xlwbook.Sheets.Item(1) TextBox1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 TextBox2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 xl.ActiveWorkbook.Close(False, "D:\\sample.xls") xl.Quit() End Sub
End Class
I am getting the errors as: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Drawing,System.Diagnostics,System.Windows.Forms /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /rootnamespace:VBCheck /doc:obj\Debug\VBCheck.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"WindowsForms\",PLATFORM=\"AnyCPU\"" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /main:VBCheck.My.MyApplication /debug+ /debug:full /out:obj\Debug\VBCheck.exe /resource:obj\Debug\VBCheck.Form1.resources /resource:obj\Debug\VBCheck.Resources.resources /target:winexe Form1.vb Form1.Designer.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb" error BC30002: Type 'Excel.Application' is not defined. error BC30002: Type 'Excel.Worksheet' is not defined. error BC30002: Type 'Excel.Workbook' is not defined. How to get rid of this error?
This sounds like you haven't referenced the Microsoft Excel Object Library. Have a read of this MS help and support Article: How to automate Excel from Visual Basic .NET to fill or to obtain data in a range by using arrays [^] I know the article is about arrays but the principle of connecting to excel is the same
Nagy Vilmos wrote:
And eat bacon. Bacon's real important for 'puters.
-
I want to read excel sheet cells. I have written the following code in Visual Basic 2005 in single desktop environment with no net connectivity:
Public Class Form1
Public xl As New Excel.Application Public xlsheet As Excel.Worksheet Public xlwbook As Excel.Workbook Public j As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Hello") xlwbook = xl.Workbooks.Open("D:\\sample.xls") xlsheet = xlwbook.Sheets.Item(1) TextBox1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 TextBox2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 xl.ActiveWorkbook.Close(False, "D:\\sample.xls") xl.Quit() End Sub
End Class
I am getting the errors as: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Drawing,System.Diagnostics,System.Windows.Forms /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /rootnamespace:VBCheck /doc:obj\Debug\VBCheck.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"WindowsForms\",PLATFORM=\"AnyCPU\"" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /main:VBCheck.My.MyApplication /debug+ /debug:full /out:obj\Debug\VBCheck.exe /resource:obj\Debug\VBCheck.Form1.resources /resource:obj\Debug\VBCheck.Resources.resources /target:winexe Form1.vb Form1.Designer.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb" error BC30002: Type 'Excel.Application' is not defined. error BC30002: Type 'Excel.Worksheet' is not defined. error BC30002: Type 'Excel.Workbook' is not defined. How to get rid of this error?
-
I want to read excel sheet cells. I have written the following code in Visual Basic 2005 in single desktop environment with no net connectivity:
Public Class Form1
Public xl As New Excel.Application Public xlsheet As Excel.Worksheet Public xlwbook As Excel.Workbook Public j As Integer Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Hello") xlwbook = xl.Workbooks.Open("D:\\sample.xls") xlsheet = xlwbook.Sheets.Item(1) TextBox1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 TextBox2.Text = xlsheet.Cells(2, 2) ' row 2 col 2 xl.ActiveWorkbook.Close(False, "D:\\sample.xls") xl.Quit() End Sub
End Class
I am getting the errors as: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Drawing,System.Diagnostics,System.Windows.Forms /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /rootnamespace:VBCheck /doc:obj\Debug\VBCheck.xml /define:"CONFIG=\"Debug\",DEBUG=-1,TRACE=-1,_MyType=\"WindowsForms\",PLATFORM=\"AnyCPU\"" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /main:VBCheck.My.MyApplication /debug+ /debug:full /out:obj\Debug\VBCheck.exe /resource:obj\Debug\VBCheck.Form1.resources /resource:obj\Debug\VBCheck.Resources.resources /target:winexe Form1.vb Form1.Designer.vb "My Project\AssemblyInfo.vb" "My Project\Application.Designer.vb" "My Project\Resources.Designer.vb" "My Project\Settings.Designer.vb" error BC30002: Type 'Excel.Application' is not defined. error BC30002: Type 'Excel.Worksheet' is not defined. error BC30002: Type 'Excel.Workbook' is not defined. How to get rid of this error?
You'll have to add Imports System.Runtime.InteropServices Imports Microsoft.Office.Interop.Excel