使用QTP從excell表中拷貝數據到數據庫表中
發表于:2013-07-22來源:淘測試作者:寶駒點擊數:
標簽:qtp
使用QTP從excell表中拷貝數據到數據庫表中
'----------------------------------------------
' 函數名稱: InsertFromTable(datasource,fold,sheet,row)
' 作用: 從excell表中插入數據到指定的數據表
' 參數說明: datasource: 指定的
數據庫名稱
' fold: excell文件目錄
' sheet: 指定的sheet,sheet名稱為數據表的名稱
' ' row= all ,那么插入該sheet下的所有行數據
' row =2 ,3等序數,則插入數據指定的行數
'----------------------------------------------
Sub InsertFromTable(datasource,fold,sheet,row)
Set excell = CreateObject("Excel.Application")
excell.Visible = false
excell.WorkBooks.Open(fold)
excell.WorkSheets(sheet).Activate
'得到該sheet的總列數
col = excell.worksheets(sheet).UsedRange.columns.count
'得到該sheet的總行數
If row = "all" Then
rows = excell.WorkSheets(sheet).UsedRange.Rows.Count
For indexi = 2 to rows
strSql = ""
For indexj = 1 to col
If indexj = col Then
strSql = strSql +"'" + CStr(excell.Cells(indexi, indexj).Text) + "'"
else
strSql = strSql + "'" + CStr(excell.Cells(indexi, indexj).Text) + "',"
End If
Next
'插入數據的sql
sql = "insert into " + sheet + " values(" + strSql + ")"
Call InsertOne(datasource,sql)
Next
else
For index = 1 to col
If index = col Then
strSql = strSql +"'" + CStr(excell.Cells(row, index).Text) + "'"
else
strSql = strSql + "'" + CStr(excell.Cells(row, index).Text) + "',"
End If
Next
sql = "insert into AUCTION_BUYER_ANONY values(" + strSql + ")"
Call InsertOne(datasource,sql)
End If
excell.ActiveWorkbook.Close(0)
excell.Quit()
End Sub
如要在dbc中插入數據,則可以使用下面的方法:
fold = "d:\test.xls"
sheet = "AUCTION_BUYER_ANONY"
call InsertFromTable(devdbc,fold,sheet,"all")
原文轉自:http://www.taobaotesting.com/blogs/qa?bid=107