Simple Trick : How to export data to Excel (CSV file)

Posted at : Jul/20/2007
10654 Views

Ada cara yang mudah untuk melakukan export data ke file excel yang merupakan Comma Separated Value(CSV) file. Hal tersebut dapat dilakukan dengan menggunakan class Process dengan method Start. Sekali kita panggil method Start dari class Process dengan mengirimkan parameter berupa file dengan ekstensi .csv maka saat itu juga aplikasi akan memanggil Excel apabila diinstal di komputer yang bersangkutan dengan content yang sudah kita tentukan.

Langkah-langkah yang harus dilakukan yaitu :

1. Buat sebuah variable string yang digunakan untuk menyimpan file excel (.csv) di lokasi direktori tertentu.

   1:  '//create csv file
   2:  Dim MycsvFile As String = My.Application.Info.DirectoryPath & _
   3:        "\myExcel.csv"

2. Buat sebuah object StreamWriter untuk menuliskan output ke file .csv.

   1:  '//data to export to excel
   2:  Dim dataToWrite As IO.StreamWriter = _
   3:  My.Computer.FileSystem.OpenTextFileWriter(MycsvFile, True)

3. Gunakan method WriteLine dari langkah ke dua dengan passing parameter berupa nama kolom yang akan di export beserta valuenya :

   1:  dataToWrite.WriteLine("No.ID, Product Name, Unit Price")
   2:  dataToWrite.WriteLine("P001, Mouse, 2500")
   3:  dataToWrite.WriteLine("P002, Keyboard, 1500")
   4:  dataToWrite.Close()

4. Panggil method Start dari class Process dengan passing parameter berupa variable yang telah dibuat di langkah pertama :

   1:  '//display its content to messagebox
   2:  MsgBox(My.Computer.FileSystem.ReadAllText(MycsvFile))
   3:   
   4:  '//display its contents to excel
   5:  Process.Start(MycsvFile)

5. Code lengkapnya seperti berikut ini :

   1:  Public Class frmDataToExcel
   2:   
   3:      Private Sub frmDataToExcel_Load(ByVal sender As System.Object, _
   4:      ByVal e As System.EventArgs) Handles MyBase.Load
   5:          '//create csv file
   6:          Dim MycsvFile As String = My.Application.Info.DirectoryPath & _
   7:             "\myExcel.csv"
   8:   
   9:          '//data to export to excel
  10:          Dim dataToWrite As IO.StreamWriter = _
  11:             My.Computer.FileSystem.OpenTextFileWriter(MycsvFile, True)
  12:   
  13:          dataToWrite.WriteLine("No.ID, Product Name, Unit Price")
  14:          dataToWrite.WriteLine("P001, Mouse, 2500")
  15:          dataToWrite.WriteLine("P002, Keyboard, 1500")
  16:          dataToWrite.Close()
  17:   
  18:          '//display its content to messagebox
  19:          MsgBox(My.Computer.FileSystem.ReadAllText(MycsvFile))
  20:   
  21:          '//display its contents to excel
  22:          Process.Start(MycsvFile)
  23:      End Sub
  24:   
  25:  End Class

 


ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Co-Founder & IT Trainer at Native Enterprise | Microsoft Azure Data Scientist | IBM RAG & Agentic AI | IBM Data Science & Data Analyst | Python Certified (PCEP, PCAP) | MOS, MTA, Xamarin Certified, ex MCT | ex MVP

CERTIFICATIONS

Microsoft Certified Associate
IBM RAG and Agentic AI Professional
IBM Data Science Professional IBM Data Analyst Professional
PCAP Associate Python Programmer Certified PCEP Entry Level Python Programmer Certified
Xamarin Certified
MOS 2007
MCPD MCTS
MCAD.NET

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  X Twitter Facebook  Instagram  LinkedIn

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2026   Rully Yulian MF   All rights reserved.