File: tables.c

package info (click to toggle)
libxlsxwriter 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,888 kB
  • sloc: ansic: 65,861; python: 2,106; makefile: 693; perl: 229; sh: 224; xml: 168; cpp: 73; javascript: 5
file content (454 lines) | stat: -rw-r--r-- 17,581 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * An example of how to add conditional formatting to an libxlsxwriter file.
 *
 * Conditional formatting allows you to apply a format to a cell or a
 * range of cells based on certain criteria.
 *
 * Copyright 2014-2026, John McNamara, jmcnamara@cpan.org
 *
 */

#include "xlsxwriter.h"

void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *format);

int main() {

    lxw_workbook  *workbook    = workbook_new("tables.xlsx");
    lxw_worksheet *worksheet1  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet2  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet3  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet4  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet5  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet6  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet7  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet8  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet9  = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet10 = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet11 = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet12 = workbook_add_worksheet(workbook, NULL);
    lxw_worksheet *worksheet13 = workbook_add_worksheet(workbook, NULL);

    lxw_format *currency_format = workbook_add_format(workbook);
    format_set_num_format(currency_format, "$#,##0");

    /*
     * Example 1. Default table with no data
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet1, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet1, CELL("B1"), "Default table with no data.", NULL);

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet1, RANGE("B3:F7"), NULL);

    /*
     * Example 2. Default table with data
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet2, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet2, CELL("B1"), "Default table with data.", NULL);

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet2, RANGE("B3:F7"), NULL);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet2, NULL);

    /*
     * Example 3. Table without default autofilter
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet3, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet3, CELL("B1"), "Table without default autofilter.", NULL);

    /* Set the table options. */
    lxw_table_options options3 = {.no_autofilter = LXW_TRUE};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet3, RANGE("B3:F7"), &options3);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet3, NULL);


    /*
     * Example 4. Table without default header row
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet4, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet4, CELL("B1"), "Table without default header row.", NULL);

    /* Set the table options. */
    lxw_table_options options4 = {.no_header_row = LXW_TRUE};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet4, RANGE("B4:F7"), &options4);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet4, NULL);


    /*
     * Example 5. Default table with "First Column" and "Last Column" options
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet5, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet5, CELL("B1"),
                           "Default table with \"First Column\" and \"Last Column\" options.",
                           NULL);

    /* Set the table options. */
    lxw_table_options options5 = {.first_column = LXW_TRUE, .last_column = LXW_TRUE};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet5, RANGE("B3:F7"), &options5);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet5, NULL);


    /*
     * Example 6. Table with banded columns but without default banded rows
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet6, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet6, CELL("B1"),
                           "Table with banded columns but without default banded rows.",
                           NULL);

    /* Set the table options. */
    lxw_table_options options6 = {.no_banded_rows = LXW_TRUE, .banded_columns = LXW_TRUE};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet6, RANGE("B3:F7"), &options6);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet6, NULL);


    /*
     * Example 7. Table with user defined column headers
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet7, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet7, CELL("B1"), "Table with user defined column headers.", NULL);


    /* Set the table options. */
    lxw_table_column col7_1 = {.header = "Product"};
    lxw_table_column col7_2 = {.header = "Quarter 1"};
    lxw_table_column col7_3 = {.header = "Quarter 2"};
    lxw_table_column col7_4 = {.header = "Quarter 3"};
    lxw_table_column col7_5 = {.header = "Quarter 4"};

    lxw_table_column *columns7[] = {&col7_1, &col7_2, &col7_3, &col7_4, &col7_5, NULL};

    lxw_table_options options7 = {.columns = columns7};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet7, RANGE("B3:F7"), &options7);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet7, NULL);


    /*
     * Example 8. Table with user defined column headers
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet8, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet8, CELL("B1"), "Table with user defined column headers.", NULL);

    /* Set the table options. */
    lxw_table_column col8_1 = {.header = "Product"};
    lxw_table_column col8_2 = {.header = "Quarter 1"};
    lxw_table_column col8_3 = {.header = "Quarter 2"};
    lxw_table_column col8_4 = {.header = "Quarter 3"};
    lxw_table_column col8_5 = {.header = "Quarter 4"};
    lxw_table_column col8_6 = {.header = "Year",
                               .formula = "=SUM(Table8[@[Quarter 1]:[Quarter 4]])"};

    lxw_table_column *columns8[] = {&col8_1, &col8_2, &col8_3, &col8_4, &col8_5, &col8_6, NULL};

    lxw_table_options options8 = {.columns = columns8};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet8, RANGE("B3:G7"), &options8);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet8, NULL);


    /*
     * Example 9. Table with totals row (but no caption or totals)
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet9, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet9, CELL("B1"),
                           "Table with totals row (but no caption or totals).",
                           NULL);


    /* Set the table options. */
    lxw_table_column col9_1 = {.header = "Product"};
    lxw_table_column col9_2 = {.header = "Quarter 1"};
    lxw_table_column col9_3 = {.header = "Quarter 2"};
    lxw_table_column col9_4 = {.header = "Quarter 3"};
    lxw_table_column col9_5 = {.header = "Quarter 4"};
    lxw_table_column col9_6 = {.header = "Year",
                               .formula = "=SUM(Table9[@[Quarter 1]:[Quarter 4]])"};

    lxw_table_column *columns9[] = {&col9_1, &col9_2, &col9_3, &col9_4, &col9_5, &col9_6, NULL};

    lxw_table_options options9 = {.total_row = LXW_TRUE, .columns = columns9};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet9, RANGE("B3:G8"), &options9);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet9, NULL);


    /*
     * Example 10. Table with totals row with user captions and functions
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet10, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet10, CELL("B1"),
                           "Table with totals row with user captions and functions.",
                           NULL);

    /* Set the table options. */
    lxw_table_column col10_1 = {.header         = "Product",
                                .total_string   = "Totals"};

    lxw_table_column col10_2 = {.header         = "Quarter 1",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col10_3 = {.header         = "Quarter 2",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col10_4 = {.header         = "Quarter 3",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col10_5 = {.header         = "Quarter 4",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col10_6 = {.header         = "Year",
                                .formula        = "=SUM(Table10[@[Quarter 1]:[Quarter 4]])",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column *columns10[] = {&col10_1, &col10_2, &col10_3, &col10_4,
                                     &col10_5, &col10_6, NULL};

    lxw_table_options options10 = {.total_row = LXW_TRUE, .columns = columns10};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet10, RANGE("B3:G8"), &options10);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet10, NULL);


    /*
     * Example 11. Table with alternative Excel style
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet11, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet11, CELL("B1"), "Table with alternative Excel style.", NULL);

    /* Set the table options. */
    lxw_table_column col11_1 = {.header         = "Product",
                                .total_string   = "Totals"};

    lxw_table_column col11_2 = {.header         = "Quarter 1",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col11_3 = {.header         = "Quarter 2",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col11_4 = {.header         = "Quarter 3",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col11_5 = {.header         = "Quarter 4",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col11_6 = {.header         = "Year",
                                .formula        = "=SUM(Table11[@[Quarter 1]:[Quarter 4]])",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column *columns11[] = {&col11_1, &col11_2, &col11_3, &col11_4,
                                     &col11_5, &col11_6, NULL};

    lxw_table_options options11 = {
        .style_type = LXW_TABLE_STYLE_TYPE_LIGHT,
        .style_type_number = 11,
        .total_row = LXW_TRUE,
        .columns = columns11
    };

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet11, RANGE("B3:G8"), &options11);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet11, NULL);

    /*
     * Example 12. Table with Excel style removed
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet12, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet12, CELL("B1"), "Table with Excel style removed.", NULL);

    /* Set the table options. */
    lxw_table_column col12_1 = {.header         = "Product",
                                .total_string   = "Totals"};

    lxw_table_column col12_2 = {.header         = "Quarter 1",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col12_3 = {.header         = "Quarter 2",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col12_4 = {.header         = "Quarter 3",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col12_5 = {.header         = "Quarter 4",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column col12_6 = {.header         = "Year",
                                .formula        = "=SUM(Table12[@[Quarter 1]:[Quarter 4]])",
                                .total_function = LXW_TABLE_FUNCTION_SUM};

    lxw_table_column *columns12[] = {&col12_1, &col12_2, &col12_3, &col12_4,
                                     &col12_5, &col12_6, NULL};

    lxw_table_options options12 = {
        .style_type = LXW_TABLE_STYLE_TYPE_LIGHT,
        .style_type_number = 0,
        .total_row = LXW_TRUE,
        .columns = columns12
    };

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet12, RANGE("B3:G8"), &options12);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet12, NULL);

    /*
     * Example 13. Table with column formats
     */

    /* Set the columns widths for clarity. */
    worksheet_set_column(worksheet13, COLS("B:G"), 12, NULL);

    /* Write the worksheet caption to explain the example. */
    worksheet_write_string(worksheet13, CELL("B1"), "Table with column formats.", NULL);

    /* Set the table options. */
    lxw_table_column col13_1 = {.header         = "Product",
                                .total_string   = "Totals"};

    lxw_table_column col13_2 = {.header         = "Quarter 1",
                                .total_function = LXW_TABLE_FUNCTION_SUM,
                                .format         = currency_format};

    lxw_table_column col13_3 = {.header         = "Quarter 2",
                                .total_function = LXW_TABLE_FUNCTION_SUM,
                                .format         = currency_format};

    lxw_table_column col13_4 = {.header         = "Quarter 3",
                                .total_function = LXW_TABLE_FUNCTION_SUM,
                                .format         = currency_format};

    lxw_table_column col13_5 = {.header         = "Quarter 4",
                                .total_function = LXW_TABLE_FUNCTION_SUM,
                                .format         = currency_format};

    lxw_table_column col13_6 = {.header         = "Year",
                                .formula        = "=SUM(Table13[@[Quarter 1]:[Quarter 4]])",
                                .total_function = LXW_TABLE_FUNCTION_SUM,
                                .format         = currency_format};

    lxw_table_column *columns13[] = {&col13_1, &col13_2, &col13_3, &col13_4,
                                     &col13_5, &col13_6, NULL};

    lxw_table_options options13 = {.total_row = LXW_TRUE, .columns = columns13};

    /* Add a table to the worksheet. */
    worksheet_add_table(worksheet13, RANGE("B3:G8"), &options13);

    /* Write the data into the worksheet cells. */
    write_worksheet_data(worksheet13, currency_format);


    return workbook_close(workbook);
}

/* Write some data to the worksheet. */
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *format) {

    worksheet_write_string(worksheet, CELL("B4"), "Apples",  NULL);
    worksheet_write_string(worksheet, CELL("B5"), "Pears",   NULL);
    worksheet_write_string(worksheet, CELL("B6"), "Bananas", NULL);
    worksheet_write_string(worksheet, CELL("B7"), "Oranges", NULL);

    worksheet_write_number(worksheet, CELL("C4"), 10000,  format);
    worksheet_write_number(worksheet, CELL("C5"),  2000,  format);
    worksheet_write_number(worksheet, CELL("C6"),  6000,  format);
    worksheet_write_number(worksheet, CELL("C7"),   500,  format);

    worksheet_write_number(worksheet, CELL("D4"),  5000,  format);
    worksheet_write_number(worksheet, CELL("D5"),  3000,  format);
    worksheet_write_number(worksheet, CELL("D6"),  6000,  format);
    worksheet_write_number(worksheet, CELL("D7"),   300,  format);

    worksheet_write_number(worksheet, CELL("E4"),  8000,  format);
    worksheet_write_number(worksheet, CELL("E5"),  4000,  format);
    worksheet_write_number(worksheet, CELL("E6"),  6500,  format);
    worksheet_write_number(worksheet, CELL("E7"),   200,  format);

    worksheet_write_number(worksheet, CELL("F4"),  6000,  format);
    worksheet_write_number(worksheet, CELL("F5"),  5000,  format);
    worksheet_write_number(worksheet, CELL("F6"),  6000,  format);
    worksheet_write_number(worksheet, CELL("F7"),   700,  format);

}