Unable to fetch values from Excel Worksheet in vb.net

V

vini dimple

Hi

i have a serious problem while i was working in Excel.I want to fetch
columns from an excel worksheet and i need to compare it with an sql
querry fields,so i tried to open an excle worksheet first..


Unfortunately my code was throwing error like


CLSID\{00020819-0000-0000-C000-000000000046} IS NOT VALID OR NOT
REGISTERED.
But the same exe is working fine in other Pc's except mine.Then i
overcome the error with the code


Dim oldCI As System.Globalization.CultureInfo =
System.Threading.Thread.CurrentThread.CurrentCultu re
System.Threading.Thread.CurrentThread.CurrentCultu re = New
System.Globalization.CultureInfo("en-US")


after that excel sheet was opening but i cant read the rows or columns
in it
error throws like "Old format or Invalid Type Library" in the
particular statement wherever i used worksheet.methodname


Here i put the entire code


please help me


Imports System.Windows.Forms
Imports Microsoft.Office.Core
Imports Excel
Public Class Form1
Inherits System.Windows.Forms.Form
Public xlApp As New Excel.Application


Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click


Try


Dim strFileName As String
Dim intRows, intCols As Integer
Dim r, c As Integer
Dim Strquerry As String
Dim strpos As Integer
Dim strposc As String
Dim StrMid As String
'Create a dialog box to find the excel file


Dim dlg As New OpenFileDialog
dlg.Filter = "Excel Files (*.xls)|*.xls"
dlg.FilterIndex = 0
dlg.ShowDialog()
strFileName = dlg.FileName


Dim oldCI As System.Globalization.CultureInfo =
System.Threading.Thread.CurrentThread.CurrentCultu re
System.Threading.Thread.CurrentThread.CurrentCultu re = New
System.Globalization.CultureInfo("en-US")


Dim xlWB As Excel.Workbook
xlWB = xlApp.Workbooks.Open(strFileName)
TextBox1.Text = strFileName
xlWB.Application.Visible = True


System.Threading.Thread.CurrentThread.CurrentCultu re = oldCI


Dim xls As Excel.Sheets
Dim xlsheet As New Excel.Worksheet


xls = xlWB.Worksheets


xlsheet = CType(xls.Item(1), Excel.Worksheet)
xlsheet.activate()


intRows = xlsheet.UsedRange.Rows.Count
If intRows <> 0 Then
intCols = xlsheet.UsedRange.Columns.Count
If intCols <> 0 Then


' Scroll through all the rows and columns retrieving values.
For r = 1 To intRows
For c = 1 To intCols
Strquerry = TextBox2.Text
strpos = InStr(1, Strquerry, "(")
strposc = InStr(strpos, Strquerry, ",")
StrMid = Mid(Strquerry, strpos, CType(xlsheet.Cells(r, c),
Excel.Range).Text)
StrMid = Mid(Strquerry, strposc, CType(xlsheet.Cells(r, c),
Excel.Range).Text)
MsgBox(StrMid)
Next
Next


xlWB.Close(False)
xlApp.Quit()
xls = Nothing
xlWB = Nothing
xlApp = Nothing
End If


End If
Catch ex As Exception
MsgBox(ex.Message)
End Try


End Sub


End Class


thanks in advance
Dimple




*** Sent via Developersdex http://www.developersdex.com ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top