PDA

View Full Version : سوال: پیدا کردن یک عدد در فایل اکسل



taghavinse
چهارشنبه 02 اسفند 1391, 16:34 عصر
میخوام توسط ویژوال برنامه ای بنویسم که توسط یک کادر یک
عدد رو ازم بگیره بعد که کلیدو زدم داخل یک فایل اکسل که قبلا ایجادش
کردم و توش اطلاعات هست رو بگرده ببینه اون عدد تو اون فایل هست
یا نه و پیغام بده . در ضمن من در کار با فایلها مبتدی هستم
میتونید کمکم کنید
ممنون

حمید محمودی
چهارشنبه 02 اسفند 1391, 19:22 عصر
با تغییرش میتونید به خواسته تون برسید.


Private Sub cmdOpenExcel_Click()
On Error GoTo ErrHandler
Dim xlsApp As Object
Dim xlsWB1 As Object
'Late binding to open an XLS file which is present on my local harddisk
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = True
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Exit Sub
ErrHandler:
MsgBox "There is a problem while opening the xls document. " & _
" Please ensure it is present!", vbCritical, "Error"

End Sub
Now, since I know that my Excel file (which I want to work with) has 15 columns and 200 rows, here is what I did to read all the content to an Array for further manipulation.
Private Sub cmdParse_Click()
On Error GoTo ErrHandler:
Dim xlsApp As Object
Dim xlsWB1 As Object
Dim xlsWS1 As Object
'Opening the file to parse now
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = False
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS1 = xlsWB1.Worksheets("Sheet1")
Dim col As Integer
Dim row As Integer
Dim str As String
str = ""
MaxRow = 200
MaxCol = 15
'Declaring an array so that we don't have to depend on the excel file anymore
ReDim CaseArray(MaxRow, MaxCol)
'Reading the Excel file and putting everything in Memory for faster manipulation
For row = 1 To MaxRow
For col = 1 To MaxCol
CaseArray(row, col) = xlsWS1.cells(row, col).Value
Next
Next
xlsWB1.Close
xlsApp.Quit
Set xlsApp = Nothing
Set xlsWB1 = Nothing
Set xlsWS1 = Nothing
Exit Sub
ErrHandler:
MsgBox "An unknown error occurred while Parsing the Excel. Sorry about that!!" , vbCritical, "Error"
End Sub



منابع دیگر:
http://blogs.msdn.com/b/rahulso/archive/2006/12/15/open-excel-file-with-vb6-and-read-the-content.aspx
http://stackoverflow.com/questions/10415105/how-to-read-an-excel-file97-03-in-visual-basic-6-0

mehran901
پنج شنبه 03 اسفند 1391, 01:14 صبح
با تغییرش میتونید به خواسته تون برسید.


Private Sub cmdOpenExcel_Click()
On Error GoTo ErrHandler
Dim xlsApp As Object
Dim xlsWB1 As Object
'Late binding to open an XLS file which is present on my local harddisk
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = True
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Exit Sub
ErrHandler:
MsgBox "There is a problem while opening the xls document. " & _
" Please ensure it is present!", vbCritical, "Error"

End Sub
Now, since I know that my Excel file (which I want to work with) has 15 columns and 200 rows, here is what I did to read all the content to an Array for further manipulation.
Private Sub cmdParse_Click()
On Error GoTo ErrHandler:
Dim xlsApp As Object
Dim xlsWB1 As Object
Dim xlsWS1 As Object
'Opening the file to parse now
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = False
Set xlsWB1 = xlsApp.Workbooks.Open(strFileName)
Set xlsWS1 = xlsWB1.Worksheets("Sheet1")
Dim col As Integer
Dim row As Integer
Dim str As String
str = ""
MaxRow = 200
MaxCol = 15
'Declaring an array so that we don't have to depend on the excel file anymore
ReDim CaseArray(MaxRow, MaxCol)
'Reading the Excel file and putting everything in Memory for faster manipulation
For row = 1 To MaxRow
For col = 1 To MaxCol
CaseArray(row, col) = xlsWS1.cells(row, col).Value
Next
Next
xlsWB1.Close
xlsApp.Quit
Set xlsApp = Nothing
Set xlsWB1 = Nothing
Set xlsWS1 = Nothing
Exit Sub
ErrHandler:
MsgBox "An unknown error occurred while Parsing the Excel. Sorry about that!!" , vbCritical, "Error"
End Sub



منابع دیگر:
http://blogs.msdn.com/b/rahulso/archive/2006/12/15/open-excel-file-with-vb6-and-read-the-content.aspx
http://stackoverflow.com/questions/10415105/how-to-read-an-excel-file97-03-in-visual-basic-6-0

دمت گرم ، این متن رو که دیدم یاد کتاب آموزش ویبی در 21 روز گرگ پری افتادم ... یادش بخیر این سورس رو کپی زده بود تو یکی از فصل هاش فک کنم :D ! اتفاقا میخواستم بنویسم حسش نبود!! چه خوب شد اینجا دیدم:بامزه:

ولی یکم متفاوت بود مثلا با getobject چک میکرد اگه شی موجود نبود بعدش می ساختش