|
|
|
Many times we have requirements to load data from CSV file, text file or Excel file...
In this article I will show a sample code which reads a specified rows and columns from excel file. |
Click here to copy the following block | Private Sub Command1_Click() ImportExcelToSQL "c:\temp\diamond_upload.xls", 0, 20, True End Sub
Sub ImportExcelToSQL(filepath, maxrows_to_read, maxcols_to_read, exitonblankrow) Dim objExcel Dim r, c, cellVal, sLine bolTemp = False Set objExcel = CreateObject("Excel.Application") objExcel.Workbooks.Open filepath
objExcel.DisplayAlerts = False objExcel.Sheets(1).Select objExcel.Sheets(1).Activate
If maxcols_to_read <= 0 Then maxcols_to_read = objExcel.Columns.Count End If
If maxrows_to_read <= 0 Then maxrows_to_read = objExcel.Rows.Count End If
For r = 1 To maxrows_to_read If exitonblankrow <> 0 And objExcel.Cells(r, 1) = "" Then Exit For End If For c = 1 To maxcols_to_read cellVal = objExcel.Cells(r, c) sLine = sLine & ", " & cellVal Next sLine = sLine & vbCrLf WriteDebug sLine Me.Caption = r DoEvents Next objExcel.Workbooks.Close objExcel.DisplayAlerts = True objExcel.Quit Set objExcel = Nothing End Sub
Sub WriteDebug(sLine) Debug.Print sLine End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|