Skip to content

Commit

Permalink
Merge pull request #118 from salvor-hardin/patch-2
Browse files Browse the repository at this point in the history
Optionally export sheet number and not sheet name
  • Loading branch information
brechtsanders authored May 25, 2024
2 parents 401f367 + b546404 commit fd58301
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions src/xlsxio_xlsx2csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct xlsx_data {
xlsxioreader xlsxioread;
FILE* dst;
int nobom;
int numbers;
int file_count;
const char* newline;
char separator;
char quote;
Expand Down Expand Up @@ -79,10 +81,25 @@ int xlsx_list_sheets_callback (const char* name, void* callbackdata)
if ((filename = (char*)malloc(strlen(data->filename) + strlen(name) + 6)) == NULL ){
fprintf(stderr, "Memory allocation error\n");
} else {

char str[16];

if (data->numbers)
{
//Vs writing the sheetname, this will write the number of the sheet !
snprintf(str, sizeof(str), "%d", data->file_count++);
}



//determine export filename
strcpy(filename, data->filename);
strcat(filename, ".");
strcat(filename, name);
if (data->numbers)
strcat(filename, str);
else
strcat(filename, name);

strcat(filename, ".csv");
//display status
printf("Sheet found: %s, exporting to: %s\n", name, filename);
Expand Down Expand Up @@ -113,6 +130,7 @@ void show_help ()
" -s separator\tspecify separator to use (default is comma)\n"
" -b \tdon't write UTF-8 BOM signature\n"
" -n \tuse UNIX style line breaks\n"
" -u \toutput sheet number instead of sheet name\n"
" xlsxfile \tpath to .xlsx file (multiple may be specified)\n"
"Description:\n"
"Converts all sheets in all specified .xlsx files to individual CSV (Comma Separated Values) files.\n"
Expand All @@ -124,10 +142,12 @@ void show_help ()
int main (int argc, char* argv[])
{
int i;
char* param;
char* param;
xlsxioreader xlsxioread;
struct xlsx_data sheetdata = {
.nobom = 0,
.numbers = 0,
.file_count = 1,
.newline = "\r\n",
.separator = ',',
.quote = '"',
Expand All @@ -139,30 +159,33 @@ int main (int argc, char* argv[])
}
for (i = 1; i < argc; i++) {
//check for command line parameters
if (argv[i][0] && (argv[i][0] == '/' || argv[i][0] == '-')) {
switch (tolower(argv[i][1])) {
if (argv[i][0] && (argv[i][0] == '/' || argv[i][0] == '-')) {
switch (tolower(argv[i][1])) {
case 'h' :
case '?' :
show_help();
case '?' :
show_help();
return 0;
case 's' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
case 's' :
if (argv[i][2])
param = argv[i] + 2;
else if (i + 1 < argc && argv[i + 1])
param = argv[++i];
else
param = NULL;
if (param)
sheetdata.separator = param[0];
param = NULL;
if (param)
sheetdata.separator = param[0];
continue;
case 'b' :
sheetdata.nobom = 1;
continue;
sheetdata.nobom = 1;
continue;
case 'u' :
sheetdata.numbers = 1;
continue;
case 'n' :
sheetdata.newline = "\n";
continue;
}
}
sheetdata.newline = "\n";
continue;
}
}
//open .xlsx file
if ((xlsxioread = xlsxioread_open(argv[i])) != NULL) {
sheetdata.xlsxioread = xlsxioread;
Expand Down

0 comments on commit fd58301

Please sign in to comment.