Sample code for 30+ languages & platforms
Tcl

XLSX Spreadsheet in AI Query

See more AI Examples

This example shows how to convert a .xlsx spreadsheet to CSV text for input in an AI query. Currently, most AIs can't handle Excel file inputs directly. If the spreadsheet is small, you can convert it to CSV text and use it as a text input.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

# .xlsx files are Zip archives
set zip [new_CkZip]

set success [CkZip_OpenZip $zip "qa_data/excel/fakeCompanies.xlsx"]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    exit
}

set csv [new_CkCsv]

set sheetNames [new_CkStringTable]

set success [CkCsv_XlsxGetSheets $csv $zip $sheetNames]
if {$success == 0} then {
    puts [CkCsv_lastErrorText $csv]
    delete_CkZip $zip
    delete_CkCsv $csv
    delete_CkStringTable $sheetNames
    exit
}

if {[CkStringTable_get_Count $sheetNames] == 0} then {
    puts "There are no sheets in the .xlsx"
    delete_CkZip $zip
    delete_CkCsv $csv
    delete_CkStringTable $sheetNames
    exit
}

# Get the name of the 1st sheet.
set sheetName [CkStringTable_stringAt $sheetNames 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.
set success [CkCsv_XlsxLoadSheet $csv $zip $sheetName]
if {$success == 0} then {
    puts [CkZip_lastErrorText $zip]
    delete_CkZip $zip
    delete_CkCsv $csv
    delete_CkStringTable $sheetNames
    exit
}

set sbCsv [new_CkStringBuilder]

CkCsv_SaveToSb $csv $sbCsv

# ------------------------------------------
set ai [new_CkAi]

CkAi_put_Provider $ai "openai"

# Use your provider's API key.
CkAi_put_ApiKey $ai "MY_API_KEY"

# Choose a model.
CkAi_put_Model $ai "gpt-4o"

# Add text inputs
CkAi_InputAddText $ai "Describe what is contained in the following CSV data."
CkAi_InputAddTextSb $ai $sbCsv

# Ask the AI for text output.
set success [CkAi_Ask $ai "text"]
if {$success == 0} then {
    puts [CkAi_lastErrorText $ai]
    delete_CkZip $zip
    delete_CkCsv $csv
    delete_CkStringTable $sheetNames
    delete_CkStringBuilder $sbCsv
    delete_CkAi $ai
    exit
}

# Get the text response.
set sbResponse [new_CkStringBuilder]

CkAi_GetOutputTextSb $ai $sbResponse
puts [CkStringBuilder_getAsString $sbResponse]

# Sample output:

# The CSV data contains information about five companies, including the following fields:
# 
# 1. **CompanyName**: The name of the company.
# 2. **Address**: The street address of the company.
# 3. **City**: The city where the company is located.
# 4. **State**: The state where the company is located, abbreviated.
# 5. **Zip**: The ZIP code for the company's location.
# 6. **Phone**: The phone number of the company.
# 
# Each row in the dataset corresponds to a different company with details provided for each of these fields:
# 
# ...
# ...

# -------------------------------------------------------------
# The response is in markdown format.
# Also see Markdown to HTML Conversion Examples.
# -------------------------------------------------------------

delete_CkZip $zip
delete_CkCsv $csv
delete_CkStringTable $sheetNames
delete_CkStringBuilder $sbCsv
delete_CkAi $ai
delete_CkStringBuilder $sbResponse