VB. Baca Excel dengan late-binding

Dengan late binding, tidak perlu tau Excel versi berapa yang ada di PC klien. Dan tidak perlu reference di projectnya.

silakan simak snapshotnya.
'late binding
Dim wb As Object
Dim rg As Object
Dim ws1 As Object

'Set Xfile = New Excel.Application
If bXLSalreadyExist = False Then
Set Xfile = CreateObject("Excel.Application")
bXLSalreadyExist = True
End If

Xfile.Workbooks.Add
Xfile.Workbooks(1).SaveAs sFile

Xfile.Workbooks.Open sFile ', , , , , , , , , , False '
Set ws1 = Xfile.Worksheets.Item("Sheet1")
Dim i As Integer
i = 2

'Get The FieldName: HEADER
'Field Account No, STock Code, CHCode
Set rg = ws1.Range("A1")
rg.Value = "Account Code"
Set rg = ws1.Range("B1")
rg.Value = "StockCode"
Set rg = ws1.Range("C1")
rg.Value = "CH Code"
Set rg = ws1.Range("D1")
rg.Value = "Rec Date"
Set rg = ws1.Range("E1")
rg.Value = "Stock Portfolio"
Set rg = ws1.Range("F1")
rg.Value = "Stock On Hand"
Set rg = ws1.Range("G1")
rg.Value = "Stock Availability"
Set rg = ws1.Range("H1")
rg.Value = "For Trade Settlement"


If rsDownload.RecordCount <> 0 Then
rsDownload.MoveFirst
While Not rsDownload.EOF

'F: Account No
Set rg = ws1.Range("A" & i)
rg.Value = rsDownload.Fields(0).Value
'F: Stock Code
Set rg = ws1.Range("B" & i)
rg.Value = rsDownload.Fields(1).Value
'F: CHCode
Set rg = ws1.Range("C" & i)
rg.Value = rsDownload.Fields(2).Value
'F: Rec Date
Set rg = ws1.Range("D" & i)
rg.Value = rsDownload.Fields(3).Value
'F: Stock Portfolio
Set rg = ws1.Range("E" & i)
rg.Value = rsDownload.Fields(4).Value
'F: Stock StockOnHand
Set rg = ws1.Range("F" & i)
rg.Value = rsDownload.Fields(5).Value
'F: Stock Stock Availability
Set rg = ws1.Range("G" & i)
rg.Value = rsDownload.Fields(6).Value
'F: ForTrdSettlement
Set rg = ws1.Range("H" & i)
rg.Value = rsDownload.Fields(7).Value

rsDownload.MoveNext
i = i + 1
Wend
End If
Xfile.Workbooks(1).Save
Xfile.Workbooks.Close



gitu aja kok.
wid, 2008 februari.

No comments:

Post a Comment