Sample code for 30+ languages & platforms
Xbase++

Create CSV File

See more CSV Examples

Demonstrates how to create a new CSV file with some simple content.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCsv
LOCAL cCsvDoc

nSuccess := 0

//  This sample code creates a new CSV file (sample.csv)
//  that contains this content:
//  
//  year,color,country,food
//  2001,red,France,cheese
//  2005,blue,"United States",hamburger

oCsv := CreateObject("Chilkat.Csv")

//  Indicate that the 1st row
//  should be treated as column names:
oCsv:HasColumnNames := 1

nSuccess := oCsv:SetColumnName(0, "year")
nSuccess := oCsv:SetColumnName(1, "color")
nSuccess := oCsv:SetColumnName(2, "country")
nSuccess := oCsv:SetColumnName(3, "food")

nSuccess := oCsv:SetCell(0, 0, "2001")
nSuccess := oCsv:SetCell(0, 1, "red")
nSuccess := oCsv:SetCell(0, 2, "France")
nSuccess := oCsv:SetCell(0, 3, "cheese")

nSuccess := oCsv:SetCell(1, 0, "2005")
nSuccess := oCsv:SetCell(1, 1, "blue")
nSuccess := oCsv:SetCell(1, 2, "United States")
nSuccess := oCsv:SetCell(1, 3, "hamburger")

//  Write the CSV to a string and display:

cCsvDoc := oCsv:SaveToString()
? cCsvDoc

//  Save the CSV to a file:
nSuccess := oCsv:SaveFile("out.csv")
IF (nSuccess != 1)
    ? oCsv:LastErrorText
ENDIF

oCsv:destroy()