Sekedar berbagi ilmu, sapa tau ada yang butuh, di bawah ini ada sintak VB buat baca dan nulis data dari dan ke file Excel(.xls). Langsung aja kita mulai, ini dia step by step
1. Bikin project baru di VB
2. Buat referensi dari VB ke library untuk Excel, caranya dari menu
Project–>References trus kasi tanda cek di
Microsoft Excel xx.x (versinya, aku pake 12.0) Object Library, trus klik
OK
3. Deklarasikan di variabel global (boleh pake module ato di form, tapi sebaiknya di module aja biar lebih gampang dikelola)
1
| Public xlApp As Excel.Application ' Excel Application Object
|
2
| Public xlBook As Excel.Workbook ' Excel Workbook Object
|
4. Nah, ini sintak Function untuk baca data dari file Excel:
01
| Public Function GetExcel(xlFileName As String, xlWorksheet As String, _
|
02
| xlCellName As String) As String
|
03
| On Error GoTo GetExcel_Err 'eror handler
|
04
| Dim strCellContents As String
|
05
| 'inisiasi Excel App Object
|
06
| Set xlApp = CreateObject("Excel.Application")
|
07
| 'inisiasi Excel Workbook Object.
|
08
| Set xlBook = xlApp.Workbooks.Open(xlFileName)
|
09
| 'membaca isi cell pada spreadsheet
|
10
| strCellContents = xlBook.worksheets(xlWorksheet).range(xlCellName).Value
|
11
| 'tutup spreadsheet
|
12
| xlBook.Close savechanges:=False
|
13
| xlApp.Quit
|
14
| Set xlApp = Nothing
|
15
| Set xlBook = Nothing
|
16
| GetExcel = strCellContents 'mengembalikan nilai
|
19
| GetExcel_Err:
|
20
| MsgBox "GetExcel Error: " & Err.Number & "-" & Err.Description
|
21
| Resume Next
|
22
| End Function
|
5. Kalo yang di bawah ini sintak Procedure untuk nulis data ke file Excel:
01
| Public Sub SetExcel(xlFileName As String, _
|
02
| xlWorksheet As String, _
|
03
| xlCellName As String, _
|
04
| xlCellContents As String)
|
05
|
|
06
| On Error GoTo SetExcel_Err 'eror handler
|
07
|
|
08
| ' inisiasi Excel App Object
|
09
| Set xlApp = CreateObject("Excel.Application")
|
10
|
|
11
| ' inisiasi Excel Workbook Object.
|
12
| Set xlBook = xlApp.Workbooks.Open(xlFileName)
|
13
|
|
14
| ' mengisi nilai tertentu ke cell tujuan
|
15
| xlBook.worksheets(xlWorksheet).range(xlCellName).Value = xlCellContents
|
16
|
|
17
| ' menyimpan file dan menutup spreadsheet
|
18
| xlBook.Save
|
19
| xlBook.Close savechanges:=False
|
20
| xlApp.Quit
|
21
| Set xlApp = Nothing
|
22
| Set xlBook = Nothing
|
23
| Exit Sub
|
24
| SetExcel_Err:
|
25
| MsgBox "SetExcel Error: " & Err.Number & "-" & Err.Description
|
26
| Resume Next
|
Function dan Procedure di atas boleh disimpan di module ataupun di form namun sebaiknya disimpan di module agar lebih mudah dikelola.
Note:
- xlFilename = nama file excelnya, tipenya string, misal “F:Tugas KuliahData.xls”
- xlWorksheet = nama sheet pada spreadsheet yg akan dibaca/ditulis datanya, tipenya string, misal “Sheet1″ atau “Pengeluaran”
- xlCellName = nama cell yang akan dibaca/ditulis datanya, misal “A1″,”JW23″ dst
- xlCellContent = data yang akan ditulis ke file Excel, tipenya string, misal “Wahyu”, “23″ dsb
Contoh Penggunaan:
1
| 'membaca file Excel dan meyimpan hasilnya ke Text1
|
2
| Text1.text = GetExcel("F:\Tugas Kuliah\Data.xls","Sheet1","A1")
|
3
| 'membaca isi Text1 dan dsimpan ke file Excel
|
4
| SetExcel "F:\Tugas Kuliah\Data.xls","Sheet1","A1",Text1.text
|
Any Question so far? Semoga bermanfaat…
Tidak ada komentar:
Posting Komentar