Xbasic

excel_to_json Function

Syntax

C result = excel_to_json(C filename [, C filter [, C order [, C argumentsXML [, C sheetName [, L flagReturnArray [, L flagHTMLEncode]]]]]])

Arguments

filenameCharacter

The file name of the Excel file.

filterCharacter

Default = "". An optional SQL WHERE clause. Ignored if data is read from multiple sheets.

orderCharacter

Default = "". An optional SQL ORDER clause. Ignored if data is read from multiple sheets.

argumentsXMLCharacter

Default = "". Required if arguments are used in the filter.

sheetNameCharacter

Default = the first sheet. The name of the Excel sheet to read records from. If you do not specify a value, the first sheet in the file is assumed. You can specify a comma delimited list of sheets, or * (to get all sheets). If you specify * or more than one sheet, you cannot specify a filter or order and the result is a JSON array with data for each sheet.

flagReturnArrayLogical

Default = .f.. This property only applies when sheetName is set to "*". When set to .f., the function returns a JSON object where each sheet name is a property in the array. For example:

{
    "Sheet1": [
        {
            "ID": 1,
            "Name": "Janet"
        },
        {
            "ID": 2,
            "Name": "Steven"
        }
    ],
    "Sheet2": [
        {
            "ID": 1,
            "Name": "Maria"
        },
        {
            "ID": 2,
            "Name": "Taylor"
        }
    ]
}

If set to .t., data is returned as an array of objects. E.g.:

[
{
"sheetname": "Sheet1",
"json": "[\r\n{\r\n\t\"ID\": 1,\r\n\t\"Name\": \"Janet\"\r\n},\r\n{\r\n\t\"ID\": 2,\r\n\t\"Name\": \"Steven\"\r\n}]"
},
{
"sheetname": "Sheet2",
"json": "[\r\n{\r\n\t\"ID\": 1,\r\n\t\"Name\": \"Maria\"\r\n},\r\n{\r\n\t\"ID\": 2,\r\n\t\"Name\": \"Taylor\"\r\n}]"
}]
flagHTMLEncodeLogical

Internal Use Only

Returns

resultCharacter

Returns the Excel file as JSON array where each record is an object in the JSON array.

Description

Takes an Excel file and returns a string of JSON data.

Example

dim fn as c = "C:\excelfiles\myExcelDoc.xlsx";
dim filter as c = ""
dim order as c = "
dim xmlArgs as c = ""
dim sheet as c = "products"

dim result as c
result = excel_to_json(fn, filter, order, xmlArgs, sheet)

showvar(result)