Xbase++ Requires Chilkat v11.3.0+
Xbase++
XLSX Convert Sheet to CSV
Open an Excel spreadsheet (.xlsx) and loads a sheet into a CSV object.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oZip
LOCAL oCsv
LOCAL oSheetNames
LOCAL cSheetName
LOCAL cCsvStr
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// .xlsx files are Zip archives
oZip := CreateObject("Chilkat.Zip")
nSuccess := oZip:OpenZip("qa_data/excel/fakeCompanies.xlsx")
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
oCsv := CreateObject("Chilkat.Csv")
oSheetNames := CreateObject("Chilkat.StringTable")
nSuccess := oCsv:XlsxGetSheets(oZip, oSheetNames)
IF (nSuccess == 0)
? oCsv:LastErrorText
oZip:destroy()
oCsv:destroy()
oSheetNames:destroy()
RETURN
ENDIF
IF (oSheetNames:Count == 0)
? "There are no sheets in the .xlsx"
oZip:destroy()
oCsv:destroy()
oSheetNames:destroy()
RETURN
ENDIF
// Get the name of the 1st sheet.
cSheetName := oSheetNames:StringAt(0)
// Load the 1st sheet into the CSV.
// We could've also loaded the 1st sheet by passing an empty string for the sheet name.
nSuccess := oCsv:XlsxLoadSheet(oZip, cSheetName)
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
oCsv:destroy()
oSheetNames:destroy()
RETURN
ENDIF
// The Chilkat CSV API can now be used to read, modify, or insert cells.
// ...
// ...
cCsvStr := oCsv:SaveToString()
? cCsvStr
// Sample output:
// CompanyName,Address,City,State,Zip,Phone
// Acme Innovations LLC,7428 Westbrook Blvd,Oak Bend,IL,60514,630-555-2398
// BlueHorizon Data Systems,1850 Crestline Park Suite 210,Rivergate,CA,92814,714-555-8821
// Pinnacle Manufacturing Corp,3912 Industrial Loop,Maple Ridge,TX,77301,281-555-4477
// Evergreen Analytics Inc,1250 Highland Terrace,Stonefield,WA,98290,425-555-9914
// NovaTech Solutions Group,578 Marketview Drive,Clearwater,FL,33760,727-555-6032
// Save to a .csv file.
nSuccess := oCsv:SaveFile2("c:/temp/qa_output/fakeCompanies.csv", "utf-8")
IF (nSuccess == 0)
? oCsv:LastErrorText
oZip:destroy()
oCsv:destroy()
oSheetNames:destroy()
RETURN
ENDIF
oZip:destroy()
oCsv:destroy()
oSheetNames:destroy()