Monday, May 4, 2009

Membuat File Excel Dari Program dengan VB

Contoh Program ini Digunakan untuk membuat file MS Excel menggunakan coding
Persiapan :
1. Buat 1 project baru dengan 1 form dan 1 commandbutton
2. Tambahkan reference dari project - > References
3. Microsoft Excel X.0 Object Library, lalu klik ok
4. Ketik coding berikut ke dalam editor dan module yang bersangkutan

Ketik coding berikut pada form

Private Sub Command1_Click()
Dim xlApp As Excel.Aplication
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet

Set xlApp = New Excel.Aplication
Set xlWB = xlApp.workbooks.Add
Set xlWS = xl WB.Worksheets.Add

‘Baris berikut ini akan mengisi cell (2,2) dengan tulisan “Hello”
‘dan akan mengisi cel (1,3) dengan tulisan “Word”
xlWS.Cells(2,2).Value = “hello”
xlWS.cells(1,3).value = “word”
“C:\mysheet.xls”.
xlWS.SaveAs “c:\mysheet.xls”
xlApp.Quit

‘Bebaskan Memory
Set xlWS = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

No comments:

Post a Comment