int main(int argc, char *argv[]) { xlsWorkBook* pWB; xlsWorkSheet* pWS; unsigned int i; int justList = 0; char *sheetName = ""; if(argc < 2) { Usage(argv[0]); } optind = 2; // skip file arg int ch; while ((ch = getopt(argc, argv, "lt:e:q:f:")) != -1) { switch (ch) { case 'l': justList = 1; break; case 'e': encoding = strdup(optarg); break; case 't': sheetName = strdup(optarg); break; case 'q': stringSeparator = optarg[0]; break; case 'f': fieldSeparator = strdup(optarg); break; default: Usage(argv[0]); break; } } struct st_row_data* row; WORD cellRow, cellCol; // open workbook, choose standard conversion pWB = xls_open(argv[1], encoding); if (!pWB) { fprintf(stderr, "File not found"); fprintf(stderr, "\n"); return EXIT_FAILURE; } // check if the requested sheet (if any) exists if (sheetName[0]) { for (i = 0; i < pWB->sheets.count; i++) { if (strcmp(sheetName, pWB->sheets.sheet[i].name) == 0) { break; } } if (i == pWB->sheets.count) { fprintf(stderr, "Sheet \"%s\" not found", sheetName); fprintf(stderr, "\n"); return EXIT_FAILURE; } } // process all sheets for (i = 0; i < pWB->sheets.count; i++) { int isFirstLine = 1; // just looking for sheet names if (justList) { printf("%s\n", pWB->sheets.sheet[i].name); continue; } // check if this the sheet we want if (sheetName[0]) { if (strcmp(sheetName, pWB->sheets.sheet[i].name) != 0) { continue; } } // open and parse the sheet pWS = xls_getWorkSheet(pWB, i); xls_parseWorkSheet(pWS); // process all rows of the sheet for (cellRow = 0; cellRow <= pWS->rows.lastrow; cellRow++) { int isFirstCol = 1; row = xls_row(pWS, cellRow); // process cells if (!isFirstLine) { printf("%s", lineSeparator); } else { isFirstLine = 0; } for (cellCol = 0; cellCol <= pWS->rows.lastcol; cellCol++) { //printf("Processing row=%d col=%d\n", cellRow+1, cellCol+1); xlsCell *cell = xls_cell(pWS, cellRow, cellCol); if ((!cell) || (cell->isHidden)) { continue; } if (!isFirstCol) { printf("%s", fieldSeparator); } else { isFirstCol = 0; } // display the colspan as only one cell, but reject rowspans (they can't be converted to CSV) if (cell->rowspan > 1) { fprintf(stderr, "Warning: %d rows spanned at col=%d row=%d: output will not match the Excel file.\n", cell->rowspan, cellCol+1, cellRow+1); } // display the value of the cell (either numeric or string) if (cell->id == 0x27e || cell->id == 0x0BD || cell->id == 0x203) { OutputNumber(cell->d); } else if (cell->id == 0x06) { // formula if (cell->l == 0) // its a number { OutputNumber(cell->d); } else { if (!strcmp(cell->str, "bool")) // its boolean, and test cell->d { OutputString((int) cell->d ? "true" : "false"); } else if (!strcmp(cell->str, "error")) // formula is in error { OutputString("*error*"); } else // ... cell->str is valid as the result of a string formula. { OutputString(cell->str); } } } else if (cell->str != NULL) { OutputString(cell->str); } else { OutputString(""); } } } xls_close_WS(pWS); } xls_close(pWB); return EXIT_SUCCESS; }
int process(char *filename) { xlsWorkBook* pWorkbook; xlsWorkSheet* pSheet; pWorkbook = xls_open(filename, "iso-8859-15//TRANSLIT"); if ( pWorkbook == NULL ) { fprintf(stderr, "Couldn't find %s.\n", filename); return EXIT_FAILURE; } // Iterate over the sheets for (int i = 0; i < pWorkbook->sheets.count; i++) { char *name = (char *)pWorkbook->sheets.sheet[i].name; char *filename = malloc(strlen(name)+5); // null and .csv sprintf(filename, "%s.csv", slugify(name)); FILE *fd = fopen(filename, "w"); pSheet = xls_getWorkSheet(pWorkbook, i); xls_parseWorkSheet(pSheet); for (int cellRow = 0; cellRow <= pSheet->rows.lastrow; cellRow++) { int isFirstCol = 1; for (int cellCol = 0; cellCol <= pSheet->rows.lastcol; cellCol++) { xlsCell *cell = xls_cell(pSheet, cellRow, cellCol); if ((!cell) || (cell->isHidden)) { continue; } if (!isFirstCol) { fprintf(fd, "%s", fieldSeparator); } else { isFirstCol = 0; } if (cell->rowspan > 1) { fprintf(stderr, "Warning: %d rows spanned at col=%d row=%d: output will not match the Excel file.\n", cell->rowspan, cellCol+1, cellRow+1); } // display the value of the cell (either numeric or string) if (cell->id == 0x27e || cell->id == 0x0BD || cell->id == 0x203) { fprintf(fd, "%.15g", cell->d); } else if (cell->id == 0x06) { if (cell->l == 0) { fprintf(fd, "%.15g", cell->d); } else { /* Handle macros */ if (!strcmp((char *)cell->str, "bool")) { write_string(fd, (int) cell->d ? "True" : "False"); } else if (!strcmp((char *)cell->str, "error")) { write_string(fd, "*error*"); } else { write_string(fd, (char *)cell->str); } } } else if (cell->str != NULL) { write_string(fd, (char *)cell->str); } else { write_string(fd, ""); } } fprintf(fd, "\n"); } fclose(fd); free(filename); } xls_close(pWorkbook); return EXIT_SUCCESS; }