#include #include #include #include /* link this against libxlsreader */ /* Print a string */ void OutputString(const char *string) { const char *str; const char sep = '"'; printf("%c", sep); for (str = string; *str; str++) { if (*str == sep) printf("\\%c", sep); else if (*str == '\\') { printf("\\\\"); } else { printf("%c", *str); } } printf("%c", sep); } void print_cell(xlsCell *cell) { /* Numeric */ if (cell->id == 0x27e || cell->id == 0x0BD || cell->id == 0x203) printf("%.15g", cell->d); /* Formula */ else if (cell->id == 0x06) { /* Numeric Formula */ if (cell->l == 0) printf("%.15g", cell->d); else { /* Boolean Formula */ if (strcmp(cell->str,"bool")) OutputString((int) cell->d ? "true" : "false"); /* Error Formula */ else if (strcmp(cell->str,"error")) OutputString("*error*"); /* Probably a String Formula */ else OutputString(cell->str); } } /* String? */ else if (cell->str != NULL) OutputString(cell->str); /*Empty Cell*/ else OutputString(""); } int main() { char *filename = "test.xls"; xlsWorkBook* pWB; xlsWorkSheet* pWS; int i,j,num_rows,num_cols; /* Open the workbook */ pWB = xls_open(filename, "ASCII"); /* Open the first sheet and parse it. * don't care about the rest yet */ pWS = xls_getWorkSheet(pWB, 0); xls_parseWorkSheet(pWS); num_rows = pWS->rows.lastrow; num_cols = pWS->rows.lastcol; for (i = 0; i <= num_rows; i++) { for (j = 0; j <= num_cols; j++) { xlsCell *cell = xls_cell(pWS, i, j); /*Ignore missing or invisibles cells*/ if ((!cell) || (cell->ishiden)) continue; print_cell(cell); /* Don't print a comma after last cel */ if (j < num_cols - 1) printf(","); } printf("\n"); } return 0; }