forked from vishnugopal/excel-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel-preview.c
118 lines (97 loc) · 2.44 KB
/
excel-preview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <assert.h>
#include <libxl.h>
#include <string.h>
#define HELP_TEXT "Usage: excel-preview /path/to/file.\n"
#define MAX_ROW 10
#define MAX_COL 10
void error(BookHandle* book)
{
const char* error = xlBookErrorMessage(*book);
printf("Error: %s\n", error);
}
void cell_contents_as_string(char* buffer, SheetHandle sheet, int row, int col) {
int cell_type = xlSheetCellType(sheet, row, col);
if(cell_type == CELLTYPE_EMPTY) {
strlcpy(buffer, "", 1);
} else if(cell_type == CELLTYPE_NUMBER) {
snprintf(buffer, 30, "%ld", (long)xlSheetReadNum(sheet, row, col, 0));
} else if (cell_type == CELLTYPE_STRING) {
strlcpy (buffer, xlSheetReadStr(sheet, row, col, 0), 300);
}
}
void print_cell_contents(SheetHandle sheet, int row, int col)
{
char buffer[300];
cell_contents_as_string(buffer, sheet, row, col);
if(*buffer && *buffer != '\0') {
printf("%s", buffer);
}
printf("\t");
}
void preview_sheet(BookHandle book, int sheet_number)
{
SheetHandle sheet = xlBookGetSheet(book, sheet_number);
if(sheet) {
int j, k;
printf("Sheet: %s\n", xlSheetName(sheet));
for(j = 0; j < MAX_ROW; j++) {
for(k = 0; k < MAX_COL; k++) {
print_cell_contents(sheet, j, k);
}
printf("\n");
}
}
}
void strtolower(char *str) {
while(*str != 0) {
if (*str >= 'A' && *str <= 'Z') {
*str = *str + 'a' - 'A';
}
str++;
}
}
BookHandle book_from_file_path(const char* file_path) {
char extension[3];
int length = strlen(file_path);
//Copy last four characters
strlcpy(extension, file_path + length - 4, 5);
strtolower(extension);
if(strncmp(extension, "xlsx", 4) == 0) {
return xlCreateXMLBook();
} else {
return xlCreateBook();
}
}
void preview(char* file_path)
{
book_from_file_path(file_path);
BookHandle book = book_from_file_path(file_path);
if(book) {
if(xlBookLoad(book, file_path)) {
int sheet_count, i;
sheet_count = xlBookSheetCount(book);
for(i = 0; i < sheet_count; i++) {
preview_sheet(book, i);
}
} else {
printf("Cannot load: %s\n", file_path);
printf(HELP_TEXT);
}
xlBookRelease(book);
}
}
int main(int argc, char *argv[])
{
char* file_path;
if(argc != 2) {
printf("Invalid arguments.\n");
printf(HELP_TEXT);
return 0;
}
// Get the file path
file_path = argv[1];
assert(file_path);
preview(file_path);
return 0;
}