xl2sql a simple tool that takes an .xls file as input, and generates a SQL script as output. You can then use the SQL script to load the extracted data info a SQLite / SpatiaLite database.
The first command will parse the .xls document, extracting any data and generating the corresponding SQL script. The second command will create and populate a database from the SQL script. When using xl2sql this way, the first worksheet will become database table xl_table_00, the second worksheet will become database table xl_table_01 and so on.
As an alternative, if you pass a second argument to xl2sql, this argument will be used as the table prefix. For example:
This will result in the tables being named italia_00, italia_01 and so on.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static void
make_table_name (const char *prefix, unsigned short index, char *table_name)
{
char buf[2048];
char *in = buf;
char *out = table_name;
sprintf (buf, "%s_%02u", prefix, index);
*out++ = '"';
while (*in != '\0')
{
if (*in == '"')
*out++ = '"';
*out++ = *in++;
}
*out++ = '"';
*out = '\0';
}
static void
print_sql_string (const char *string)
{
const char *p = string;
putchar (',');
putchar (' ');
putchar ('\'');
while (*p != '\0')
{
if (*p == '\'')
{
putchar ('\'');
}
putchar (*p);
p++;
}
putchar ('\'');
}
int
main (int argc, char *argv[])
{
unsigned int worksheet_index;
const char *table_prefix = "xl_table";
char table_name[2048];
const void *handle;
int ret;
unsigned int info;
unsigned int max_worksheet;
unsigned int rows;
unsigned short columns;
unsigned int row;
unsigned short col;
if (argc == 2 || argc == 3)
{
if (argc == 3)
table_prefix = argv[2];
}
else
{
fprintf (stderr, "usage: xl2sql path.xls [table_prefix]\n");
return -1;
}
{
fprintf (stderr, "OPEN ERROR: %d\n", ret);
return -1;
}
{
fprintf (stderr, "GET-INFO [FREEXL_BIFF_PASSWORD] Error: %d\n", ret);
goto stop;
}
switch (info)
{
break;
default:
fprintf (stderr, "Password protected: (not accessible)\n");
goto stop;
};
{
fprintf (stderr, "GET-INFO [FREEXL_BIFF_SHEET_COUNT] Error: %d\n",
ret);
goto stop;
}
printf ("--\n-- this SQL script was automatically created by xl2sql\n");
printf ("--\n-- input .xls document was: %s\n--\n", argv[1]);
printf ("\nBEGIN;\n\n");
for (worksheet_index = 0; worksheet_index < max_worksheet;
worksheet_index++)
{
const char *utf8_worsheet_name;
make_table_name (table_prefix, worksheet_index, table_name);
ret =
&utf8_worsheet_name);
{
fprintf (stderr, "GET-WORKSHEET-NAME Error: %d\n", ret);
goto stop;
}
{
fprintf (stderr, "SELECT-ACTIVE_WORKSHEET Error: %d\n", ret);
goto stop;
}
{
fprintf (stderr, "WORKSHEET-DIMENSIONS Error: %d\n", ret);
goto stop;
}
printf ("--\n-- creating a DB table\n");
printf ("-- extracting data from Worksheet #%u: %s\n--\n",
worksheet_index, utf8_worsheet_name);
printf ("CREATE TABLE %s (\n", table_name);
printf ("\trow_no INTEGER NOT NULL PRIMARY KEY");
for (col = 0; col < columns; col++)
printf (",\n\tcol_%03u MULTITYPE", col);
printf (");\n");
printf ("--\n-- populating the same table\n--\n");
for (row = 0; row < rows; row++)
{
printf ("INSERT INTO %s (row_no", table_name);
for (col = 0; col < columns; col++)
printf (", col_%03u", col);
printf (") VALUES (%u", row);
for (col = 0; col < columns; col++)
{
{
fprintf (stderr,
"CELL-VALUE-ERROR (r=%u c=%u): %d\n", row,
col, ret);
goto stop;
}
{
break;
break;
break;
break;
default:
printf (", NULL");
break;
};
}
printf (");\n");
}
printf ("\n-- done: table end\n\n\n\n");
}
printf ("COMMIT;\n");
stop:
{
fprintf (stderr, "CLOSE ERROR: %d\n", ret);
return -1;
}
return 0;
}