初始化PHP-Xlswrite扩展

This commit is contained in:
ykxiao
2024-03-05 10:01:08 +08:00
commit 879cf9584d
2483 changed files with 1054962 additions and 0 deletions

View File

@ -0,0 +1,57 @@
###############################################################################
#
# Makefile for libxlsxwriter examples.
#
# Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
#
# Keep the output quiet by default.
Q=@
ifdef V
Q=
endif
# Directory variables.
INC_DIR = ../include
# Flags passed to the C++ compiler.
CFLAGS += -g -Wall -Wextra
# Source files to compile.
SRCS = $(wildcard *.c)
EXES = $(patsubst %.c,%,$(SRCS))
LIBXLSXWRITER = ../src/libxlsxwriter.a
LIBS = $(LIBXLSXWRITER) -lz
ifdef USE_SYSTEM_MINIZIP
LIBS += -lminizip
endif
ifdef USE_OPENSSL_MD5
LIBS += -lcrypto
endif
all : $(LIBXLSXWRITER) $(EXES)
$(LIBXLSXWRITER):
$(Q)$(MAKE) -C ../third_party/minizip
ifndef USE_STANDARD_TMPFILE
$(Q)$(MAKE) -C ../third_party/tmpfileplus
endif
ifndef USE_STANDARD_DOUBLE
$(Q)$(MAKE) -C ../third_party/dtoa
endif
ifndef USE_NO_MD5
$(Q)$(MAKE) -C ../third_party/md5
endif
$(Q)$(MAKE) -C ../src
clean :
$(Q)rm -f $(EXES)
# Executable targets.
%: %.c $(LIBXLSXWRITER)
$(Q)$(CC) -I$(INC_DIR) $(CFLAGS) $< -o $@ $(LIBS)
test_valgrind: all
$(Q)$(foreach exe,$(EXES),valgrind -q --error-exitcode=1 --leak-check=full ./$(exe) || exit;)

View File

@ -0,0 +1,63 @@
/*
* Anatomy of a simple libxlsxwriter program.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook. */
lxw_workbook *workbook = workbook_new("anatomy.xlsx");
/* Add a worksheet with a user defined sheet name. */
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Demo");
/* Add a worksheet with Excel's default sheet name: Sheet2. */
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, NULL);
/* Add some cell formats. */
lxw_format *myformat1 = workbook_add_format(workbook);
lxw_format *myformat2 = workbook_add_format(workbook);
/* Set the bold property for the first format. */
format_set_bold(myformat1);
/* Set a number format for the second format. */
format_set_num_format(myformat2, "$#,##0.00");
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet1, 0, 0, 20, NULL);
/* Write some unformatted data. */
worksheet_write_string(worksheet1, 0, 0, "Peach", NULL);
worksheet_write_string(worksheet1, 1, 0, "Plum", NULL);
/* Write formatted data. */
worksheet_write_string(worksheet1, 2, 0, "Pear", myformat1);
/* Formats can be reused. */
worksheet_write_string(worksheet1, 3, 0, "Persimmon", myformat1);
/* Write some numbers. */
worksheet_write_number(worksheet1, 5, 0, 123, NULL);
worksheet_write_number(worksheet1, 6, 0, 4567.555, myformat2);
/* Write to the second worksheet. */
worksheet_write_string(worksheet2, 0, 0, "Some text", myformat1);
/* Close the workbook, save the file and free any memory. */
lxw_error error = workbook_close(workbook);
/* Check if there was any error creating the xlsx file. */
if (error)
printf("Error in workbook_close().\n"
"Error %d = %s\n", error, lxw_strerror(error));
return error;
}

View File

@ -0,0 +1,42 @@
/*
* Example of how to use the libxlsxwriter library to write simple
* array formulas.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("array_formula.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some data for the formulas. */
worksheet_write_number(worksheet, 0, 1, 500, NULL);
worksheet_write_number(worksheet, 1, 1, 10, NULL);
worksheet_write_number(worksheet, 4, 1, 1, NULL);
worksheet_write_number(worksheet, 5, 1, 2, NULL);
worksheet_write_number(worksheet, 6, 1, 3, NULL);
worksheet_write_number(worksheet, 0, 2, 300, NULL);
worksheet_write_number(worksheet, 1, 2, 15, NULL);
worksheet_write_number(worksheet, 4, 2, 20234, NULL);
worksheet_write_number(worksheet, 5, 2, 21003, NULL);
worksheet_write_number(worksheet, 6, 2, 10000, NULL);
/* Write an array formula that returns a single value. */
worksheet_write_array_formula(worksheet, 0, 0, 0, 0, "{=SUM(B1:C1*B2:C2)}", NULL);
/* Similar to above but using the RANGE macro. */
worksheet_write_array_formula(worksheet, RANGE("A2:A2"), "{=SUM(B1:C1*B2:C2)}", NULL);
/* Write an array formula that returns a range of values. */
worksheet_write_array_formula(worksheet, 4, 0, 6, 0, "{=TREND(C5:C7,B5:B7)}", NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,362 @@
/*
* Example of adding an autofilter to a worksheet in Excel using
* libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
void write_worksheet_header(lxw_worksheet *worksheet, lxw_format *header);
int main() {
lxw_workbook *workbook = workbook_new("autofilter.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);
struct row {
char region[16];
char item[16];
int volume;
char month[16];
};
struct row data[] = {
{"East", "Apple", 9000, "July" },
{"East", "Apple", 5000, "July" },
{"South", "Orange", 9000, "September" },
{"North", "Apple", 2000, "November" },
{"West", "Apple", 9000, "November" },
{"South", "Pear", 7000, "October" },
{"North", "Pear", 9000, "August" },
{"West", "Orange", 1000, "December" },
{"West", "Grape", 1000, "November" },
{"South", "Pear", 10000, "April" },
{"West", "Grape", 6000, "January" },
{"South", "Orange", 3000, "May" },
{"North", "Apple", 3000, "December" },
{"South", "Apple", 7000, "February" },
{"West", "Grape", 1000, "December" },
{"East", "Grape", 8000, "February" },
{"South", "Grape", 10000, "June" },
{"West", "Pear", 7000, "December" },
{"South", "Apple", 2000, "October" },
{"East", "Grape", 7000, "December" },
{"North", "Grape", 6000, "April" },
{"East", "Pear", 8000, "February" },
{"North", "Apple", 7000, "August" },
{"North", "Orange", 7000, "July" },
{"North", "Apple", 6000, "June" },
{"South", "Grape", 8000, "September" },
{"West", "Apple", 3000, "October" },
{"South", "Orange", 10000, "November" },
{"West", "Grape", 4000, "July" },
{"North", "Orange", 5000, "August" },
{"East", "Orange", 1000, "November" },
{"East", "Orange", 4000, "October" },
{"North", "Grape", 5000, "August" },
{"East", "Apple", 1000, "December" },
{"South", "Apple", 10000, "March" },
{"East", "Grape", 7000, "October" },
{"West", "Grape", 1000, "September" },
{"East", "Grape", 10000, "October" },
{"South", "Orange", 8000, "March" },
{"North", "Apple", 4000, "July" },
{"South", "Orange", 5000, "July" },
{"West", "Apple", 4000, "June" },
{"East", "Apple", 5000, "April" },
{"North", "Pear", 3000, "August" },
{"East", "Grape", 9000, "November" },
{"North", "Orange", 8000, "October" },
{"East", "Apple", 10000, "June" },
{"South", "Pear", 1000, "December" },
{"North", "Grape", 10000, "July" },
{"East", "Grape", 6000, "February" }
};
uint16_t i;
lxw_row_col_options hidden = {.hidden = LXW_TRUE};
lxw_format *header = workbook_add_format(workbook);
format_set_bold(header);
/*
* Example 1. Autofilter without conditions.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet1, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet1, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet1, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet1, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet1, i + 1, 3, data[i].month, NULL);
}
/* Add the autofilter. */
worksheet_autofilter(worksheet1, 0, 0, 50, 3);
/*
* Example 2. Autofilter with a filter condition in the first column.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet2, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet2, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet2, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet2, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet2, i + 1, 3, data[i].month, NULL);
/* It isn't sufficient to just apply the filter condition below. We
* must also hide the rows that don't match the criteria since Excel
* doesn't do that automatically. */
if (strcmp(data[i].region, "East") == 0) {
/* Row matches the filter, no further action required. */
}
else {
/* Hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet2, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
/* Note, the if() statement above is written to match the logic of the
* criteria in worksheet_filter_column() below. However you could get
* the same results with the following simpler, but reversed, code:
*
* if (strcmp(data[i].region, "East") != 0) {
* worksheet_set_row_opt(worksheet2, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
* }
*
* The same applies to the Examples 3-6 as well.
*/
}
/* Add the autofilter. */
worksheet_autofilter(worksheet2, 0, 0, 50, 3);
/* Add the filter criteria. */
lxw_filter_rule filter_rule2 = {.criteria = LXW_FILTER_CRITERIA_EQUAL_TO,
.value_string = "East"};
worksheet_filter_column(worksheet2, 0, &filter_rule2);
/*
* Example 3. Autofilter with a dual filter condition in one of the columns.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet3, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet3, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet3, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet3, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet3, i + 1, 3, data[i].month, NULL);
if (strcmp(data[i].region, "East") == 0 || strcmp(data[i].region, "South") == 0) {
/* Row matches the filter, no further action required. */
}
else {
/* We need to hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet3, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
}
/* Add the autofilter. */
worksheet_autofilter(worksheet3, 0, 0, 50, 3);
/* Add the filter criteria. */
lxw_filter_rule filter_rule3a = {.criteria = LXW_FILTER_CRITERIA_EQUAL_TO,
.value_string = "East"};
lxw_filter_rule filter_rule3b = {.criteria = LXW_FILTER_CRITERIA_EQUAL_TO,
.value_string = "South"};
worksheet_filter_column2(worksheet3, 0, &filter_rule3a, &filter_rule3b, LXW_FILTER_OR);
/*
* Example 4. Autofilter with filter conditions in two columns.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet4, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet4, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet4, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet4, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet4, i + 1, 3, data[i].month, NULL);
if (strcmp(data[i].region, "East") == 0 &&
data[i].volume > 3000 && data[i].volume < 8000)
{
/* Row matches the filter, no further action required. */
}
else {
/* We need to hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet4, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
}
/* Add the autofilter. */
worksheet_autofilter(worksheet4, 0, 0, 50, 3);
/* Add the filter criteria. */
lxw_filter_rule filter_rule4a = {.criteria = LXW_FILTER_CRITERIA_EQUAL_TO,
.value_string = "East"};
lxw_filter_rule filter_rule4b = {.criteria = LXW_FILTER_CRITERIA_GREATER_THAN,
.value = 3000};
lxw_filter_rule filter_rule4c = {.criteria = LXW_FILTER_CRITERIA_LESS_THAN,
.value = 8000};
worksheet_filter_column(worksheet4, 0, &filter_rule4a);
worksheet_filter_column2(worksheet4, 2, &filter_rule4b, &filter_rule4c, LXW_FILTER_AND);
/*
* Example 5. Autofilter with a dual filter condition in one of the columns.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet5, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet5, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet5, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet5, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet5, i + 1, 3, data[i].month, NULL);
if (strcmp(data[i].region, "East") == 0 ||
strcmp(data[i].region, "North") == 0 ||
strcmp(data[i].region, "South") == 0)
{
/* Row matches the filter, no further action required. */
}
else {
/* We need to hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet5, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
}
/* Add the autofilter. */
worksheet_autofilter(worksheet5, 0, 0, 50, 3);
/* Add the filter criteria. */
char* list[] = {"East", "North", "South", NULL};
worksheet_filter_list(worksheet5, 0, list);
/*
* Example 6. Autofilter with filter for blanks.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet6, header);
/* Simulate one blank cell in the data, to test the filter. */
data[5].region[0] = '\0';
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet6, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet6, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet6, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet6, i + 1, 3, data[i].month, NULL);
if (strcmp(data[i].region, "") == 0) {
/* Row matches the filter, no further action required. */
}
else {
/* We need to hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet6, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
}
/* Add the autofilter. */
worksheet_autofilter(worksheet6, 0, 0, 50, 3);
/* Add the filter criteria. */
lxw_filter_rule filter_rule6 = {.criteria = LXW_FILTER_CRITERIA_BLANKS};
worksheet_filter_column(worksheet6, 0, &filter_rule6);
/*
* Example 7. Autofilter with filter for non-blanks.
*/
/* Set up the worksheet data. */
write_worksheet_header(worksheet7, header);
/* Write the row data. */
for (i = 0; i < sizeof(data)/sizeof(struct row); i++) {
worksheet_write_string(worksheet7, i + 1, 0, data[i].region, NULL);
worksheet_write_string(worksheet7, i + 1, 1, data[i].item, NULL);
worksheet_write_number(worksheet7, i + 1, 2, data[i].volume, NULL);
worksheet_write_string(worksheet7, i + 1, 3, data[i].month, NULL);
if (strcmp(data[i].region, "") != 0) {
/* Row matches the filter, no further action required. */
}
else {
/* We need to hide rows that don't match the filter. */
worksheet_set_row_opt(worksheet7, i + 1, LXW_DEF_ROW_HEIGHT, NULL, &hidden);
}
}
/* Add the autofilter. */
worksheet_autofilter(worksheet7, 0, 0, 50, 3);
/* Add the filter criteria. */
lxw_filter_rule filter_rule7 = {.criteria = LXW_FILTER_CRITERIA_NON_BLANKS};
worksheet_filter_column(worksheet7, 0, &filter_rule7);
return workbook_close(workbook);
}
void write_worksheet_header(lxw_worksheet *worksheet, lxw_format *header) {
/* Make the columns wider for clarity. */
worksheet_set_column(worksheet, 0, 3, 12, NULL);
/* Write the column headers. */
worksheet_set_row(worksheet, 0, 20, header);
worksheet_write_string(worksheet, 0, 0, "Region", NULL);
worksheet_write_string(worksheet, 0, 1, "Item", NULL);
worksheet_write_string(worksheet, 0, 2, "Volume", NULL);
worksheet_write_string(worksheet, 0, 3, "Month", NULL);
}

View File

@ -0,0 +1,20 @@
/*
* An example of setting a worksheet background image with libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("background.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_set_background(worksheet, "logo.png");
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,57 @@
/*
* An example of a simple Excel chart using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Write some data to the worksheet. */
void write_worksheet_data(lxw_worksheet *worksheet) {
uint8_t data[5][3] = {
/* Three columns of data. */
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row, col, data[row][col], NULL);
}
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some data for the chart. */
write_worksheet_data(worksheet);
/* Create a chart object. */
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Configure the chart. In simplest case we just add some value data
* series. The NULL categories will default to 1 to 5 like in Excel.
*/
chart_add_series(chart, NULL, "Sheet1!$A$1:$A$5");
chart_add_series(chart, NULL, "Sheet1!$B$1:$B$5");
chart_add_series(chart, NULL, "Sheet1!$C$1:$C$5");
lxw_chart_font font = {.bold = LXW_EXPLICIT_FALSE, .color = LXW_COLOR_BLUE};
chart_title_set_name(chart, "Year End Results");
chart_title_set_name_font(chart, &font);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("B7"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,143 @@
/*
* An example of creating Excel area charts using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 40, 30},
{3, 40, 25},
{4, 50, 30},
{5, 30, 10},
{6, 25, 5},
{7, 50, 10}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_area.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a area chart.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_AREA);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a stacked area chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_AREA_STACKED);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a percent stacked area chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_AREA_STACKED_PERCENT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,143 @@
/*
* An example of creating Excel bar charts using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_bar.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a bar chart.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a stacked bar chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_BAR_STACKED);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a percent stacked bar chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_BAR_STACKED_PERCENT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,94 @@
/*
* An example of a clustered category chart using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
worksheet_write_string(worksheet, 0, 0, "Types", bold);
worksheet_write_string(worksheet, 1, 0, "Type 1", NULL);
worksheet_write_string(worksheet, 4, 0, "Type 2", NULL);
worksheet_write_string(worksheet, 0, 1, "Sub Type", bold);
worksheet_write_string(worksheet, 1, 1, "Sub Type A", NULL);
worksheet_write_string(worksheet, 2, 1, "Sub Type B", NULL);
worksheet_write_string(worksheet, 3, 1, "Sub Type C", NULL);
worksheet_write_string(worksheet, 4, 1, "Sub Type D", NULL);
worksheet_write_string(worksheet, 5, 1, "Sub Type E", NULL);
worksheet_write_string(worksheet, 0, 2, "Value 1", bold);
worksheet_write_number(worksheet, 1, 2, 5000, NULL);
worksheet_write_number(worksheet, 2, 2, 2000, NULL);
worksheet_write_number(worksheet, 3, 2, 250, NULL);
worksheet_write_number(worksheet, 4, 2, 6000, NULL);
worksheet_write_number(worksheet, 5, 2, 500, NULL);
worksheet_write_string(worksheet, 0, 3, "Value 2", bold);
worksheet_write_number(worksheet, 1, 3, 8000, NULL);
worksheet_write_number(worksheet, 2, 3, 3000, NULL);
worksheet_write_number(worksheet, 3, 3, 1000, NULL);
worksheet_write_number(worksheet, 4, 3, 6000, NULL);
worksheet_write_number(worksheet, 5, 3, 300, NULL);
worksheet_write_string(worksheet, 0, 4, "Value 3", bold);
worksheet_write_number(worksheet, 1, 4, 6000, NULL);
worksheet_write_number(worksheet, 2, 4, 4000, NULL);
worksheet_write_number(worksheet, 3, 4, 2000, NULL);
worksheet_write_number(worksheet, 4, 4, 6500, NULL);
worksheet_write_number(worksheet, 5, 4, 200, NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_clustered2.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Configure the series. Note, that the categories are 2D ranges (from
* column A to column B). This creates the clusters. The series are shown
* as formula strings for clarity but you can also use variables with the
* chart_series_set_categories() and chart_series_set_values()
* functions. See the docs.
*/
chart_add_series(chart,
"=Sheet1!$A$2:$B$6",
"=Sheet1!$C$2:$C$6");
chart_add_series(chart,
"=Sheet1!$A$2:$B$6",
"=Sheet1!$D$2:$D$6");
chart_add_series(chart,
"=Sheet1!$A$2:$B$6",
"=Sheet1!$E$2:$E$6");
/* Set an Excel chart style. */
chart_set_style(chart, 37);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("G3"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,143 @@
/*
* An example of creating Excel column charts using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_column.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a column chart.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a stacked column chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a percent stacked column chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED_PERCENT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,386 @@
/*
* A demo of an various Excel chart data label features that are available via
* a libxlsxwriter chart.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_data_labels.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Some chart positioning options. */
lxw_chart_options options = {.x_offset = 25, .y_offset = 10};
/* Write some data for the chart. */
worksheet_write_string(worksheet, 0, 0, "Number", bold);
worksheet_write_number(worksheet, 1, 0, 2, NULL);
worksheet_write_number(worksheet, 2, 0, 3, NULL);
worksheet_write_number(worksheet, 3, 0, 4, NULL);
worksheet_write_number(worksheet, 4, 0, 5, NULL);
worksheet_write_number(worksheet, 5, 0, 6, NULL);
worksheet_write_number(worksheet, 6, 0, 7, NULL);
worksheet_write_string(worksheet, 0, 1, "Data", bold);
worksheet_write_number(worksheet, 1, 1, 20, NULL);
worksheet_write_number(worksheet, 2, 1, 10, NULL);
worksheet_write_number(worksheet, 3, 1, 20, NULL);
worksheet_write_number(worksheet, 4, 1, 30, NULL);
worksheet_write_number(worksheet, 5, 1, 40, NULL);
worksheet_write_number(worksheet, 6, 1, 30, NULL);
worksheet_write_string(worksheet, 0, 2, "Text", bold);
worksheet_write_string(worksheet, 1, 2, "Jan", NULL);
worksheet_write_string(worksheet, 2, 2, "Feb", NULL);
worksheet_write_string(worksheet, 3, 2, "Mar", NULL);
worksheet_write_string(worksheet, 4, 2, "Apr", NULL);
worksheet_write_string(worksheet, 5, 2, "May", NULL);
worksheet_write_string(worksheet, 6, 2, "Jun", NULL);
/*
* Chart 1. Example with standard data labels.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with standard data labels");
/* Add a data series to the chart. */
lxw_chart_series *series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D2"), chart, &options);
/*
* Chart 2. Example with value and category data labels.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Category and Value data labels");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Turn on Value and Category labels. */
chart_series_set_labels_options(series, LXW_FALSE, LXW_TRUE, LXW_TRUE);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D18"), chart, &options);
/*
* Chart 3. Example with standard data labels with different font.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Data labels with user defined font");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
lxw_chart_font font1 = {.bold = LXW_TRUE, .color = LXW_COLOR_RED, .rotation = -30};
chart_series_set_labels_font(series, &font1);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D34"), chart, &options);
/*
* Chart 4. Example with standard data labels and formatting.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Data labels with formatting");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Set the border/line and fill for the data labels. */
lxw_chart_line line1 = {.color = LXW_COLOR_RED};
lxw_chart_fill fill1 = {.color = LXW_COLOR_YELLOW};
chart_series_set_labels_line(series, &line1);
chart_series_set_labels_fill(series, &fill1);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D50"), chart, &options);
/*
* Chart 5.Example with custom string data labels.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with custom string data labels");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Create some custom labels. */
lxw_chart_data_label data_label5_1 = {.value = "Amy"};
lxw_chart_data_label data_label5_2 = {.value = "Bea"};
lxw_chart_data_label data_label5_3 = {.value = "Eva"};
lxw_chart_data_label data_label5_4 = {.value = "Fay"};
lxw_chart_data_label data_label5_5 = {.value = "Liv"};
lxw_chart_data_label data_label5_6 = {.value = "Una"};
/* Create an array of label pointers. NULL indicates the end of the array. */
lxw_chart_data_label *data_labels5[] = {
&data_label5_1,
&data_label5_2,
&data_label5_3,
&data_label5_4,
&data_label5_5,
&data_label5_6,
NULL
};
/* Set the custom labels. */
chart_series_set_labels_custom(series, data_labels5);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D66"), chart, &options);
/*
* Chart 6. Example with custom data labels from cells.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with custom data labels from cells");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Create some custom labels. */
lxw_chart_data_label data_label6_1 = {.value = "=Sheet1!$C$2"};
lxw_chart_data_label data_label6_2 = {.value = "=Sheet1!$C$3"};
lxw_chart_data_label data_label6_3 = {.value = "=Sheet1!$C$4"};
lxw_chart_data_label data_label6_4 = {.value = "=Sheet1!$C$5"};
lxw_chart_data_label data_label6_5 = {.value = "=Sheet1!$C$6"};
lxw_chart_data_label data_label6_6 = {.value = "=Sheet1!$C$7"};
/* Create an array of label pointers. NULL indicates the end of the array. */
lxw_chart_data_label *data_labels6[] = {
&data_label6_1,
&data_label6_2,
&data_label6_3,
&data_label6_4,
&data_label6_5,
&data_label6_6,
NULL
};
/* Set the custom labels. */
chart_series_set_labels_custom(series, data_labels6);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D82"), chart, &options);
/*
* Chart 7. Example with custom and default data labels.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Mixed custom and default data labels");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
lxw_chart_font font2 = {.color = LXW_COLOR_RED};
/* Add the series data labels. */
chart_series_set_labels(series);
/* Create some custom labels. */
/* The following is used to get a mix of default and custom labels. The
* items initialized with '{0}' and items without a custom label (points 5
* and 6 which come after NULL) will get the default value. We also set a
* font for the custom items as an extra example.
*/
lxw_chart_data_label data_label7_1 = {.value = "=Sheet1!$C$2", .font = &font2};
lxw_chart_data_label data_label7_2 = {0};
lxw_chart_data_label data_label7_3 = {.value = "=Sheet1!$C$4", .font = &font2};
lxw_chart_data_label data_label7_4 = {.value = "=Sheet1!$C$5", .font = &font2};
/* Create an array of label pointers. NULL indicates the end of the array. */
lxw_chart_data_label *data_labels7[] = {
&data_label7_1,
&data_label7_2,
&data_label7_3,
&data_label7_4,
NULL
};
/* Set the custom labels. */
chart_series_set_labels_custom(series, data_labels7);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D98"), chart, &options);
/*
* Chart 8. Example with deleted/hidden custom data labels.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with deleted data labels");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Create some custom labels. */
lxw_chart_data_label hide = {.hide = LXW_TRUE};
lxw_chart_data_label keep = {.hide = LXW_FALSE};
/* An initialized struct like this would also work: */
/* lxw_chart_data_label keep = {0}; */
/* Create an array of label pointers. NULL indicates the end of the array. */
lxw_chart_data_label *data_labels8[] = {
&hide,
&keep,
&hide,
&hide,
&keep,
&hide,
NULL
};
/* Set the custom labels. */
chart_series_set_labels_custom(series, data_labels8);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D114"), chart, &options);
/*
* Chart 9.Example with custom string data labels and formatting.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with custom labels and formatting");
/* Add a data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
"=Sheet1!$B$2:$B$7");
/* Add the series data labels. */
chart_series_set_labels(series);
/* Set the border/line and fill for the data labels. */
lxw_chart_line line2 = {.color = LXW_COLOR_RED};
lxw_chart_fill fill2 = {.color = LXW_COLOR_YELLOW};
lxw_chart_line line3 = {.color = LXW_COLOR_BLUE};
lxw_chart_fill fill3 = {.color = LXW_COLOR_GREEN};
/* Create some custom labels. */
lxw_chart_data_label data_label9_1 = {.value = "Amy", .line = &line3};
lxw_chart_data_label data_label9_2 = {.value = "Bea"};
lxw_chart_data_label data_label9_3 = {.value = "Eva"};
lxw_chart_data_label data_label9_4 = {.value = "Fay"};
lxw_chart_data_label data_label9_5 = {.value = "Liv"};
lxw_chart_data_label data_label9_6 = {.value = "Una", .fill = &fill3};
/* Set the default formatting for the data labels in the series. */
chart_series_set_labels_line(series, &line2);
chart_series_set_labels_fill(series, &fill2);
/* Create an array of label pointers. NULL indicates the end of the array. */
lxw_chart_data_label *data_labels9[] = {
&data_label9_1,
&data_label9_2,
&data_label9_3,
&data_label9_4,
&data_label9_5,
&data_label9_6,
NULL
};
/* Set the custom labels. */
chart_series_set_labels_custom(series, data_labels9);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart_opt(worksheet, CELL("D130"), chart, &options);
return workbook_close(workbook);
}

View File

@ -0,0 +1,120 @@
/*
* An example of creating Excel column charts with data tables using the
* libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_data_table.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a column chart with a data table.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Chart with Data Table");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set a default data table on the X-axis. */
chart_set_table(chart);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a column chart with a data table and legend keys.
*/
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Data Table with legend keys");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set a data table on the X-axis with the legend keys shown. */
chart_set_table(chart);
chart_set_table_grid(chart, LXW_TRUE, LXW_TRUE, LXW_TRUE, LXW_TRUE);
/* Turn off the legend. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,203 @@
/*
* A demo of an various Excel chart data tools that are available via a
* libxlsxwriter chart.
*
* These include Drop Lines and High-Low Lines.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_data_tools.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Example with High Low Lines.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with High-Low Lines");
/* Add the first series to the chart. */
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add high-low lines to the chart. */
chart_set_high_low_lines(chart, NULL);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Example with Drop Lines.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with Drop Lines");
/* Add the first series to the chart. */
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add drop lines to the chart. */
chart_set_drop_lines(chart, NULL);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Example with Up-Down bars.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with Up-Down bars");
/* Add the first series to the chart. */
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add Up-Down bars to the chart. */
chart_set_up_down_bars(chart);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
/*
* Chart 4. Example with Up-Down bars with formatting.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with Up-Down bars");
/* Add the first series to the chart. */
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add Up-Down bars to the chart, with formatting. */
lxw_chart_line line = {.color = LXW_COLOR_BLACK};
lxw_chart_fill up_fill = {.color = 0x00B050};
lxw_chart_fill down_fill = {.color = LXW_COLOR_RED};
chart_set_up_down_bars_format(chart, &line, &up_fill, &line, &down_fill);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E50"), chart);
/*
* Chart 5. Example with Markers and data labels.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with Data Labels and Markers");
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add series markers. */
chart_series_set_marker_type(series, LXW_CHART_MARKER_CIRCLE);
/* Add series data labels. */
chart_series_set_labels(series);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E66"), chart);
/*
* Chart 6. Example with Error Bars.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with Error Bars");
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add error bars to show Standard Error. */
chart_series_set_error_bars(series->y_error_bars,
LXW_CHART_ERROR_BAR_TYPE_STD_ERROR, 0);
/* Add series data labels. */
chart_series_set_labels(series);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E82"), chart);
/*
* Chart 7. Example with a trendline
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add a chart title. */
chart_title_set_name(chart, "Chart with a Trendline");
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Add a polynomial trendline. */
lxw_chart_line poly_line = {.color = LXW_COLOR_GRAY,
.dash_type = LXW_CHART_LINE_DASH_LONG_DASH};
chart_series_set_trendline(series, LXW_CHART_TRENDLINE_TYPE_POLY, 3);
chart_series_set_trendline_line(series, &poly_line);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E98"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,162 @@
/*
* An example of creating an Excel doughnut chart using the libxlsxwriter library.
*
* The demo also shows how to set segment colors. It is possible to define
* chart colors for most types of libxlsxwriter charts via the series
* formatting functions. However, Pie/Doughnut charts are a special case since
* each segment is represented as a point so it is necessary to assign
* formatting to each point in the series.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
worksheet_write_string(worksheet, CELL("A1"), "Category", bold);
worksheet_write_string(worksheet, CELL("A2"), "Glazed", NULL);
worksheet_write_string(worksheet, CELL("A3"), "Chocolate", NULL);
worksheet_write_string(worksheet, CELL("A4"), "Cream", NULL);
worksheet_write_string(worksheet, CELL("B1"), "Values", bold);
worksheet_write_number(worksheet, CELL("B2"), 50, NULL);
worksheet_write_number(worksheet, CELL("B3"), 35, NULL);
worksheet_write_number(worksheet, CELL("B4"), 15, NULL);
}
/*
* Create a worksheet with examples charts.
*
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_doughnut.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart ;
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1: Create a simple doughnut chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_DOUGHNUT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Doughnut sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Popular Doughnut Types");
/* Set an Excel chart style. */
chart_set_style(chart, 10);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D2"), chart);
/*
* Chart 2: Create a doughnut chart with user defined segment colors.
*/
chart = workbook_add_chart(workbook, LXW_CHART_DOUGHNUT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Doughnut sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Doughnut Chart with user defined colors");
/* Add for fills for use in the chart. */
lxw_chart_fill fill1 = {.color = 0xFA58D0};
lxw_chart_fill fill2 = {.color = 0x61210B};
lxw_chart_fill fill3 = {.color = 0xF5F6CE};
/* Add some points with the above fills. */
lxw_chart_point point1 = {.fill = &fill1};
lxw_chart_point point2 = {.fill = &fill2};
lxw_chart_point point3 = {.fill = &fill3};
/* Create an array of the point objects. */
lxw_chart_point *points[] = {&point1,
&point2,
&point3,
NULL};
/* Add/override the points/segments of the chart. */
chart_series_set_points(series, points);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D18"), chart);
/*
* Chart 3: Create a Doughnut chart with rotation of the segments.
*/
chart = workbook_add_chart(workbook, LXW_CHART_DOUGHNUT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Doughnut sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Doughnut Chart with segment rotation");
/* Change the angle/rotation of the first segment. */
chart_set_rotation(chart, 90);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D34"), chart);
/*
* Chart 4: Create a Doughnut chart with user defined hole size and other
* options.
*/
chart = workbook_add_chart(workbook, LXW_CHART_DOUGHNUT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Doughnut sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Doughnut Chart with options applied.");
/* Add/override the points/segments defined in Chart 2. */
chart_series_set_points(series, points);
/* Set an Excel chart style. */
chart_set_style(chart, 26);
/* Change the angle/rotation of the first segment. */
chart_set_rotation(chart, 28);
/* Change the hole size. */
chart_set_hole_size(chart, 33);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D50"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,65 @@
/*
* An example of a simple Excel chart with user defined fonts using the
* libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart_fonts.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some data for the chart. */
worksheet_write_number(worksheet, 0, 0, 10, NULL);
worksheet_write_number(worksheet, 1, 0, 40, NULL);
worksheet_write_number(worksheet, 2, 0, 50, NULL);
worksheet_write_number(worksheet, 3, 0, 20, NULL);
worksheet_write_number(worksheet, 4, 0, 10, NULL);
worksheet_write_number(worksheet, 5, 0, 50, NULL);
/* Create a chart object. */
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Configure the chart. */
chart_add_series(chart, NULL, "Sheet1!$A$1:$A$6");
/* Create some fonts to use in the chart. */
lxw_chart_font font1 = {.name = "Calibri", .color = LXW_COLOR_BLUE};
lxw_chart_font font2 = {.name = "Courier", .color = 0x92D050};
lxw_chart_font font3 = {.name = "Arial", .color = 0x00B0F0};
lxw_chart_font font4 = {.name = "Century", .color = LXW_COLOR_RED};
lxw_chart_font font5 = {.rotation = -30};
lxw_chart_font font6 = {.bold = LXW_TRUE,
.italic = LXW_TRUE,
.underline = LXW_TRUE,
.color = 0x7030A0};
/* Write the chart title with a font. */
chart_title_set_name(chart, "Test Results");
chart_title_set_name_font(chart, &font1);
/* Write the Y axis with a font. */
chart_axis_set_name(chart->y_axis, "Units");
chart_axis_set_name_font(chart->y_axis, &font2);
chart_axis_set_num_font(chart->y_axis, &font3);
/* Write the X axis with a font. */
chart_axis_set_name(chart->x_axis, "Month");
chart_axis_set_name_font(chart->x_axis, &font4);
chart_axis_set_num_font(chart->x_axis, &font5);
/* Display the chart legend at the bottom of the chart. */
chart_legend_set_position(chart, LXW_CHART_LEGEND_BOTTOM);
chart_legend_set_font(chart, &font6);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("C1"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,142 @@
/*
* An example of creating an Excel line chart using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_line.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a line chart.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 10);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a stacked line chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE_STACKED);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a percent stacked line chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_LINE_STACKED_PERCENT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,77 @@
/*
* An example of a simple Excel chart with patterns using the libxlsxwriter
* library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart_pattern.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
worksheet_write_string(worksheet, 0, 0, "Shingle", bold);
worksheet_write_number(worksheet, 1, 0, 105, NULL);
worksheet_write_number(worksheet, 2, 0, 150, NULL);
worksheet_write_number(worksheet, 3, 0, 130, NULL);
worksheet_write_number(worksheet, 4, 0, 90, NULL);
worksheet_write_string(worksheet, 0, 1, "Brick", bold);
worksheet_write_number(worksheet, 1, 1, 50, NULL);
worksheet_write_number(worksheet, 2, 1, 120, NULL);
worksheet_write_number(worksheet, 3, 1, 100, NULL);
worksheet_write_number(worksheet, 4, 1, 110, NULL);
/* Create a chart object. */
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Configure the chart. */
lxw_chart_series *series1 = chart_add_series(chart, NULL, "Sheet1!$A$2:$A$5");
lxw_chart_series *series2 = chart_add_series(chart, NULL, "Sheet1!$B$2:$B$5");
chart_series_set_name(series1, "=Sheet1!$A$1");
chart_series_set_name(series2, "=Sheet1!$B$1");
chart_title_set_name(chart, "Cladding types");
chart_axis_set_name(chart->x_axis, "Region");
chart_axis_set_name(chart->y_axis, "Number of houses");
/* Configure an add the chart series patterns. */
lxw_chart_pattern pattern1 = {.type = LXW_CHART_PATTERN_SHINGLE,
.fg_color = 0x804000,
.bg_color = 0XC68C53};
lxw_chart_pattern pattern2 = {.type = LXW_CHART_PATTERN_HORIZONTAL_BRICK,
.fg_color = 0XB30000,
.bg_color = 0XFF6666};
chart_series_set_pattern(series1, &pattern1);
chart_series_set_pattern(series2, &pattern2);
/* Configure and set the chart series borders. */
lxw_chart_line line1 = {.color = 0x804000};
lxw_chart_line line2 = {.color = 0xb30000};
chart_series_set_line(series1, &line1);
chart_series_set_line(series2, &line2);
/* Widen the gap between the series/categories. */
chart_set_series_gap(chart, 70);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D2"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,132 @@
/*
* An example of creating an Excel pie chart using the libxlsxwriter library.
*
* The demo also shows how to set segment colors. It is possible to define
* chart colors for most types of libxlsxwriter charts via the series
* formatting functions. However, Pie/Doughnut charts are a special case since
* each segment is represented as a point so it is necessary to assign
* formatting to each point in the series.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
worksheet_write_string(worksheet, CELL("A1"), "Category", bold);
worksheet_write_string(worksheet, CELL("A2"), "Apple", NULL);
worksheet_write_string(worksheet, CELL("A3"), "Cherry", NULL);
worksheet_write_string(worksheet, CELL("A4"), "Pecan", NULL);
worksheet_write_string(worksheet, CELL("B1"), "Values", bold);
worksheet_write_number(worksheet, CELL("B2"), 60, NULL);
worksheet_write_number(worksheet, CELL("B3"), 30, NULL);
worksheet_write_number(worksheet, CELL("B4"), 10, NULL);
}
/*
* Create a worksheet with examples charts.
*
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_pie.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart ;
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1: Create a simple pie chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_PIE);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Pie sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Popular Pie Types");
/* Set an Excel chart style. */
chart_set_style(chart, 10);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D2"), chart);
/*
* Chart 2: Create a pie chart with user defined segment colors.
*/
chart = workbook_add_chart(workbook, LXW_CHART_PIE);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Pie sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Pie Chart with user defined colors");
/* Add for fills for use in the chart. */
lxw_chart_fill fill1 = {.color = 0x5ABA10};
lxw_chart_fill fill2 = {.color = 0xFE110E};
lxw_chart_fill fill3 = {.color = 0xCA5C05};
/* Add some points with the above fills. */
lxw_chart_point point1 = {.fill = &fill1};
lxw_chart_point point2 = {.fill = &fill2};
lxw_chart_point point3 = {.fill = &fill3};
/* Create an array of the point objects. */
lxw_chart_point *points[] = {&point1,
&point2,
&point3,
NULL};
/* Add/override the points/segments of the chart. */
chart_series_set_points(series, points);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D18"), chart);
/*
* Chart 3: Create a pie chart with rotation of the segments.
*/
chart = workbook_add_chart(workbook, LXW_CHART_PIE);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$4", "=Sheet1!$B$2:$B$4");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "Pie sales data");
/* Add a chart title. */
chart_title_set_name(chart, "Pie Chart with segment rotation");
/* Change the angle/rotation of the first segment. */
chart_set_rotation(chart, 90);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,62 @@
/*
* An example of creating an Excel pie chart with user defined colors using
* the libxlsxwriter library.
*
* In general formatting is applied to an entire series in a chart. However,
* it is occasionally required to format individual points in a series. In
* particular this is required for Pie/Doughnut charts where each segment is
* represented by a point.
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Create a worksheet with an example Pie chart.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_pie_colors.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Write some data for the chart. */
worksheet_write_string(worksheet, CELL("A1"), "Pass", NULL);
worksheet_write_string(worksheet, CELL("A2"), "Fail", NULL);
worksheet_write_number(worksheet, CELL("B1"), 90, NULL);
worksheet_write_number(worksheet, CELL("B2"), 10, NULL);
/* Create a pie chart. */
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_PIE);
/* Add the data series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$1:$A$2",
"=Sheet1!$B$1:$B$2");
/* Create some fills for the chart points/segments. */
lxw_chart_fill red_fill = {.color = LXW_COLOR_RED };
lxw_chart_fill green_fill = {.color = LXW_COLOR_GREEN};
/* Add the fills to the point objects. */
lxw_chart_point red_point = {.fill = &red_fill };
lxw_chart_point green_point = {.fill = &green_fill};
/* Create an array of pointer to chart points. Note, the array should be
* NULL terminated. */
lxw_chart_point *points[] = {&green_point,
&red_point,
NULL};
/* Add the points to the series. */
chart_series_set_points(series, points);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D2"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,143 @@
/*
* An example of creating Excel radar charts using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 30, 25},
{3, 60, 40},
{4, 70, 50},
{5, 50, 30},
{6, 40, 50},
{7, 30, 40}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_radar.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a radar chart.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_RADAR);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a radar chart with markers.
*/
chart = workbook_add_chart(workbook, LXW_CHART_RADAR_WITH_MARKERS);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a filled radar chart.
*/
chart = workbook_add_chart(workbook, LXW_CHART_RADAR_FILLED);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,205 @@
/*
* An example of creating Excel scatter charts using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_scatter.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/*
* Chart 1. Create a scatter chart, the default shows points only.
*/
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_SCATTER);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a scatter chart with straight lines and markers
* connecting the points.
*/
chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a scatter chart with straight lines connecting the
* points.
*/
chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_STRAIGHT);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
/*
* Chart 4. Create a scatter chart with smooth lines and markers
* connecting the points.
*/
chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 14);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E50"), chart);
/*
* Chart 5. Create a scatter chart with smooth lines connecting the
* points.
*/
chart = workbook_add_chart(workbook, LXW_CHART_SCATTER_SMOOTH);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 15);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E66"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,62 @@
/*
* An example showing all 48 default chart styles available in Excel 2007
* using the libxlsxwriter library. Note, these styles are not the same as the
* styles available in Excel 2013.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
int chart_types[] = {LXW_CHART_COLUMN, LXW_CHART_AREA, LXW_CHART_LINE, LXW_CHART_PIE};
char *chart_names[] = {"Column", "Area", "Line", "Pie"};
char chart_title[32] = {0};
int row_num, col_num, chart_num, style_num;
lxw_worksheet *worksheet;
lxw_chart *chart;
lxw_workbook *workbook = workbook_new("chart_styles.xlsx");
for (chart_num = 0; chart_num < 4; chart_num++) {
/* Add a worksheet for each chart type. */
worksheet = workbook_add_worksheet(workbook, chart_names[chart_num]);
worksheet_set_zoom(worksheet, 30);
/* Create 48 charts, each with a different style. */
style_num = 1;
for (row_num = 0; row_num < 90; row_num += 15) {
for (col_num = 0; col_num < 64; col_num += 8) {
chart = workbook_add_chart(workbook, chart_types[chart_num]);
sprintf(chart_title, "Style %d", style_num);
chart_add_series(chart, NULL, "=Data!$A$1:$A$6");
chart_title_set_name(chart, chart_title);
chart_set_style(chart, style_num);
worksheet_insert_chart(worksheet, row_num, col_num, chart);
style_num++;
}
}
}
/* Create a worksheet with data for the charts. */
worksheet = workbook_add_worksheet(workbook, "Data");
worksheet_write_number(worksheet, 0, 0, 10, NULL);
worksheet_write_number(worksheet, 1, 0, 40, NULL);
worksheet_write_number(worksheet, 2, 0, 50, NULL);
worksheet_write_number(worksheet, 3, 0, 20, NULL);
worksheet_write_number(worksheet, 4, 0, 10, NULL);
worksheet_write_number(worksheet, 5, 0, 50, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,40 @@
/*
* An example of a simple Excel chart using the libxlsxwriter library. This
* example is used in the "Working with Charts" section of the docs.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart_line.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart;
lxw_chart_series *series;
/* Write some data for the chart. */
worksheet_write_number(worksheet, 0, 0, 10, NULL);
worksheet_write_number(worksheet, 1, 0, 40, NULL);
worksheet_write_number(worksheet, 2, 0, 50, NULL);
worksheet_write_number(worksheet, 3, 0, 20, NULL);
worksheet_write_number(worksheet, 4, 0, 10, NULL);
worksheet_write_number(worksheet, 5, 0, 50, NULL);
/* Create a chart object. */
chart = workbook_add_chart(workbook, LXW_CHART_LINE);
/* Configure the chart. */
series = chart_add_series(chart, NULL, "Sheet1!$A$1:$A$6");
(void)series; /* Do something with series in the real examples. */
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("C1"), chart);
return workbook_close(workbook);
}

View File

@ -0,0 +1,85 @@
/*
* An example of creating an Excel chartsheet using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chartsheet.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chartsheet *chartsheet = workbook_add_chartsheet(workbook, NULL);
lxw_chart_series *series;
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
/* Create a bar chart. */
lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Add the chart to the chartsheet. */
chartsheet_set_chart(chartsheet, chart);
/* Display the chartsheet as the active sheet when the workbook is opened. */
chartsheet_activate(chartsheet);
return workbook_close(workbook);
}

View File

@ -0,0 +1,20 @@
/*
* An example of writing cell comments to a worksheet using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("comments1.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_write_string( worksheet, 0, 0, "Hello" , NULL);
worksheet_write_comment(worksheet, 0, 0, "This is a comment");
return workbook_close(workbook);
}

View File

@ -0,0 +1,281 @@
/*
* An example of writing cell comments to a worksheet using libxlsxwriter.
*
* Each of the worksheets demonstrates different features of cell comments.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("comments2.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_format *text_wrap = workbook_add_format(workbook);
format_set_text_wrap(text_wrap);
format_set_align(text_wrap, LXW_ALIGN_VERTICAL_TOP);
/*
* Example 1. Demonstrates a simple cell comments without formatting.
*/
/* Set up some worksheet formatting. */
worksheet_set_column(worksheet1, 2, 2, 25, NULL);
worksheet_set_row(worksheet1, 2, 50, NULL);
worksheet_write_string(worksheet1, CELL("C3"),
"Hold the mouse over this cell to see the comment.",
text_wrap);
worksheet_write_comment(worksheet1, CELL("C3"), "This is a comment.");
/*
* Example 2. Demonstrates visible and hidden comments.
*/
/* Set up some worksheet formatting. */
worksheet_set_column(worksheet2, 2, 2, 25, NULL);
worksheet_set_row(worksheet2, 2, 50, NULL);
worksheet_set_row(worksheet2, 2, 50, NULL);
worksheet_write_string(worksheet2, CELL("C3"),
"This cell comment is visible.",
text_wrap);
/* Use an option to make the comment visible. */
lxw_comment_options options2 = {.visible = LXW_COMMENT_DISPLAY_VISIBLE};
worksheet_write_comment_opt(worksheet2, CELL("C3"), "Hello.", &options2);
worksheet_write_string(worksheet2, CELL("C6"),
"This cell comment isn't visible until you pass "
"the mouse over it (the default).",
text_wrap);
worksheet_write_comment(worksheet2, CELL("C6"), "Hello.");
/*
* Example 3. Demonstrates visible and hidden comments, set at the
* worksheet level.
*/
worksheet_set_column(worksheet3, 2, 2, 25, NULL);
worksheet_set_row(worksheet3, 2, 50, NULL);
worksheet_set_row(worksheet3, 5, 50, NULL);
worksheet_set_row(worksheet3, 8, 50, NULL);
/* Make all comments on the worksheet visible. */
worksheet_show_comments(worksheet3);
worksheet_write_string(worksheet3, CELL("C3"),
"This cell comment is visible, explicitly.",
text_wrap);
lxw_comment_options options3a = {.visible = LXW_COMMENT_DISPLAY_VISIBLE};
worksheet_write_comment_opt(worksheet3, 2, 2, "Hello", &options3a);
worksheet_write_string(worksheet3, CELL("C6"),
"This cell comment is also visible because "
"we used worksheet_show_comments().",
text_wrap);
worksheet_write_comment(worksheet3, CELL("C6"), "Hello");
worksheet_write_string(worksheet3, CELL("C9"),
"However, we can still override it locally.",
text_wrap);
lxw_comment_options options3b = {.visible = LXW_COMMENT_DISPLAY_HIDDEN};
worksheet_write_comment_opt(worksheet3, CELL("C9"), "Hello", &options3b);
/*
* Example 4. Demonstrates changes to the comment box dimensions.
*/
worksheet_set_column(worksheet4, 2, 2, 25, NULL);
worksheet_set_row(worksheet4, 2, 50, NULL);
worksheet_set_row(worksheet4, 5, 50, NULL);
worksheet_set_row(worksheet4, 8, 50, NULL);
worksheet_set_row(worksheet4, 15, 50, NULL);
worksheet_set_row(worksheet4, 18, 50, NULL);
worksheet_show_comments(worksheet4);
worksheet_write_string(worksheet4, CELL("C3"),
"This cell comment is default size.",
text_wrap);
worksheet_write_comment_opt(worksheet4, 2, 2, "Hello", NULL);
worksheet_write_string(worksheet4, CELL("C6"),
"This cell comment is twice as wide.",
text_wrap);
lxw_comment_options options4a = {.x_scale = 2.0};
worksheet_write_comment_opt(worksheet4, CELL("C6"), "Hello", &options4a);
worksheet_write_string(worksheet4, CELL("C9"),
"This cell comment is twice as high.",
text_wrap);
lxw_comment_options options4b = {.y_scale = 2.0};
worksheet_write_comment_opt(worksheet4, CELL("C9"), "Hello", &options4b);
worksheet_write_string(worksheet4, CELL("C16"),
"This cell comment is scaled in both directions.",
text_wrap);
lxw_comment_options options4c = {.x_scale = 1.2, .y_scale = 0.5};
worksheet_write_comment_opt(worksheet4, CELL("C16"), "Hello", &options4c);
worksheet_write_string(worksheet4, CELL("C19"),
"This cell comment has width and height specified in pixels.",
text_wrap);
lxw_comment_options options4d = {.width = 200, .height = 50};
worksheet_write_comment_opt(worksheet4, CELL("C19"), "Hello", &options4d);
/*
* Example 5. Demonstrates changes to the cell comment position.
*/
worksheet_set_column(worksheet5, 2, 2, 25, NULL);
worksheet_set_row(worksheet5, 2, 50, NULL);
worksheet_set_row(worksheet5, 5, 50, NULL);
worksheet_set_row(worksheet5, 8, 50, NULL);
worksheet_show_comments(worksheet5);
worksheet_write_string(worksheet5, CELL("C3"),
"This cell comment is in the default position.",
text_wrap);
worksheet_write_comment(worksheet5, 2, 2, "Hello");
worksheet_write_string(worksheet5, CELL("C6"),
"This cell comment has been moved to another cell.",
text_wrap);
lxw_comment_options options5a = {.start_row = 3, .start_col = 4};
worksheet_write_comment_opt(worksheet5, CELL("C6"), "Hello", &options5a);
worksheet_write_string(worksheet5, CELL("C9"),
"This cell comment has been shifted within its default cell.",
text_wrap);
lxw_comment_options options5b = {.x_offset = 30, .y_offset = 12};
worksheet_write_comment_opt(worksheet5, CELL("C9"), "Hello", &options5b);
/*
* Example 6. Demonstrates changes to the comment background color.
*/
worksheet_set_column(worksheet6, 2, 2, 25, NULL);
worksheet_set_row(worksheet6, 2, 50, NULL);
worksheet_set_row(worksheet6, 5, 50, NULL);
worksheet_set_row(worksheet6, 8, 50, NULL);
worksheet_show_comments(worksheet6);
worksheet_write_string(worksheet6, CELL("C3"),
"This cell comment has a different color.",
text_wrap);
lxw_comment_options options6a = {.color = LXW_COLOR_GREEN};
worksheet_write_comment_opt(worksheet6, 2, 2, "Hello", &options6a);
worksheet_write_string(worksheet6, CELL("C6"),
"This cell comment has the default color.",
text_wrap);
worksheet_write_comment(worksheet6, CELL("C6"), "Hello");
worksheet_write_string(worksheet6, CELL("C9"),
"This cell comment has a different color.",
text_wrap);
lxw_comment_options options6b = {.color = 0xFF6600};
worksheet_write_comment_opt(worksheet6, CELL("C9"), "Hello", &options6b);
/*
* Example 7. Demonstrates how to set the cell comment author.
*/
worksheet_set_column(worksheet7, 2, 2, 30, NULL);
worksheet_set_row(worksheet7, 2, 50, NULL);
worksheet_set_row(worksheet7, 5, 60, NULL);
worksheet_write_string(worksheet7, CELL("C3"),
"Move the mouse over this cell and you will see 'Cell C3 "
"commented by' (blank) in the status bar at the bottom.",
text_wrap);
worksheet_write_comment(worksheet7, CELL("C3"), "Hello");
worksheet_write_string(worksheet7, CELL("C6"),
"Move the mouse over this cell and you will see 'Cell C6 "
"commented by libxlsxwriter' in the status bar at the bottom.",
text_wrap);
lxw_comment_options options7a = {.author = "libxlsxwriter"};
worksheet_write_comment_opt(worksheet7, CELL("C6"), "Hello", &options7a);
/*
* Example 8. Demonstrates the need to explicitly set the row height.
*/
worksheet_set_column(worksheet8, 2, 2, 25, NULL);
worksheet_set_row(worksheet8, 2, 80, NULL);
worksheet_show_comments(worksheet8);
worksheet_write_string(worksheet8, CELL("C3"),
"The height of this row has been adjusted explicitly using "
"worksheet_set_row(). The size of the comment box is "
"adjusted accordingly by libxlsxwriter",
text_wrap);
worksheet_write_comment(worksheet8, CELL("C3"), "Hello");
worksheet_write_string(worksheet8, CELL("C6"),
"The height of this row has been adjusted by Excel when the "
"file is opened due to the text wrap property being set. "
"Unfortunately this means that the height of the row is "
"unknown to libxlsxwriter at run time and thus the comment "
"box is stretched as well.\n\n"
"Use worksheet_set_row() to specify the row height explicitly "
"to avoid this problem.",
text_wrap);
worksheet_write_comment(worksheet8, CELL("C6"), "Hello");
return workbook_close(workbook);
}

View File

@ -0,0 +1,55 @@
/*
* An a simple example of how to add conditional formatting to an
* libxlsxwriter file.
*
* See conditional_format.c for a more comprehensive example.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("conditional_format_simple.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some sample data. */
worksheet_write_number(worksheet, CELL("B1"), 34, NULL);
worksheet_write_number(worksheet, CELL("B2"), 32, NULL);
worksheet_write_number(worksheet, CELL("B3"), 31, NULL);
worksheet_write_number(worksheet, CELL("B4"), 35, NULL);
worksheet_write_number(worksheet, CELL("B5"), 36, NULL);
worksheet_write_number(worksheet, CELL("B6"), 30, NULL);
worksheet_write_number(worksheet, CELL("B7"), 38, NULL);
worksheet_write_number(worksheet, CELL("B8"), 38, NULL);
worksheet_write_number(worksheet, CELL("B9"), 32, NULL);
/* Add a format with red text. */
lxw_format *custom_format = workbook_add_format(workbook);
format_set_font_color(custom_format, LXW_COLOR_RED);
/* Create a conditional format object. A static object would also work. */
lxw_conditional_format *conditional_format = calloc(1, sizeof(lxw_conditional_format));
/* Set the format type: a cell conditional: */
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
/* Set the criteria to use: */
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_LESS_THAN;
/* Set the value to which the criteria will be applied: */
conditional_format->value = 33;
/* Set the format to use if the criteria/value applies: */
conditional_format->format = custom_format;
/* Now apply the format to data range. */
worksheet_conditional_format_range(worksheet, RANGE("B1:B9"), conditional_format);
/* Free the object and close the file. */
free(conditional_format);
return workbook_close(workbook);
}

View File

@ -0,0 +1,400 @@
/*
* 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-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Write some data to the worksheet. */
void write_worksheet_data(lxw_worksheet *worksheet) {
uint8_t data[10][10] = {
{34, 72, 38, 30, 75, 48, 75, 66, 84, 86},
{6, 24, 1, 84, 54, 62, 60, 3, 26, 59},
{28, 79, 97, 13, 85, 93, 93, 22, 5, 14},
{27, 71, 40, 17, 18, 79, 90, 93, 29, 47},
{88, 25, 33, 23, 67, 1, 59, 79, 47, 36},
{24, 100, 20, 88, 29, 33, 38, 54, 54, 88},
{6, 57, 88, 28, 10, 26, 37, 7, 41, 48},
{52, 78, 1, 96, 26, 45, 47, 33, 96, 36},
{60, 54, 81, 66, 81, 90, 80, 93, 12, 55},
{70, 5, 46, 14, 71, 19, 66, 36, 41, 21},
};
int row, col;
for (row = 0; row < 10; row++)
for (col = 0; col < 10; col++)
worksheet_write_number(worksheet, row +2, col +1, data[row][col], NULL);
}
/* Reset the conditional format options back to their initial state. */
void reset_conditional_format(lxw_conditional_format *conditional_format) {
memset(conditional_format, 0, sizeof(lxw_conditional_format));
}
int main() {
lxw_workbook *workbook = workbook_new("conditional_format.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);
/* Add a format. Light red fill with dark red text. */
lxw_format *format1 = workbook_add_format(workbook);
format_set_bg_color(format1, 0xFFC7CE);
format_set_font_color(format1, 0x9C0006);
/* Add a format. Green fill with dark green text. */
lxw_format *format2 = workbook_add_format(workbook);
format_set_bg_color(format2, 0xC6EFCE);
format_set_font_color(format2, 0x006100);
/* Create a single conditional format object to reuse in the examples. */
lxw_conditional_format *conditional_format = calloc(1, sizeof(lxw_conditional_format));
/*
* Example 1. Conditional formatting based on simple cell based criteria.
*/
write_worksheet_data(worksheet1);
worksheet_write_string(worksheet1,
CELL("A1"),
"Cells with values >= 50 are in light red. "
"Values < 50 are in light green.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_GREATER_THAN_OR_EQUAL_TO;
conditional_format->value = 50;
conditional_format->format = format1;
worksheet_conditional_format_range(worksheet1, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_LESS_THAN;
conditional_format->value = 50;
conditional_format->format = format2;
worksheet_conditional_format_range(worksheet1, RANGE("B3:K12"), conditional_format);
/*
* Example 2. Conditional formatting based on max and min values.
*/
write_worksheet_data(worksheet2);
worksheet_write_string(worksheet2,
CELL("A1"),
"Values between 30 and 70 are in light red. "
"Values outside that range are in light green.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_BETWEEN;
conditional_format->min_value = 30;
conditional_format->max_value = 70;
conditional_format->format = format1;
worksheet_conditional_format_range(worksheet2, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_NOT_BETWEEN;
conditional_format->min_value = 30;
conditional_format->max_value = 70;
conditional_format->format = format2;
worksheet_conditional_format_range(worksheet2, RANGE("B3:K12"), conditional_format);
/*
* Example 3. Conditional formatting with duplicate and unique values.
*/
write_worksheet_data(worksheet3);
worksheet_write_string(worksheet3,
CELL("A1"),
"Duplicate values are in light red. "
"Unique values are in light green.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_DUPLICATE;
conditional_format->format = format1;
worksheet_conditional_format_range(worksheet3, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_UNIQUE;
conditional_format->format = format2;
worksheet_conditional_format_range(worksheet3, RANGE("B3:K12"), conditional_format);
/*
* Example 4. Conditional formatting with above and below average values.
*/
write_worksheet_data(worksheet4);
worksheet_write_string(worksheet4,
CELL("A1"),
"Above average values are in light red. "
"Below average values are in light green.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_AVERAGE;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_AVERAGE_ABOVE;
conditional_format->format = format1;
worksheet_conditional_format_range(worksheet4, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_AVERAGE;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_AVERAGE_BELOW;
conditional_format->format = format2;
worksheet_conditional_format_range(worksheet4, RANGE("B3:K12"), conditional_format);
/*
* Example 5. Conditional formatting with top and bottom values.
*/
write_worksheet_data(worksheet5);
worksheet_write_string(worksheet5,
CELL("A1"),
"Top 10 values are in light red. "
"Bottom 10 values are in light green.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_TOP;
conditional_format->value = 10;
conditional_format->format = format1;
worksheet_conditional_format_range(worksheet5, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_BOTTOM;
conditional_format->value = 10;
conditional_format->format = format2;
worksheet_conditional_format_range(worksheet5, RANGE("B3:K12"), conditional_format);
/*
* Example 6. Conditional formatting with multiple ranges.
*/
write_worksheet_data(worksheet6);
worksheet_write_string(worksheet6,
CELL("A1"),
"Cells with values >= 50 are in light red."
"Values < 50 are in light green. Non-contiguous ranges.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_GREATER_THAN_OR_EQUAL_TO;
conditional_format->value = 50;
conditional_format->format = format1;
conditional_format->multi_range = "B3:K6 B9:K12";
worksheet_conditional_format_range(worksheet6, RANGE("B3:K12"), conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_LESS_THAN;
conditional_format->value = 50;
conditional_format->format = format2;
conditional_format->multi_range = "B3:K6 B9:K12";
worksheet_conditional_format_range(worksheet6, RANGE("B3:K12"), conditional_format);
/* Reset the options before the next example. */
reset_conditional_format(conditional_format);
/*
* Example 7. Conditional formatting with 2 color scales.
*/
/* Write the worksheet data. */
for (int i = 1; i <= 12; i++) {
worksheet_write_number(worksheet7, i + 1, 1, i, NULL);
worksheet_write_number(worksheet7, i + 1, 3, i, NULL);
worksheet_write_number(worksheet7, i + 1, 6, i, NULL);
worksheet_write_number(worksheet7, i + 1, 8, i, NULL);
}
worksheet_write_string(worksheet7,
CELL("A1"),
"Examples of color scales with default and user colors.",
NULL);
worksheet_write_string(worksheet7, CELL("B2"), "2 Color Scale", NULL);
worksheet_write_string(worksheet7, CELL("D2"), "2 Color Scale + user colors", NULL);
worksheet_write_string(worksheet7, CELL("G2"), "3 Color Scale", NULL);
worksheet_write_string(worksheet7, CELL("I2"), "3 Color Scale + user colors", NULL);
/* 2 color scale with standard colors. */
conditional_format->type = LXW_CONDITIONAL_2_COLOR_SCALE;
worksheet_conditional_format_range(worksheet7, RANGE("B3:B14"), conditional_format);
/* 2 color scale with user defined colors. */
conditional_format->type = LXW_CONDITIONAL_2_COLOR_SCALE;
conditional_format->min_color = 0xFF0000;
conditional_format->max_color = 0x00FF00;
worksheet_conditional_format_range(worksheet7, RANGE("D3:D14"), conditional_format);
/* Reset the colors before the next example. */
reset_conditional_format(conditional_format);
/* 3 color scale with standard colors. */
conditional_format->type = LXW_CONDITIONAL_3_COLOR_SCALE;
worksheet_conditional_format_range(worksheet7, RANGE("G3:G14"), conditional_format);
/* 3 color scale with user defined colors. */
conditional_format->type = LXW_CONDITIONAL_3_COLOR_SCALE;
conditional_format->min_color = 0xC5D9F1;
conditional_format->mid_color = 0x8DB4E3;
conditional_format->max_color = 0x538ED5;
worksheet_conditional_format_range(worksheet7, RANGE("I3:I14"), conditional_format);
reset_conditional_format(conditional_format);
/*
* Example 8. Conditional formatting with data bars.
*/
/* Write the worksheet data. */
for (int i = 1; i <= 12; i++) {
worksheet_write_number(worksheet8, i + 1, 1, i, NULL);
worksheet_write_number(worksheet8, i + 1, 3, i, NULL);
worksheet_write_number(worksheet8, i + 1, 5, i, NULL);
worksheet_write_number(worksheet8, i + 1, 7, i, NULL);
worksheet_write_number(worksheet8, i + 1, 9, i, NULL);
}
int data[] = {-1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0};
for (int i = 1; i <= 12; i++) {
worksheet_write_number(worksheet8, i + 1, 11, data[i -1], NULL);
worksheet_write_number(worksheet8, i + 1, 13, data[i -1], NULL);
}
worksheet_write_string(worksheet8,
CELL("A1"),
"Examples of data bars.",
NULL);
worksheet_write_string(worksheet8, CELL("B2"), "Default data bars", NULL);
worksheet_write_string(worksheet8, CELL("D2"), "Bars only", NULL);
worksheet_write_string(worksheet8, CELL("F2"), "With user color", NULL);
worksheet_write_string(worksheet8, CELL("H2"), "Solid bars", NULL);
worksheet_write_string(worksheet8, CELL("J2"), "Right to left", NULL);
worksheet_write_string(worksheet8, CELL("L2"), "Excel 2010 style", NULL);
worksheet_write_string(worksheet8, CELL("N2"), "Negative same as positive", NULL);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
worksheet_conditional_format_range(worksheet8, RANGE("B3:B14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->bar_only = LXW_TRUE;
worksheet_conditional_format_range(worksheet8, RANGE("D3:D14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->bar_color = 0x63C384;
worksheet_conditional_format_range(worksheet8, RANGE("F3:F14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->bar_solid = LXW_TRUE;
worksheet_conditional_format_range(worksheet8, RANGE("H3:H14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->bar_direction = LXW_CONDITIONAL_BAR_DIRECTION_RIGHT_TO_LEFT;
worksheet_conditional_format_range(worksheet8, RANGE("J3:J14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->data_bar_2010 = LXW_TRUE;
worksheet_conditional_format_range(worksheet8, RANGE("L3:L14"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_DATA_BAR;
conditional_format->bar_negative_color_same = LXW_TRUE;
conditional_format->bar_negative_border_color_same = LXW_TRUE;
worksheet_conditional_format_range(worksheet8, RANGE("N3:N14"), conditional_format);
reset_conditional_format(conditional_format);
/*
* Example 9. Conditional formatting with icon sets.
*/
/* Write the worksheet data. */
for (int i = 1; i <= 3; i++) {
worksheet_write_number(worksheet9, 2, i, i, NULL);
worksheet_write_number(worksheet9, 3, i, i, NULL);
worksheet_write_number(worksheet9, 4, i, i, NULL);
worksheet_write_number(worksheet9, 5, i, i, NULL);
}
for (int i = 1; i <= 4; i++) {
worksheet_write_number(worksheet9, 6, i, i, NULL);
}
for (int i = 1; i <= 5; i++) {
worksheet_write_number(worksheet9, 7, i, i, NULL);
worksheet_write_number(worksheet9, 8, i, i, NULL);
}
worksheet_write_string(worksheet9,
CELL("A1"),
"Examples of conditional formats with icon sets.",
NULL);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_3_TRAFFIC_LIGHTS_UNRIMMED;
worksheet_conditional_format_range(worksheet9, RANGE("B3:D3"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_3_TRAFFIC_LIGHTS_UNRIMMED;
conditional_format->reverse_icons = LXW_TRUE;
worksheet_conditional_format_range(worksheet9, RANGE("B4:D4"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_3_TRAFFIC_LIGHTS_UNRIMMED;
conditional_format->icons_only = LXW_TRUE;
worksheet_conditional_format_range(worksheet9, RANGE("B5:D5"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_3_ARROWS_COLORED;
worksheet_conditional_format_range(worksheet9, RANGE("B6:D6"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_4_ARROWS_COLORED;
worksheet_conditional_format_range(worksheet9, RANGE("B7:E7"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_5_ARROWS_COLORED;
worksheet_conditional_format_range(worksheet9, RANGE("B8:F8"), conditional_format);
reset_conditional_format(conditional_format);
conditional_format->type = LXW_CONDITIONAL_TYPE_ICON_SETS;
conditional_format->icon_style = LXW_CONDITIONAL_ICONS_5_RATINGS;
worksheet_conditional_format_range(worksheet9, RANGE("B9:F9"), conditional_format);
reset_conditional_format(conditional_format);
free(conditional_format);
return workbook_close(workbook);
}

View File

@ -0,0 +1,34 @@
/*
* Example of using libxlsxwriter for writing large files in constant memory
* mode.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_row_t row;
lxw_col_t col;
lxw_row_t max_row = 1000;
lxw_col_t max_col = 50;
/* Set the worksheet options. */
lxw_workbook_options options = {.constant_memory = LXW_TRUE,
.tmpdir = NULL,
.use_zip64 = LXW_FALSE};
/* Create a new workbook with options. */
lxw_workbook *workbook = workbook_new_opt("constant_memory.xlsx", &options);
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
for (row = 0; row < max_row; row++) {
for (col = 0; col < max_col; col++) {
worksheet_write_number(worksheet, row, col, 123.45, NULL);
}
}
return workbook_close(workbook);
}

View File

@ -0,0 +1,310 @@
/*
* Examples of how to add data validation and dropdown lists using the
* libxlsxwriter library.
*
* Data validation is a feature of Excel which allows you to restrict the data
* that a user enters in a cell and to display help and warning messages. It
* also allows you to restrict input to values in a drop down list.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *format) {
worksheet_write_string(worksheet, CELL("A1"),
"Some examples of data validation in libxlsxwriter",
format);
worksheet_write_string(worksheet, CELL("B1"), "Enter values in this column", format);
worksheet_write_string(worksheet, CELL("D1"), "Sample Data", format);
worksheet_write_string(worksheet, CELL("D3"), "Integers", NULL);
worksheet_write_number(worksheet, CELL("E3"), 1, NULL);
worksheet_write_number(worksheet, CELL("F3"), 10, NULL);
worksheet_write_string(worksheet, CELL("D4"), "List Data", NULL);
worksheet_write_string(worksheet, CELL("E4"), "open", NULL);
worksheet_write_string(worksheet, CELL("F4"), "high", NULL);
worksheet_write_string(worksheet, CELL("G4"), "close", NULL);
worksheet_write_string(worksheet, CELL("D5"), "Formula", NULL);
worksheet_write_formula(worksheet, CELL("E5"), "=AND(F5=50,G5=60)", NULL);
worksheet_write_number(worksheet, CELL("F5"), 50, NULL);
worksheet_write_number(worksheet, CELL("G5"), 60, NULL);
}
/*
* Create a worksheet with data validations.
*/
int main() {
lxw_workbook *workbook = workbook_new("data_validate1.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_data_validation *data_validation = calloc(1, sizeof(lxw_data_validation));
/* Add a format to use to highlight the header cells. */
lxw_format *format = workbook_add_format(workbook);
format_set_border(format, LXW_BORDER_THIN);
format_set_fg_color(format, 0xC6EFCE);
format_set_bold(format);
format_set_text_wrap(format);
format_set_align(format, LXW_ALIGN_VERTICAL_CENTER);
format_set_indent(format, 1);
/* Write some data for the validations. */
write_worksheet_data(worksheet, format);
/* Set up layout of the worksheet. */
worksheet_set_column(worksheet, 0, 0, 55, NULL);
worksheet_set_column(worksheet, 1, 1, 15, NULL);
worksheet_set_column(worksheet, 3, 3, 15, NULL);
worksheet_set_row(worksheet, 0, 36, NULL);
/*
* Example 1. Limiting input to an integer in a fixed range.
*/
worksheet_write_string(worksheet,
CELL("A3"),
"Enter an integer between 1 and 10",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number = 1;
data_validation->maximum_number = 10;
worksheet_data_validation_cell(worksheet, CELL("B3"), data_validation);
/*
* Example 2. Limiting input to an integer outside a fixed range.
*/
worksheet_write_string(worksheet,
CELL("A5"),
"Enter an integer not between 1 and 10 (using cell references)",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_NOT_BETWEEN;
data_validation->minimum_formula = "=E3";
data_validation->maximum_formula = "=F3";
worksheet_data_validation_cell(worksheet, CELL("B5"), data_validation);
/*
* Example 3. Limiting input to an integer greater than a fixed value.
*/
worksheet_write_string(worksheet,
CELL("A7"),
"Enter an integer greater than 0",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_GREATER_THAN;
data_validation->value_number = 0;
worksheet_data_validation_cell(worksheet, CELL("B7"), data_validation);
/*
* Example 4. Limiting input to an integer less than a fixed value.
*/
worksheet_write_string(worksheet,
CELL("A9"),
"Enter an integer less than 10",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_LESS_THAN;
data_validation->value_number = 10;
worksheet_data_validation_cell(worksheet, CELL("B9"), data_validation);
/*
* Example 5. Limiting input to a decimal in a fixed range.
*/
worksheet_write_string(worksheet,
CELL("A11"),
"Enter a decimal between 0.1 and 0.5",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_DECIMAL;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number = 0.1;
data_validation->maximum_number = 0.5;
worksheet_data_validation_cell(worksheet, CELL("B11"), data_validation);
/*
* Example 6. Limiting input to a value in a dropdown list.
*/
worksheet_write_string(worksheet,
CELL("A13"),
"Select a value from a drop down list",
NULL);
char *list[] = {"open", "high", "close", NULL};
data_validation->validate = LXW_VALIDATION_TYPE_LIST;
data_validation->value_list = list;
worksheet_data_validation_cell(worksheet, CELL("B13"), data_validation);
/*
* Example 7. Limiting input to a value in a dropdown list.
*/
worksheet_write_string(worksheet,
CELL("A15"),
"Select a value from a drop down list (using a cell range)",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_LIST;
data_validation->value_formula = "=$E$4:$G$4";
worksheet_data_validation_cell(worksheet, CELL("B15"), data_validation);
/*
* Example 8. Limiting input to a date in a fixed range.
*/
worksheet_write_string(worksheet,
CELL("A17"),
"Enter a date between 1/1/2008 and 12/12/2008",
NULL);
lxw_datetime datetime1 = {2008, 1, 1, 0, 0, 0};
lxw_datetime datetime2 = {2008, 12, 12, 0, 0, 0};
data_validation->validate = LXW_VALIDATION_TYPE_DATE;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_datetime = datetime1;
data_validation->maximum_datetime = datetime2;
worksheet_data_validation_cell(worksheet, CELL("B17"), data_validation);
/*
* Example 9. Limiting input to a time in a fixed range.
*/
worksheet_write_string(worksheet,
CELL("A19"),
"Enter a time between 6:00 and 12:00",
NULL);
lxw_datetime datetime3 = {0, 0, 0, 6, 0, 0};
lxw_datetime datetime4 = {0, 0, 0, 12, 0, 0};
data_validation->validate = LXW_VALIDATION_TYPE_DATE;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_datetime = datetime3;
data_validation->maximum_datetime = datetime4;
worksheet_data_validation_cell(worksheet, CELL("B19"), data_validation);
/*
* Example 10. Limiting input to a string greater than a fixed length.
*/
worksheet_write_string(worksheet,
CELL("A21"),
"Enter a string longer than 3 characters",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_LENGTH;
data_validation->criteria = LXW_VALIDATION_CRITERIA_GREATER_THAN;
data_validation->value_number = 3;
worksheet_data_validation_cell(worksheet, CELL("B21"), data_validation);
/*
* Example 11. Limiting input based on a formula.
*/
worksheet_write_string(worksheet,
CELL("A23"),
"Enter a value if the following is true \"=AND(F5=50,G5=60)\"",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_CUSTOM_FORMULA;
data_validation->value_formula = "=AND(F5=50,G5=60)";
worksheet_data_validation_cell(worksheet, CELL("B23"), data_validation);
/*
* Example 12. Displaying and modifying data validation messages.
*/
worksheet_write_string(worksheet,
CELL("A25"),
"Displays a message when you select the cell",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number = 1;
data_validation->maximum_number = 100;
data_validation->input_title = "Enter an integer:";
data_validation->input_message = "between 1 and 100";
worksheet_data_validation_cell(worksheet, CELL("B25"), data_validation);
/*
* Example 13. Displaying and modifying data validation messages.
*/
worksheet_write_string(worksheet,
CELL("A27"),
"Display a custom error message when integer isn't between 1 and 100",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number = 1;
data_validation->maximum_number = 100;
data_validation->input_title = "Enter an integer:";
data_validation->input_message = "between 1 and 100";
data_validation->error_title = "Input value is not valid!";
data_validation->error_message = "It should be an integer between 1 and 100";
worksheet_data_validation_cell(worksheet, CELL("B27"), data_validation);
/*
* Example 14. Displaying and modifying data validation messages.
*/
worksheet_write_string(worksheet,
CELL("A29"),
"Display a custom info message when integer isn't between 1 and 100",
NULL);
data_validation->validate = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number = 1;
data_validation->maximum_number = 100;
data_validation->input_title = "Enter an integer:";
data_validation->input_message = "between 1 and 100";
data_validation->error_title = "Input value is not valid!";
data_validation->error_message = "It should be an integer between 1 and 100";
data_validation->error_type = LXW_VALIDATION_ERROR_TYPE_INFORMATION;
worksheet_data_validation_cell(worksheet, CELL("B29"), data_validation);
/* Cleanup. */
free(data_validation);
return workbook_close(workbook);
}

View File

@ -0,0 +1,41 @@
/*
* Example of writing a dates and time in Excel using a number with date
* formatting. This demonstrates that dates and times in Excel are just
* formatted real numbers.
*
* An easier approach using a lxw_datetime struct is shown in example
* dates_and_times02.c.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* A number to display as a date. */
double number = 41333.5;
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("date_and_times01.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a format with date formatting. */
lxw_format *format = workbook_add_format(workbook);
format_set_num_format(format, "mmm d yyyy hh:mm AM/PM");
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 20, NULL);
/* Write the number without formatting. */
worksheet_write_number(worksheet, 0, 0, number, NULL ); // 41333.5
/* Write the number with formatting. Note: the worksheet_write_datetime()
* or worksheet_write_unixtime() functions are preferable for writing
* dates and times. This is for demonstration purposes only.
*/
worksheet_write_number(worksheet, 1, 0, number, format); // Feb 28 2013 12:00 PM
return workbook_close(workbook);
}

View File

@ -0,0 +1,34 @@
/*
* Example of writing dates and times in Excel using an lxw_datetime struct
* and date formatting.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* A datetime to display. */
lxw_datetime datetime = {2013, 2, 28, 12, 0, 0.0};
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("date_and_times02.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a format with date formatting. */
lxw_format *format = workbook_add_format(workbook);
format_set_num_format(format, "mmm d yyyy hh:mm AM/PM");
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 20, NULL);
/* Write the datetime without formatting. */
worksheet_write_datetime(worksheet, 0, 0, &datetime, NULL ); // 41333.5
/* Write the datetime with formatting. */
worksheet_write_datetime(worksheet, 1, 0, &datetime, format); // Feb 28 2013 12:00 PM
return workbook_close(workbook);
}

View File

@ -0,0 +1,36 @@
/*
* Example of writing dates and times in Excel using a Unix datetime and date
* formatting.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("date_and_times03.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a format with date formatting. */
lxw_format *format = workbook_add_format(workbook);
format_set_num_format(format, "mmm d yyyy hh:mm AM/PM");
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 20, NULL);
/* Write some Unix datetimes with formatting. */
/* 1970-01-01. The Unix epoch. */
worksheet_write_unixtime(worksheet, 0, 0, 0, format);
/* 2000-01-01. */
worksheet_write_unixtime(worksheet, 1, 0, 1577836800, format);
/* 1900-01-01. */
worksheet_write_unixtime(worksheet, 2, 0, -2208988800, format);
return workbook_close(workbook);
}

View File

@ -0,0 +1,70 @@
/*
* Example of writing dates and times in Excel using different date formats.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* A datetime to display. */
lxw_datetime datetime = {2013, 1, 23, 12, 30, 5.123};
uint32_t row = 0;
uint16_t col = 0;
int i;
/* Examples date and time formats. In the output file compare how changing
* the format strings changes the appearance of the date.
*/
char *date_formats[] = {
"dd/mm/yy",
"mm/dd/yy",
"dd m yy",
"d mm yy",
"d mmm yy",
"d mmmm yy",
"d mmmm yyy",
"d mmmm yyyy",
"dd/mm/yy hh:mm",
"dd/mm/yy hh:mm:ss",
"dd/mm/yy hh:mm:ss.000",
"hh:mm",
"hh:mm:ss",
"hh:mm:ss.000",
};
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("date_and_times04.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a bold format. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Write the column headers. */
worksheet_write_string(worksheet, row, col, "Formatted date", bold);
worksheet_write_string(worksheet, row, col + 1, "Format", bold);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 1, 20, NULL);
/* Write the same date and time using each of the above formats. */
for (i = 0; i < 14; i++) {
row++;
/* Create a format for the date or time.*/
lxw_format *format = workbook_add_format(workbook);
format_set_num_format(format, date_formats[i]);
format_set_align(format, LXW_ALIGN_LEFT);
/* Write the datetime with each format. */
worksheet_write_datetime(worksheet, row, col, &datetime, format);
/* Also write the format string for comparison. */
worksheet_write_string(worksheet, row, col + 1, date_formats[i], NULL);
}
return workbook_close(workbook);
}

View File

@ -0,0 +1,48 @@
/*
* Example of how to create defined names using libxlsxwriter. This method is
* used to define a user friendly name to represent a value, a single cell or
* a range of cells in a workbook.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("defined_name.xlsx");
lxw_worksheet *worksheet;
/* We don't use the returned worksheets in this example and use a generic
* loop instead. */
workbook_add_worksheet(workbook, NULL);
workbook_add_worksheet(workbook, NULL);
/* Define some global/workbook names. */
workbook_define_name(workbook, "Sales", "=!G1:H10");
workbook_define_name(workbook, "Exchange_rate", "=0.96");
workbook_define_name(workbook, "Sales", "=Sheet1!$G$1:$H$10");
/* Define a local/worksheet name. */
workbook_define_name(workbook, "Sheet2!Sales", "=Sheet2!$G$1:$G$10");
/* Write some text to the worksheets and one of the defined name in a formula. */
LXW_FOREACH_WORKSHEET(worksheet, workbook){
worksheet_set_column(worksheet, 0, 0, 45, NULL);
worksheet_write_string(worksheet, 0, 0,
"This worksheet contains some defined names.", NULL);
worksheet_write_string(worksheet, 1, 0,
"See Formulas -> Name Manager above.", NULL);
worksheet_write_string(worksheet, 2, 0,
"Example formula in cell B3 ->", NULL);
worksheet_write_formula(worksheet, 2, 1, "=Exchange_rate", NULL);
}
return workbook_close(workbook);
}

View File

@ -0,0 +1,41 @@
/*
* A simple example of some of the features of the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("demo.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a format. */
lxw_format *format = workbook_add_format(workbook);
/* Set the bold property for the format */
format_set_bold(format);
/* Change the column width for clarity. */
worksheet_set_column(worksheet, 0, 0, 20, NULL);
/* Write some simple text. */
worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
/* Text with formatting. */
worksheet_write_string(worksheet, 1, 0, "World", format);
/* Write some numbers. */
worksheet_write_number(worksheet, 2, 0, 123, NULL);
worksheet_write_number(worksheet, 3, 0, 123.456, NULL);
/* Insert an image. */
worksheet_insert_image(worksheet, 1, 2, "logo.png");
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,40 @@
/*
* A simple formatting example that demonstrates how to add diagonal
* cell borders using the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("diagonal_border.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add some diagonal border formats. */
lxw_format *format1 = workbook_add_format(workbook);
format_set_diag_type( format1, LXW_DIAGONAL_BORDER_UP);
lxw_format *format2 = workbook_add_format(workbook);
format_set_diag_type( format2, LXW_DIAGONAL_BORDER_DOWN);
lxw_format *format3 = workbook_add_format(workbook);
format_set_diag_type( format3, LXW_DIAGONAL_BORDER_UP_DOWN);
lxw_format *format4 = workbook_add_format(workbook);
format_set_diag_type( format4, LXW_DIAGONAL_BORDER_UP_DOWN);
format_set_diag_border(format4, LXW_BORDER_HAIR);
format_set_diag_color( format4, LXW_COLOR_RED);
worksheet_write_string(worksheet, CELL("B3"), "Text", format1);
worksheet_write_string(worksheet, CELL("B6"), "Text", format2);
worksheet_write_string(worksheet, CELL("B9"), "Text", format3);
worksheet_write_string(worksheet, CELL("B12"), "Text", format4);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,34 @@
/*
* Example of setting custom document properties for an Excel spreadsheet
* using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("doc_custom_properties.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_datetime datetime = {2016, 12, 12, 0, 0, 0.0};
/* Set some custom document properties in the workbook. */
workbook_set_custom_property_string (workbook, "Checked by", "Eve");
workbook_set_custom_property_datetime(workbook, "Date completed", &datetime);
workbook_set_custom_property_number (workbook, "Document number", 12345);
workbook_set_custom_property_number (workbook, "Reference number", 1.2345);
workbook_set_custom_property_boolean (workbook, "Has Review", 1);
workbook_set_custom_property_boolean (workbook, "Signed off", 0);
/* Add some text to the file. */
worksheet_set_column(worksheet, 0, 0, 50, NULL);
worksheet_write_string(worksheet, 0, 0,
"Select 'Workbook Properties' to see properties." , NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,40 @@
/*
* Example of setting document properties such as Author, Title, etc., for an
* Excel spreadsheet using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("doc_properties.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Create a properties structure and set some of the fields. */
lxw_doc_properties properties = {
.title = "This is an example spreadsheet",
.subject = "With document properties",
.author = "John McNamara",
.manager = "Dr. Heinz Doofenshmirtz",
.company = "of Wolves",
.category = "Example spreadsheets",
.keywords = "Sample, Example, Properties",
.comments = "Created with libxlsxwriter",
.status = "Quo",
};
/* Set the properties in the workbook. */
workbook_set_properties(workbook, &properties);
/* Add some text to the file. */
worksheet_set_column(worksheet, 0, 0, 50, NULL);
worksheet_write_string(worksheet, 0, 0,
"Select 'Workbook Properties' to see properties." , NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,303 @@
/*
* An example of how to use libxlsxwriter to write functions that create
* dynamic arrays. These functions are new to Excel 365. The examples mirror
* the examples in the Excel documentation on these functions.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *header);
int main() {
lxw_workbook *workbook = workbook_new("dynamic_arrays.xlsx");
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Filter");
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Unique");
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Sort");
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Sortby");
lxw_worksheet *worksheet5 = workbook_add_worksheet(workbook, "Xlookup");
lxw_worksheet *worksheet6 = workbook_add_worksheet(workbook, "Xmatch");
lxw_worksheet *worksheet7 = workbook_add_worksheet(workbook, "Randarray");
lxw_worksheet *worksheet8 = workbook_add_worksheet(workbook, "Sequence");
lxw_worksheet *worksheet9 = workbook_add_worksheet(workbook, "Spill ranges");
lxw_worksheet *worksheet10 = workbook_add_worksheet(workbook, "Older functions");
lxw_format *header1 = workbook_add_format(workbook);
format_set_bg_color(header1, 0x74AC4C);
format_set_font_color(header1, 0xFFFFFF);
lxw_format *header2 = workbook_add_format(workbook);
format_set_bg_color(header2, 0x528FD3);
format_set_font_color(header2, 0xFFFFFF);
/*
* Example of using the FILTER() function.
*/
worksheet_write_dynamic_formula(worksheet1, CELL("F2"),
"=_xlfn._xlws.FILTER(A1:D17,C1:C17=K2)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet1, CELL("K1"), "Product", header2);
worksheet_write_string(worksheet1, CELL("K2"), "Apple", NULL );
worksheet_write_string(worksheet1, CELL("F1"), "Region", header2);
worksheet_write_string(worksheet1, CELL("G1"), "Sales Rep", header2);
worksheet_write_string(worksheet1, CELL("H1"), "Product", header2);
worksheet_write_string(worksheet1, CELL("I1"), "Units", header2);
write_worksheet_data(worksheet1, header1);
worksheet_set_column_pixels(worksheet1, COLS("E:E"), 20, NULL);
worksheet_set_column_pixels(worksheet1, COLS("J:J"), 20, NULL);
/*
* Example of using the UNIQUE() function.
*/
worksheet_write_dynamic_formula(worksheet2, CELL("F2"),
"=_xlfn.UNIQUE(B2:B17)",
NULL);
/* A more complex example combining SORT and UNIQUE. */
worksheet_write_dynamic_formula(worksheet2, CELL("H2"),
"=_xlfn._xlws.SORT(_xlfn.UNIQUE(B2:B17))",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet2, CELL("F1"), "Sales Rep", header2);
worksheet_write_string(worksheet2, CELL("H1"), "Sales Rep", header2);
write_worksheet_data(worksheet2, header1);
worksheet_set_column_pixels(worksheet2, COLS("E:E"), 20, NULL);
worksheet_set_column_pixels(worksheet2, COLS("G:G"), 20, NULL);
/*
* Example of using the SORT() function.
*/
worksheet_write_dynamic_formula(worksheet3, CELL("F2"),
"=_xlfn._xlws.SORT(B2:B17)",
NULL);
/* A more complex example combining SORT and FILTER. */
worksheet_write_dynamic_formula(worksheet3, CELL("H2"),
"=_xlfn._xlws.SORT(_xlfn._xlws.FILTER(C2:D17,D2:D17>5000,\"\"),2,1)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet3, CELL("F1"), "Sales Rep", header2);
worksheet_write_string(worksheet3, CELL("H1"), "Product", header2);
worksheet_write_string(worksheet3, CELL("I1"), "Units", header2);
write_worksheet_data(worksheet3, header1);
worksheet_set_column_pixels(worksheet3, COLS("E:E"), 20, NULL);
worksheet_set_column_pixels(worksheet3, COLS("G:G"), 20, NULL);
/*
* Example of using the SORTBY() function.
*/
worksheet_write_dynamic_formula(worksheet4, CELL("D2"),
"=_xlfn.SORTBY(A2:B9,B2:B9)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet4, CELL("A1"), "Name", header1);
worksheet_write_string(worksheet4, CELL("B1"), "Age", header1);
worksheet_write_string(worksheet4, CELL("A2"), "Tom", NULL);
worksheet_write_string(worksheet4, CELL("A3"), "Fred", NULL);
worksheet_write_string(worksheet4, CELL("A4"), "Amy", NULL);
worksheet_write_string(worksheet4, CELL("A5"), "Sal", NULL);
worksheet_write_string(worksheet4, CELL("A6"), "Fritz", NULL);
worksheet_write_string(worksheet4, CELL("A7"), "Srivan", NULL);
worksheet_write_string(worksheet4, CELL("A8"), "Xi", NULL);
worksheet_write_string(worksheet4, CELL("A9"), "Hector", NULL);
worksheet_write_number(worksheet4, CELL("B2"), 52, NULL);
worksheet_write_number(worksheet4, CELL("B3"), 65, NULL);
worksheet_write_number(worksheet4, CELL("B4"), 22, NULL);
worksheet_write_number(worksheet4, CELL("B5"), 73, NULL);
worksheet_write_number(worksheet4, CELL("B6"), 19, NULL);
worksheet_write_number(worksheet4, CELL("B7"), 39, NULL);
worksheet_write_number(worksheet4, CELL("B8"), 19, NULL);
worksheet_write_number(worksheet4, CELL("B9"), 66, NULL);
worksheet_write_string(worksheet4, CELL("D1"), "Name", header2);
worksheet_write_string(worksheet4, CELL("E1"), "Age", header2);
worksheet_set_column_pixels(worksheet4, COLS("C:C"), 20, NULL);
/*
* Example of using the XLOOKUP() function.
*/
worksheet_write_dynamic_formula(worksheet5, CELL("F1"),
"=_xlfn.XLOOKUP(E1,A2:A9,C2:C9)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet5, CELL("A1"), "Country", header1);
worksheet_write_string(worksheet5, CELL("B1"), "Abr", header1);
worksheet_write_string(worksheet5, CELL("C1"), "Prefix", header1);
worksheet_write_string(worksheet5, CELL("A2"), "China", NULL);
worksheet_write_string(worksheet5, CELL("A3"), "India", NULL);
worksheet_write_string(worksheet5, CELL("A4"), "United States", NULL);
worksheet_write_string(worksheet5, CELL("A5"), "Indonesia", NULL);
worksheet_write_string(worksheet5, CELL("A6"), "Brazil", NULL);
worksheet_write_string(worksheet5, CELL("A7"), "Pakistan", NULL);
worksheet_write_string(worksheet5, CELL("A8"), "Nigeria", NULL);
worksheet_write_string(worksheet5, CELL("A9"), "Bangladesh", NULL);
worksheet_write_string(worksheet5, CELL("B2"), "CN", NULL);
worksheet_write_string(worksheet5, CELL("B3"), "IN", NULL);
worksheet_write_string(worksheet5, CELL("B4"), "US", NULL);
worksheet_write_string(worksheet5, CELL("B5"), "ID", NULL);
worksheet_write_string(worksheet5, CELL("B6"), "BR", NULL);
worksheet_write_string(worksheet5, CELL("B7"), "PK", NULL);
worksheet_write_string(worksheet5, CELL("B8"), "NG", NULL);
worksheet_write_string(worksheet5, CELL("B9"), "BD", NULL);
worksheet_write_number(worksheet5, CELL("C2"), 86, NULL);
worksheet_write_number(worksheet5, CELL("C3"), 91, NULL);
worksheet_write_number(worksheet5, CELL("C4"), 1, NULL);
worksheet_write_number(worksheet5, CELL("C5"), 62, NULL);
worksheet_write_number(worksheet5, CELL("C6"), 55, NULL);
worksheet_write_number(worksheet5, CELL("C7"), 92, NULL);
worksheet_write_number(worksheet5, CELL("C8"), 234, NULL);
worksheet_write_number(worksheet5, CELL("C9"), 880, NULL);
worksheet_write_string(worksheet5, CELL("E1"), "Brazil", header2);
worksheet_set_column_pixels(worksheet5, COLS("A:A"), 100, NULL);
worksheet_set_column_pixels(worksheet5, COLS("D:D"), 20, NULL);
/*
* Example of using the XMATCH() function.
*/
worksheet_write_dynamic_formula(worksheet6, CELL("D2"),
"=_xlfn.XMATCH(C2,A2:A6)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet6, CELL("A1"), "Product", header1);
worksheet_write_string(worksheet6, CELL("A2"), "Apple", NULL);
worksheet_write_string(worksheet6, CELL("A3"), "Grape", NULL);
worksheet_write_string(worksheet6, CELL("A4"), "Pear", NULL);
worksheet_write_string(worksheet6, CELL("A5"), "Banana", NULL);
worksheet_write_string(worksheet6, CELL("A6"), "Cherry", NULL);
worksheet_write_string(worksheet6, CELL("C1"), "Product", header2);
worksheet_write_string(worksheet6, CELL("D1"), "Position", header2);
worksheet_write_string(worksheet6, CELL("C2"), "Grape", NULL);
worksheet_set_column_pixels(worksheet6, COLS("B:B"), 20, NULL);
/*
* Example of using the RANDARRAY() function.
*/
worksheet_write_dynamic_formula(worksheet7, CELL("A1"),
"=_xlfn.RANDARRAY(5,3,1,100, TRUE)",
NULL);
/*
* Example of using the SEQUENCE() function.
*/
worksheet_write_dynamic_formula(worksheet8, CELL("A1"),
"=_xlfn.SEQUENCE(4,5)",
NULL);
/*
* Example of using the Spill range operator.
*/
worksheet_write_dynamic_formula(worksheet9, CELL("H2"),
"=_xlfn.ANCHORARRAY(F2)",
NULL);
worksheet_write_dynamic_formula(worksheet9, CELL("J2"),
"=COUNTA(_xlfn.ANCHORARRAY(F2))",
NULL);
/* Write the data the function will work on. */
worksheet_write_dynamic_formula(worksheet9, CELL("F2"),
"=_xlfn.UNIQUE(B2:B17)",
NULL);
worksheet_write_string(worksheet9, CELL("F1"), "Unique", header2);
worksheet_write_string(worksheet9, CELL("H1"), "Spill", header2);
worksheet_write_string(worksheet9, CELL("J1"), "Spill", header2);
write_worksheet_data(worksheet9, header1);
worksheet_set_column_pixels(worksheet9, COLS("E:E"), 20, NULL);
worksheet_set_column_pixels(worksheet9, COLS("G:G"), 20, NULL);
worksheet_set_column_pixels(worksheet9, COLS("I:I"), 20, NULL);
/*
* Example of using dynamic ranges with older Excel functions.
*/
worksheet_write_dynamic_array_formula(worksheet10, RANGE("B1:B3"),
"=LEN(A1:A3)",
NULL);
/* Write the data the function will work on. */
worksheet_write_string(worksheet10, CELL("A1"), "Foo", NULL);
worksheet_write_string(worksheet10, CELL("A2"), "Food", NULL);
worksheet_write_string(worksheet10, CELL("A3"), "Frood", NULL);
return workbook_close(workbook);
}
/* A simple function and data structure to populate some of the worksheets. */
struct worksheet_data {
char col1[10];
char col2[10];
char col3[10];
int col4;
};
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *header) {
struct worksheet_data data[160] = {
{"East", "Tom", "Apple", 6380},
{"West", "Fred", "Grape", 5619},
{"North", "Amy", "Pear", 4565},
{"South", "Sal", "Banana", 5323},
{"East", "Fritz", "Apple", 4394},
{"West", "Sravan", "Grape", 7195},
{"North", "Xi", "Pear", 5231},
{"South", "Hector", "Banana", 2427},
{"East", "Tom", "Banana", 4213},
{"West", "Fred", "Pear", 3239},
{"North", "Amy", "Grape", 6520},
{"South", "Sal", "Apple", 1310},
{"East", "Fritz", "Banana", 6274},
{"West", "Sravan", "Pear", 4894},
{"North", "Xi", "Grape", 7580},
{"South", "Hector", "Apple", 9814},
};
worksheet_write_string(worksheet, CELL("A1"), "Region", header);
worksheet_write_string(worksheet, CELL("B1"), "Sales Rep", header);
worksheet_write_string(worksheet, CELL("C1"), "Product", header);
worksheet_write_string(worksheet, CELL("D1"), "Units", header);
for (int row = 0; row < 16; row++) {
worksheet_write_string(worksheet, row + 1, 0, data[row].col1, NULL);
worksheet_write_string(worksheet, row + 1, 1, data[row].col2, NULL);
worksheet_write_string(worksheet, row + 1, 2, data[row].col3, NULL);
worksheet_write_number(worksheet, row + 1, 3, data[row].col4, NULL);
}
}

View File

@ -0,0 +1,46 @@
/*
* Example of writing some data with font formatting to a simple Excel
* file using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook. */
lxw_workbook *workbook = workbook_new("format_font.xlsx");
/* Add a worksheet. */
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 20, NULL);
/* Add some formats. */
lxw_format *format1 = workbook_add_format(workbook);
lxw_format *format2 = workbook_add_format(workbook);
lxw_format *format3 = workbook_add_format(workbook);
/* Set the bold property for format 1. */
format_set_bold(format1);
/* Set the italic property for format 2. */
format_set_italic(format2);
/* Set the bold and italic properties for format 3. */
format_set_bold (format3);
format_set_italic(format3);
/* Write some formatted strings. */
worksheet_write_string(worksheet, 0, 0, "This is bold", format1);
worksheet_write_string(worksheet, 1, 0, "This is italic", format2);
worksheet_write_string(worksheet, 2, 0, "Bold and italic", format3);
/* Close the workbook, save the file and free any memory. */
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,68 @@
/*
* Example of writing some data with numeric formatting to a simple Excel file
* using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("format_num_format.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 30, NULL);
/* Add some formats. */
lxw_format *format01 = workbook_add_format(workbook);
lxw_format *format02 = workbook_add_format(workbook);
lxw_format *format03 = workbook_add_format(workbook);
lxw_format *format04 = workbook_add_format(workbook);
lxw_format *format05 = workbook_add_format(workbook);
lxw_format *format06 = workbook_add_format(workbook);
lxw_format *format07 = workbook_add_format(workbook);
lxw_format *format08 = workbook_add_format(workbook);
lxw_format *format09 = workbook_add_format(workbook);
lxw_format *format10 = workbook_add_format(workbook);
lxw_format *format11 = workbook_add_format(workbook);
/* Set some example number formats. */
format_set_num_format(format01, "0.000");
format_set_num_format(format02, "#,##0");
format_set_num_format(format03, "#,##0.00");
format_set_num_format(format04, "0.00");
format_set_num_format(format05, "mm/dd/yy");
format_set_num_format(format06, "mmm d yyyy");
format_set_num_format(format07, "d mmmm yyyy");
format_set_num_format(format08, "dd/mm/yyyy hh:mm AM/PM");
format_set_num_format(format09, "0 \"dollar and\" .00 \"cents\"");
/* Write data using the formats. */
worksheet_write_number(worksheet, 0, 0, 3.1415926, NULL); // 3.1415926
worksheet_write_number(worksheet, 1, 0, 3.1415926, format01); // 3.142
worksheet_write_number(worksheet, 2, 0, 1234.56, format02); // 1,235
worksheet_write_number(worksheet, 3, 0, 1234.56, format03); // 1,234.56
worksheet_write_number(worksheet, 4, 0, 49.99, format04); // 49.99
worksheet_write_number(worksheet, 5, 0, 36892.521, format05); // 01/01/01
worksheet_write_number(worksheet, 6, 0, 36892.521, format06); // Jan 1 2001
worksheet_write_number(worksheet, 7, 0, 36892.521, format07); // 1 January 2001
worksheet_write_number(worksheet, 8, 0, 36892.521, format08); // 01/01/2001 12:30 AM
worksheet_write_number(worksheet, 9, 0, 1.87, format09); // 1 dollar and .87 cents
/* Show limited conditional number formats. */
format_set_num_format(format10, "[Green]General;[Red]-General;General");
worksheet_write_number(worksheet, 10, 0, 123, format10); // > 0 Green
worksheet_write_number(worksheet, 11, 0, -45, format10); // < 0 Red
worksheet_write_number(worksheet, 12, 0, 0, format10); // = 0 Default color
/* Format a Zip code. */
format_set_num_format(format11, "00000");
worksheet_write_number(worksheet, 13, 0, 1209, format11);
/* Close the workbook, save the file and free any memory. */
return workbook_close(workbook);
}

View File

@ -0,0 +1,132 @@
/*
* This program shows several examples of how to set up headers and
* footers with libxlsxwriter.
*
* The control characters used in the header/footer strings are:
*
* Control Category Description
* ======= ======== ===========
* &L Justification Left
* &C Center
* &R Right
*
* &P Information Page number
* &N Total number of pages
* &D Date
* &T Time
* &F File name
* &A Worksheet name
*
* &fontsize Font Font size
* &"font,style" Font name and style
* &U Single underline
* &E Double underline
* &S Strikethrough
* &X Superscript
* &Y Subscript
*
* &[Picture] Images Image placeholder
* &G Same as &[Picture]
*
* && Miscellaneous Literal ampersand &
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("headers_footers.xlsx");
char preview[] = "Select Print Preview to see the header and footer";
/*
* A simple example to start
*/
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Simple");
char header1[] = "&CHere is some centered text.";
char footer1[] = "&LHere is some left aligned text.";
worksheet_set_header(worksheet1, header1);
worksheet_set_footer(worksheet1, footer1);
worksheet_set_column(worksheet1, 0, 0, 50, NULL);
worksheet_write_string(worksheet1, 0, 0, preview, NULL);
/*
* A simple example to start
*/
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Image");
lxw_header_footer_options header_options = {.image_left = "logo_small.png"};
worksheet_set_header_opt(worksheet2, "&L&[Picture]", &header_options);
worksheet_set_margins(worksheet2, -1, -1, 1.3, -1);
worksheet_set_column(worksheet2, 0, 0, 50, NULL);
worksheet_write_string(worksheet2, 0, 0, preview, NULL);
/*
* This is an example of some of the header/footer variables.
*/
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Variables");
char header3[] = "&LPage &P of &N" "&CFilename: &F" "&RSheetname: &A";
char footer3[] = "&LCurrent date: &D" "&RCurrent time: &T";
lxw_row_t breaks[] = {20, 0};
worksheet_set_header(worksheet3, header3);
worksheet_set_footer(worksheet3, footer3);
worksheet_set_column(worksheet3, 0, 0, 50, NULL);
worksheet_write_string(worksheet3, 0, 0, preview, NULL);
worksheet_set_h_pagebreaks(worksheet3, breaks);
worksheet_write_string(worksheet3, 20, 0, "Next page", NULL);
/*
* This example shows how to use more than one font.
*/
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Mixed fonts");
char header4[] = "&C&\"Courier New,Bold\"Hello &\"Arial,Italic\"World";
char footer4[] = "&C&\"Symbol\"e&\"Arial\" = mc&X2";
worksheet_set_header(worksheet4, header4);
worksheet_set_footer(worksheet4, footer4);
worksheet_set_column(worksheet4, 0, 0, 50, NULL);
worksheet_write_string(worksheet4, 0, 0, preview, NULL);
/*
* Example of line wrapping.
*/
lxw_worksheet *worksheet5 = workbook_add_worksheet(workbook, "Word wrap");
char header5[] = "&CHeading 1\nHeading 2";
worksheet_set_header(worksheet5, header5);
worksheet_set_column(worksheet5, 0, 0, 50, NULL);
worksheet_write_string(worksheet5, 0, 0, preview, NULL);
/*
* Example of inserting a literal ampersand &
*/
lxw_worksheet *worksheet6 = workbook_add_worksheet(workbook, "Ampersand");
char header6[] = "&CCuriouser && Curiouser - Attorneys at Law";
worksheet_set_header(worksheet6, header6);
worksheet_set_column(worksheet6, 0, 0, 50, NULL);
worksheet_write_string(worksheet6, 0, 0, preview, NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,21 @@
/*
* Example of writing some data to a simple Excel file using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("hello_world.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
worksheet_write_number(worksheet, 1, 0, 123, NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,42 @@
/*
* An example of how to hide rows and columns using the libxlsxwriter
* library.
*
* In order to hide rows without setting each one, (of approximately 1 million
* rows), Excel uses an optimization to hide all rows that don't have data. In
* Libxlsxwriter we replicate that using the worksheet_set_default_row()
* function.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("hide_row_col.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_row_t row;
/* Write some data. */
worksheet_write_string(worksheet, 0, 3, "Some hidden columns.", NULL);
worksheet_write_string(worksheet, 7, 0, "Some hidden rows.", NULL);
/* Hide all rows without data. */
worksheet_set_default_row(worksheet, 15, LXW_TRUE);
/* Set the height of empty rows that we want to display even if it is */
/* the default height. */
for (row = 1; row <= 6; row++)
worksheet_set_row(worksheet, row, 15, NULL);
/* Columns can be hidden explicitly. This doesn't increase the file size. */
lxw_row_col_options options = {.hidden = 1};
worksheet_set_column_opt(worksheet, COLS("G:XFD"), 8.43, NULL, &options);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,32 @@
/*
* Example of how to hide a worksheet using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("hide_sheet.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);
/* Hide Sheet2. It won't be visible until it is unhidden in Excel. */
worksheet_hide(worksheet2);
worksheet_write_string(worksheet1, 0, 0, "Sheet2 is hidden", NULL);
worksheet_write_string(worksheet2, 0, 0, "Now it's my turn to find you!", NULL);
worksheet_write_string(worksheet3, 0, 0, "Sheet2 is hidden", NULL);
/* Make the first column wider to make the text clearer. */
worksheet_set_column(worksheet1, 0, 0, 30, NULL);
worksheet_set_column(worksheet2, 0, 0, 30, NULL);
worksheet_set_column(worksheet3, 0, 0, 30, NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,53 @@
/*
* Example of writing urls/hyperlinks with the libxlsxwriter library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook. */
lxw_workbook *workbook = workbook_new("hyperlinks.xlsx");
/* Add a worksheet. */
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Get the default url format (used in the overwriting examples below). */
lxw_format *url_format = workbook_get_default_url_format(workbook);
/* Create a user defined link format. */
lxw_format *red_format = workbook_add_format(workbook);
format_set_underline (red_format, LXW_UNDERLINE_SINGLE);
format_set_font_color(red_format, LXW_COLOR_RED);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 30, NULL);
/* Write a hyperlink. A default blue underline will be used if the format is NULL. */
worksheet_write_url(worksheet, 0, 0, "http://libxlsxwriter.github.io", NULL);
/* Write a hyperlink but overwrite the displayed string. Note, we need to
* specify the format for the string to match the default hyperlink. */
worksheet_write_url (worksheet, 2, 0, "http://libxlsxwriter.github.io", NULL);
worksheet_write_string(worksheet, 2, 0, "Read the documentation.", url_format);
/* Write a hyperlink with a different format. */
worksheet_write_url(worksheet, 4, 0, "http://libxlsxwriter.github.io", red_format);
/* Write a mail hyperlink. */
worksheet_write_url (worksheet, 6, 0, "mailto:jmcnamara@cpan.org", NULL);
/* Write a mail hyperlink and overwrite the displayed string. We again
* specify the format for the string to match the default hyperlink. */
worksheet_write_url (worksheet, 8, 0, "mailto:jmcnamara@cpan.org", NULL);
worksheet_write_string(worksheet, 8, 0, "Drop me a line.", url_format);
/* Close the workbook, save the file and free any memory. */
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,38 @@
/*
* An example of turning off worksheet cells errors/warnings using
* libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("ignore_errors.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write strings that looks like numbers. This will cause an Excel warning. */
worksheet_write_string(worksheet, CELL("C2"), "123", NULL);
worksheet_write_string(worksheet, CELL("C3"), "123", NULL);
/* Write a divide by zero formula. This will also cause an Excel warning. */
worksheet_write_formula(worksheet, CELL("C5"), "=1/0", NULL);
worksheet_write_formula(worksheet, CELL("C6"), "=1/0", NULL);
/* Turn off some of the warnings: */
worksheet_ignore_errors(worksheet, LXW_IGNORE_NUMBER_STORED_AS_TEXT, "C3");
worksheet_ignore_errors(worksheet, LXW_IGNORE_EVAL_ERROR, "C6");
/* Write some descriptions for the cells and make the column wider for clarity. */
worksheet_set_column(worksheet, 1, 1, 16, NULL);
worksheet_write_string(worksheet, CELL("B2"), "Warning:", NULL);
worksheet_write_string(worksheet, CELL("B3"), "Warning turned off:", NULL);
worksheet_write_string(worksheet, CELL("B5"), "Warning:", NULL);
worksheet_write_string(worksheet, CELL("B6"), "Warning turned off:", NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,54 @@
/*
* An example of inserting an image from a memory buffer into a worksheet
* using the libxlsxwriter library.
*
* Copyright 2014-2021 John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Simple array with some PNG data. */
unsigned char image_buffer[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00,
0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc,
0x61, 0x05, 0x00, 0x00, 0x00, 0x20, 0x63, 0x48, 0x52, 0x4d, 0x00, 0x00,
0x7a, 0x26, 0x00, 0x00, 0x80, 0x84, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00,
0x80, 0xe8, 0x00, 0x00, 0x75, 0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00,
0x3a, 0x98, 0x00, 0x00, 0x17, 0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00,
0x00, 0x46, 0x49, 0x44, 0x41, 0x54, 0x48, 0x4b, 0x63, 0xfc, 0xcf, 0x40,
0x63, 0x00, 0xb4, 0x80, 0xa6, 0x88, 0xb6, 0xa6, 0x83, 0x82, 0x87, 0xa6,
0xce, 0x1f, 0xb5, 0x80, 0x98, 0xe0, 0x1d, 0x8d, 0x03, 0x82, 0xa1, 0x34,
0x1a, 0x44, 0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45,
0xa3, 0x41, 0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0xa3, 0x41,
0x44, 0x30, 0x04, 0x08, 0x2a, 0x18, 0x4d, 0x45, 0x03, 0x1f, 0x44, 0x00,
0xaa, 0x35, 0xdd, 0x4e, 0xe6, 0xd5, 0xa1, 0x22, 0x00, 0x00, 0x00, 0x00,
0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
unsigned int image_size = 200;
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("image_buffer.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_image_options options = {.x_offset = 34, .y_offset = 4,
.x_scale = 2, .y_scale = 1};
/* Insert the image from the buffer. */
worksheet_insert_image_buffer(worksheet, CELL("B3"), image_buffer, image_size);
/* Insert the image from the same buffer, with some options. */
worksheet_insert_image_buffer_opt(worksheet, CELL("B7"), image_buffer, image_size, &options);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,46 @@
/*
* An example of inserting images into a worksheet using the libxlsxwriter
* library.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
/* Create a new workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("images.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Change some of the column widths for clarity. */
worksheet_set_column(worksheet, COLS("A:A"), 30, NULL);
/* Insert an image. */
worksheet_write_string(worksheet, CELL("A2"), "Insert an image in a cell:", NULL);
worksheet_insert_image(worksheet, CELL("B2"), "logo.png");
/* Insert an image offset in the cell. */
worksheet_write_string(worksheet, CELL("A12"), "Insert an offset image:", NULL);
lxw_image_options options1 = {.x_offset = 15, .y_offset = 10};
worksheet_insert_image_opt(worksheet, CELL("B12"), "logo.png", &options1);
/* Insert an image with scaling. */
worksheet_write_string(worksheet, CELL("A22"), "Insert a scaled image:", NULL);
lxw_image_options options2 = {.x_scale = 0.5, .y_scale = 0.5};
worksheet_insert_image_opt(worksheet, CELL("B22"), "logo.png", &options2);
/* Insert an image with a hyperlink. */
worksheet_write_string(worksheet, CELL("A32"), "Insert an image with a hyperlink:", NULL);
lxw_image_options options3 = {.url = "https://github.com/jmcnamara"};
worksheet_insert_image_opt(worksheet, CELL("B32"), "logo.png", &options3);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,40 @@
/*
* An example of using the new Excel LAMBDA() function with the libxlsxwriter
* library. Note, this function is only currently available if you are
* subscribed to the Microsoft Office Beta Channel program.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("lambda.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_write_string(worksheet, 0, 0,
"Note: Lambda functions currently only work with "
"the Beta Channel versions of Excel 365", NULL);
/* Note that the formula name is prefixed with "_xlfn." and that the
* lambda function parameters are prefixed with "_xlpm.". These prefixes
* won't show up in Excel.
*/
worksheet_write_dynamic_formula(worksheet, CELL("A2"),
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))(32)",
NULL);
/* Create the lambda function as a defined name and write it as a dynamic formula. */
workbook_define_name(workbook,
"ToCelsius",
"=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))");
worksheet_write_dynamic_formula(worksheet, CELL("A3"), "=ToCelsius(212)", NULL);
workbook_close(workbook);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -0,0 +1,25 @@
/*****************************************************************************
*
* An example of adding macros to a libxlsxwriter file using a VBA project
* file extracted from an existing Excel .xlsm file.
*
* The vba_extract.py utility from the libxlsxwriter examples directory can be
* used to extract the vbaProject.bin file.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("macro.xlsm");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a macro that will execute when the file is opened. */
workbook_add_vba_project(workbook, "vbaProject.bin");
worksheet_write_string(worksheet, 0, 0, "Overwrite this", NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,38 @@
/*
* An example of merging cells using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("merge_range.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *merge_format = workbook_add_format(workbook);
/* Configure a format for the merged range. */
format_set_align(merge_format, LXW_ALIGN_CENTER);
format_set_align(merge_format, LXW_ALIGN_VERTICAL_CENTER);
format_set_bold(merge_format);
format_set_bg_color(merge_format, LXW_COLOR_YELLOW);
format_set_border(merge_format, LXW_BORDER_THIN);
/* Increase the cell size of the merged cells to highlight the formatting. */
worksheet_set_column(worksheet, 1, 3, 12, NULL);
worksheet_set_row(worksheet, 3, 30, NULL);
worksheet_set_row(worksheet, 6, 30, NULL);
worksheet_set_row(worksheet, 7, 30, NULL);
/* Merge 3 cells. */
worksheet_merge_range(worksheet, 3, 1, 3, 3, "Merged Range", merge_format);
/* Merge 3 cells over two rows. */
worksheet_merge_range(worksheet, 6, 1, 7, 3, "Merged Range", merge_format);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,49 @@
/*
* An example of merging cells containing a rich string using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("merge_rich_string.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Configure a format for the merged range. */
lxw_format *merge_format = workbook_add_format(workbook);
format_set_align(merge_format, LXW_ALIGN_CENTER);
format_set_align(merge_format, LXW_ALIGN_VERTICAL_CENTER);
format_set_border(merge_format, LXW_BORDER_THIN);
/* Configure formats for the rich string. */
lxw_format *red = workbook_add_format(workbook);
format_set_font_color(red, LXW_COLOR_RED);
lxw_format *blue = workbook_add_format(workbook);
format_set_font_color(blue, LXW_COLOR_BLUE);
/* Create the fragments for the rich string. */
lxw_rich_string_tuple fragment1 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment2 = {.format = red, .string = "red" };
lxw_rich_string_tuple fragment3 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment4 = {.format = blue, .string = "blue" };
lxw_rich_string_tuple *rich_string[] = {&fragment1, &fragment2,
&fragment3, &fragment4, NULL};
/* Write an empty string to the merged range. */
worksheet_merge_range(worksheet, 1, 1, 4, 3, "", merge_format);
/* We then overwrite the first merged cell with a rich string. Note that
* we must also pass the cell format used in the merged cells format at
* the end. */
worksheet_write_rich_string(worksheet, 1, 1, rich_string, merge_format);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,255 @@
/*
* Example of how use libxlsxwriter to generate Excel outlines and grouping.
*
* Excel allows you to group rows or columns so that they can be hidden or
* displayed with a single mouse click. This feature is referred to as
* outlines.
*
* Outlines can reduce complex data down to a few salient sub-totals or
* summaries.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("outline.xlsx");
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Outlined Rows");
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Collapsed Rows");
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Outline Columns");
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Outline levels");
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/*
* Example 1: Create a worksheet with outlined rows. It also includes
* SUBTOTAL() functions so that it looks like the type of automatic
* outlines that are generated when you use the 'Sub Totals' option.
*
* For outlines the important parameters are 'hidden' and 'level'. Rows
* with the same 'level' are grouped together. The group will be collapsed
* if 'hidden' is non-zero.
*/
/* The option structs with the outline level set. */
lxw_row_col_options options1 = {.hidden = 0, .level = 2, .collapsed = 0};
lxw_row_col_options options2 = {.hidden = 0, .level = 1, .collapsed = 0};
/* Set the column width for clarity. */
worksheet_set_column(worksheet1, COLS("A:A"), 20, NULL);
/* Set the row options with the outline level. */
worksheet_set_row_opt(worksheet1, 1, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 2, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 3, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 4, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 5, LXW_DEF_ROW_HEIGHT, NULL, &options2);
worksheet_set_row_opt(worksheet1, 6, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 7, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 8, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 9, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 10, LXW_DEF_ROW_HEIGHT, NULL, &options2);
/* Add data and formulas to the worksheet. */
worksheet_write_string(worksheet1, CELL("A1"), "Region", bold);
worksheet_write_string(worksheet1, CELL("A2"), "North", NULL);
worksheet_write_string(worksheet1, CELL("A3"), "North", NULL);
worksheet_write_string(worksheet1, CELL("A4"), "North", NULL);
worksheet_write_string(worksheet1, CELL("A5"), "North", NULL);
worksheet_write_string(worksheet1, CELL("A6"), "North Total", bold);
worksheet_write_string(worksheet1, CELL("B1"), "Sales", bold);
worksheet_write_number(worksheet1, CELL("B2"), 1000, NULL);
worksheet_write_number(worksheet1, CELL("B3"), 1200, NULL);
worksheet_write_number(worksheet1, CELL("B4"), 900, NULL);
worksheet_write_number(worksheet1, CELL("B5"), 1200, NULL);
worksheet_write_formula(worksheet1, CELL("B6"), "=SUBTOTAL(9,B2:B5)", bold);
worksheet_write_string(worksheet1, CELL("A7"), "South", NULL);
worksheet_write_string(worksheet1, CELL("A8"), "South", NULL);
worksheet_write_string(worksheet1, CELL("A9"), "South", NULL);
worksheet_write_string(worksheet1, CELL("A10"), "South", NULL);
worksheet_write_string(worksheet1, CELL("A11"), "South Total", bold);
worksheet_write_number(worksheet1, CELL("B7"), 400, NULL);
worksheet_write_number(worksheet1, CELL("B8"), 600, NULL);
worksheet_write_number(worksheet1, CELL("B9"), 500, NULL);
worksheet_write_number(worksheet1, CELL("B10"), 600, NULL);
worksheet_write_formula(worksheet1, CELL("B11"), "=SUBTOTAL(9,B7:B10)", bold);
worksheet_write_string(worksheet1, CELL("A12"), "Grand Total", bold);
worksheet_write_formula(worksheet1, CELL("B12"), "=SUBTOTAL(9,B2:B10)", bold);
/*
* Example 2: Create a worksheet with outlined rows. This is the same as
* the previous example except that the rows are collapsed. Note: We need
* to indicate the row that contains the collapsed symbol '+' with the
* optional parameter, 'collapsed'.
*/
/* The option structs with the outline level and collapsed property set. */
lxw_row_col_options options3 = {.hidden = 1, .level = 2, .collapsed = 0};
lxw_row_col_options options4 = {.hidden = 1, .level = 1, .collapsed = 0};
lxw_row_col_options options5 = {.hidden = 0, .level = 0, .collapsed = 1};
/* Set the column width for clarity. */
worksheet_set_column(worksheet2, COLS("A:A"), 20, NULL);
/* Set the row options with the outline level. */
worksheet_set_row_opt(worksheet2, 1, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 2, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 3, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 4, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 5, LXW_DEF_ROW_HEIGHT, NULL, &options4);
worksheet_set_row_opt(worksheet2, 6, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 7, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 8, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 9, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 10, LXW_DEF_ROW_HEIGHT, NULL, &options4);
worksheet_set_row_opt(worksheet2, 11, LXW_DEF_ROW_HEIGHT, NULL, &options5);
/* Add data and formulas to the worksheet. */
worksheet_write_string(worksheet2, CELL("A1"), "Region", bold);
worksheet_write_string(worksheet2, CELL("A2"), "North", NULL);
worksheet_write_string(worksheet2, CELL("A3"), "North", NULL);
worksheet_write_string(worksheet2, CELL("A4"), "North", NULL);
worksheet_write_string(worksheet2, CELL("A5"), "North", NULL);
worksheet_write_string(worksheet2, CELL("A6"), "North Total", bold);
worksheet_write_string(worksheet2, CELL("B1"), "Sales", bold);
worksheet_write_number(worksheet2, CELL("B2"), 1000, NULL);
worksheet_write_number(worksheet2, CELL("B3"), 1200, NULL);
worksheet_write_number(worksheet2, CELL("B4"), 900, NULL);
worksheet_write_number(worksheet2, CELL("B5"), 1200, NULL);
worksheet_write_formula(worksheet2, CELL("B6"), "=SUBTOTAL(9,B2:B5)", bold);
worksheet_write_string(worksheet2, CELL("A7"), "South", NULL);
worksheet_write_string(worksheet2, CELL("A8"), "South", NULL);
worksheet_write_string(worksheet2, CELL("A9"), "South", NULL);
worksheet_write_string(worksheet2, CELL("A10"), "South", NULL);
worksheet_write_string(worksheet2, CELL("A11"), "South Total", bold);
worksheet_write_number(worksheet2, CELL("B7"), 400, NULL);
worksheet_write_number(worksheet2, CELL("B8"), 600, NULL);
worksheet_write_number(worksheet2, CELL("B9"), 500, NULL);
worksheet_write_number(worksheet2, CELL("B10"), 600, NULL);
worksheet_write_formula(worksheet2, CELL("B11"), "=SUBTOTAL(9,B7:B10)", bold);
worksheet_write_string(worksheet2, CELL("A12"), "Grand Total", bold);
worksheet_write_formula(worksheet2, CELL("B12"), "=SUBTOTAL(9,B2:B10)", bold);
/*
* Example 3: Create a worksheet with outlined columns.
*/
lxw_row_col_options options6 = {.hidden = 0, .level = 1, .collapsed = 0};
/* Add data and formulas to the worksheet. */
worksheet_write_string(worksheet3, CELL("A1"), "Month", NULL);
worksheet_write_string(worksheet3, CELL("B1"), "Jan", NULL);
worksheet_write_string(worksheet3, CELL("C1"), "Feb", NULL);
worksheet_write_string(worksheet3, CELL("D1"), "Mar", NULL);
worksheet_write_string(worksheet3, CELL("E1"), "Apr", NULL);
worksheet_write_string(worksheet3, CELL("F1"), "May", NULL);
worksheet_write_string(worksheet3, CELL("G1"), "Jun", NULL);
worksheet_write_string(worksheet3, CELL("H1"), "Total", NULL);
worksheet_write_string(worksheet3, CELL("A2"), "North", NULL);
worksheet_write_number(worksheet3, CELL("B2"), 50, NULL);
worksheet_write_number(worksheet3, CELL("C2"), 20, NULL);
worksheet_write_number(worksheet3, CELL("D2"), 15, NULL);
worksheet_write_number(worksheet3, CELL("E2"), 25, NULL);
worksheet_write_number(worksheet3, CELL("F2"), 65, NULL);
worksheet_write_number(worksheet3, CELL("G2"), 80, NULL);
worksheet_write_formula(worksheet3, CELL("H2"), "=SUM(B2:G2)", NULL);
worksheet_write_string(worksheet3, CELL("A3"), "South", NULL);
worksheet_write_number(worksheet3, CELL("B3"), 10, NULL);
worksheet_write_number(worksheet3, CELL("C3"), 20, NULL);
worksheet_write_number(worksheet3, CELL("D3"), 30, NULL);
worksheet_write_number(worksheet3, CELL("E3"), 50, NULL);
worksheet_write_number(worksheet3, CELL("F3"), 50, NULL);
worksheet_write_number(worksheet3, CELL("G3"), 50, NULL);
worksheet_write_formula(worksheet3, CELL("H3"), "=SUM(B3:G3)", NULL);
worksheet_write_string(worksheet3, CELL("A4"), "East", NULL);
worksheet_write_number(worksheet3, CELL("B4"), 45, NULL);
worksheet_write_number(worksheet3, CELL("C4"), 75, NULL);
worksheet_write_number(worksheet3, CELL("D4"), 50, NULL);
worksheet_write_number(worksheet3, CELL("E4"), 15, NULL);
worksheet_write_number(worksheet3, CELL("F4"), 75, NULL);
worksheet_write_number(worksheet3, CELL("G4"), 100, NULL);
worksheet_write_formula(worksheet3, CELL("H4"), "=SUM(B4:G4)", NULL);
worksheet_write_string(worksheet3, CELL("A5"), "West", NULL);
worksheet_write_number(worksheet3, CELL("B5"), 15, NULL);
worksheet_write_number(worksheet3, CELL("C5"), 15, NULL);
worksheet_write_number(worksheet3, CELL("D5"), 55, NULL);
worksheet_write_number(worksheet3, CELL("E5"), 35, NULL);
worksheet_write_number(worksheet3, CELL("F5"), 20, NULL);
worksheet_write_number(worksheet3, CELL("G5"), 50, NULL);
worksheet_write_formula(worksheet3, CELL("H5"), "=SUM(B5:G5)", NULL);
worksheet_write_formula(worksheet3, CELL("H6"), "=SUM(H2:H5)", bold);
/* Add bold format to the first row. */
worksheet_set_row(worksheet3, 0, LXW_DEF_ROW_HEIGHT, bold);
/* Set column formatting and the outline level. */
worksheet_set_column( worksheet3, COLS("A:A"), 10, bold);
worksheet_set_column_opt(worksheet3, COLS("B:G"), 5, NULL, &options6);
worksheet_set_column( worksheet3, COLS("H:H"), 10, NULL);
/*
* Example 4: Show all possible outline levels.
*/
lxw_row_col_options level1 = {.level = 1, .hidden = 0, .collapsed = 0};
lxw_row_col_options level2 = {.level = 2, .hidden = 0, .collapsed = 0};
lxw_row_col_options level3 = {.level = 3, .hidden = 0, .collapsed = 0};
lxw_row_col_options level4 = {.level = 4, .hidden = 0, .collapsed = 0};
lxw_row_col_options level5 = {.level = 5, .hidden = 0, .collapsed = 0};
lxw_row_col_options level6 = {.level = 6, .hidden = 0, .collapsed = 0};
lxw_row_col_options level7 = {.level = 7, .hidden = 0, .collapsed = 0};
worksheet_write_string(worksheet4, 0, 0, "Level 1", NULL);
worksheet_write_string(worksheet4, 1, 0, "Level 2", NULL);
worksheet_write_string(worksheet4, 2, 0, "Level 3", NULL);
worksheet_write_string(worksheet4, 3, 0, "Level 4", NULL);
worksheet_write_string(worksheet4, 4, 0, "Level 5", NULL);
worksheet_write_string(worksheet4, 5, 0, "Level 6", NULL);
worksheet_write_string(worksheet4, 6, 0, "Level 7", NULL);
worksheet_write_string(worksheet4, 7, 0, "Level 6", NULL);
worksheet_write_string(worksheet4, 8, 0, "Level 5", NULL);
worksheet_write_string(worksheet4, 9, 0, "Level 4", NULL);
worksheet_write_string(worksheet4, 10, 0, "Level 3", NULL);
worksheet_write_string(worksheet4, 11, 0, "Level 2", NULL);
worksheet_write_string(worksheet4, 12, 0, "Level 1", NULL);
worksheet_set_row_opt(worksheet4, 0, LXW_DEF_ROW_HEIGHT, NULL, &level1);
worksheet_set_row_opt(worksheet4, 1, LXW_DEF_ROW_HEIGHT, NULL, &level2);
worksheet_set_row_opt(worksheet4, 2, LXW_DEF_ROW_HEIGHT, NULL, &level3);
worksheet_set_row_opt(worksheet4, 3, LXW_DEF_ROW_HEIGHT, NULL, &level4);
worksheet_set_row_opt(worksheet4, 4, LXW_DEF_ROW_HEIGHT, NULL, &level5);
worksheet_set_row_opt(worksheet4, 5, LXW_DEF_ROW_HEIGHT, NULL, &level6);
worksheet_set_row_opt(worksheet4, 6, LXW_DEF_ROW_HEIGHT, NULL, &level7);
worksheet_set_row_opt(worksheet4, 7, LXW_DEF_ROW_HEIGHT, NULL, &level6);
worksheet_set_row_opt(worksheet4, 8, LXW_DEF_ROW_HEIGHT, NULL, &level5);
worksheet_set_row_opt(worksheet4, 9, LXW_DEF_ROW_HEIGHT, NULL, &level4);
worksheet_set_row_opt(worksheet4, 10, LXW_DEF_ROW_HEIGHT, NULL, &level3);
worksheet_set_row_opt(worksheet4, 11, LXW_DEF_ROW_HEIGHT, NULL, &level2);
worksheet_set_row_opt(worksheet4, 12, LXW_DEF_ROW_HEIGHT, NULL, &level1);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,271 @@
/*
* Example of how use libxlsxwriter to generate Excel outlines and grouping.
*
* These examples focus mainly on collapsed outlines. See also the outlines.c
* example program for more general examples.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* This function will generate the same data and sub-totals on each worksheet.
* Used in the examples 1-4.
*/
void create_row_example_data(lxw_worksheet *worksheet, lxw_format *bold) {
/* Set the column width for clarity. */
worksheet_set_column(worksheet, COLS("A:A"), 20, NULL);
/* Add data and formulas to the worksheet. */
worksheet_write_string(worksheet, CELL("A1"), "Region", bold);
worksheet_write_string(worksheet, CELL("A2"), "North", NULL);
worksheet_write_string(worksheet, CELL("A3"), "North", NULL);
worksheet_write_string(worksheet, CELL("A4"), "North", NULL);
worksheet_write_string(worksheet, CELL("A5"), "North", NULL);
worksheet_write_string(worksheet, CELL("A6"), "North Total", bold);
worksheet_write_string(worksheet, CELL("B1"), "Sales", bold);
worksheet_write_number(worksheet, CELL("B2"), 1000, NULL);
worksheet_write_number(worksheet, CELL("B3"), 1200, NULL);
worksheet_write_number(worksheet, CELL("B4"), 900, NULL);
worksheet_write_number(worksheet, CELL("B5"), 1200, NULL);
worksheet_write_formula(worksheet, CELL("B6"), "=SUBTOTAL(9,B2:B5)", bold);
worksheet_write_string(worksheet, CELL("A7"), "South", NULL);
worksheet_write_string(worksheet, CELL("A8"), "South", NULL);
worksheet_write_string(worksheet, CELL("A9"), "South", NULL);
worksheet_write_string(worksheet, CELL("A10"), "South", NULL);
worksheet_write_string(worksheet, CELL("A11"), "South Total", bold);
worksheet_write_number(worksheet, CELL("B7"), 400, NULL);
worksheet_write_number(worksheet, CELL("B8"), 600, NULL);
worksheet_write_number(worksheet, CELL("B9"), 500, NULL);
worksheet_write_number(worksheet, CELL("B10"), 600, NULL);
worksheet_write_formula(worksheet, CELL("B11"), "=SUBTOTAL(9,B7:B10)", bold);
worksheet_write_string(worksheet, CELL("A12"), "Grand Total", bold);
worksheet_write_formula(worksheet, CELL("B12"), "=SUBTOTAL(9,B2:B10)", bold);
}
/* This function will generate the same data and sub-totals on each worksheet.
* Used in the examples 5-6.
*/
void create_col_example_data(lxw_worksheet *worksheet, lxw_format *bold) {
/* Add data and formulas to the worksheet. */
worksheet_write_string(worksheet, CELL("A1"), "Month", NULL);
worksheet_write_string(worksheet, CELL("B1"), "Jan", NULL);
worksheet_write_string(worksheet, CELL("C1"), "Feb", NULL);
worksheet_write_string(worksheet, CELL("D1"), "Mar", NULL);
worksheet_write_string(worksheet, CELL("E1"), "Apr", NULL);
worksheet_write_string(worksheet, CELL("F1"), "May", NULL);
worksheet_write_string(worksheet, CELL("G1"), "Jun", NULL);
worksheet_write_string(worksheet, CELL("H1"), "Total", NULL);
worksheet_write_string(worksheet, CELL("A2"), "North", NULL);
worksheet_write_number(worksheet, CELL("B2"), 50, NULL);
worksheet_write_number(worksheet, CELL("C2"), 20, NULL);
worksheet_write_number(worksheet, CELL("D2"), 15, NULL);
worksheet_write_number(worksheet, CELL("E2"), 25, NULL);
worksheet_write_number(worksheet, CELL("F2"), 65, NULL);
worksheet_write_number(worksheet, CELL("G2"), 80, NULL);
worksheet_write_formula(worksheet, CELL("H2"), "=SUM(B2:G2)", NULL);
worksheet_write_string(worksheet, CELL("A3"), "South", NULL);
worksheet_write_number(worksheet, CELL("B3"), 10, NULL);
worksheet_write_number(worksheet, CELL("C3"), 20, NULL);
worksheet_write_number(worksheet, CELL("D3"), 30, NULL);
worksheet_write_number(worksheet, CELL("E3"), 50, NULL);
worksheet_write_number(worksheet, CELL("F3"), 50, NULL);
worksheet_write_number(worksheet, CELL("G3"), 50, NULL);
worksheet_write_formula(worksheet, CELL("H3"), "=SUM(B3:G3)", NULL);
worksheet_write_string(worksheet, CELL("A4"), "East", NULL);
worksheet_write_number(worksheet, CELL("B4"), 45, NULL);
worksheet_write_number(worksheet, CELL("C4"), 75, NULL);
worksheet_write_number(worksheet, CELL("D4"), 50, NULL);
worksheet_write_number(worksheet, CELL("E4"), 15, NULL);
worksheet_write_number(worksheet, CELL("F4"), 75, NULL);
worksheet_write_number(worksheet, CELL("G4"), 100, NULL);
worksheet_write_formula(worksheet, CELL("H4"), "=SUM(B4:G4)", NULL);
worksheet_write_string(worksheet, CELL("A5"), "West", NULL);
worksheet_write_number(worksheet, CELL("B5"), 15, NULL);
worksheet_write_number(worksheet, CELL("C5"), 15, NULL);
worksheet_write_number(worksheet, CELL("D5"), 55, NULL);
worksheet_write_number(worksheet, CELL("E5"), 35, NULL);
worksheet_write_number(worksheet, CELL("F5"), 20, NULL);
worksheet_write_number(worksheet, CELL("G5"), 50, NULL);
worksheet_write_formula(worksheet, CELL("H5"), "=SUM(B5:G5)", NULL);
worksheet_write_formula(worksheet, CELL("H6"), "=SUM(H2:H5)", bold);
}
int main() {
lxw_workbook *workbook = workbook_new("outline_collapsed.xlsx");
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Outlined Rows");
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Collapsed Rows 1");
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Collapsed Rows 2");
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Collapsed Rows 3");
lxw_worksheet *worksheet5 = workbook_add_worksheet(workbook, "Outline Columns");
lxw_worksheet *worksheet6 = workbook_add_worksheet(workbook, "Collapsed Columns");
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/*
* Example 1: Create a worksheet with outlined rows. It also includes
* SUBTOTAL() functions so that it looks like the type of automatic
* outlines that are generated when you use the 'Sub Totals' option.
*
* For outlines the important parameters are 'hidden' and 'level'. Rows
* with the same 'level' are grouped together. The group will be collapsed
* if 'hidden' is non-zero.
*/
/* The option structs with the outline level set. */
lxw_row_col_options options1 = {.hidden = 0, .level = 2, .collapsed = 0};
lxw_row_col_options options2 = {.hidden = 0, .level = 1, .collapsed = 0};
/* Set the row outline properties set. */
worksheet_set_row_opt(worksheet1, 1, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 2, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 3, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 4, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 5, LXW_DEF_ROW_HEIGHT, NULL, &options2);
worksheet_set_row_opt(worksheet1, 6, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 7, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 8, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 9, LXW_DEF_ROW_HEIGHT, NULL, &options1);
worksheet_set_row_opt(worksheet1, 10, LXW_DEF_ROW_HEIGHT, NULL, &options2);
/* Write the sub-total data that is common to the row examples. */
create_row_example_data(worksheet1, bold);
/*
* Example 2: Create a worksheet with collapsed outlined rows.
* This is the same as the example 1 except that the all rows are collapsed.
*/
/* The option structs with the outline properties set. */
lxw_row_col_options options3 = {.hidden = 1, .level = 2, .collapsed = 0};
lxw_row_col_options options4 = {.hidden = 1, .level = 1, .collapsed = 0};
lxw_row_col_options options5 = {.hidden = 0, .level = 0, .collapsed = 1};
/* Set the row options with the outline level. */
worksheet_set_row_opt(worksheet2, 1, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 2, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 3, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 4, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 5, LXW_DEF_ROW_HEIGHT, NULL, &options4);
worksheet_set_row_opt(worksheet2, 6, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 7, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 8, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 9, LXW_DEF_ROW_HEIGHT, NULL, &options3);
worksheet_set_row_opt(worksheet2, 10, LXW_DEF_ROW_HEIGHT, NULL, &options4);
worksheet_set_row_opt(worksheet2, 11, LXW_DEF_ROW_HEIGHT, NULL, &options5);
/* Write the sub-total data that is common to the row examples. */
create_row_example_data(worksheet2, bold);
/*
* Example 3: Create a worksheet with collapsed outlined rows. Same as the
* example 1 except that the two sub-totals are collapsed.
*/
lxw_row_col_options options6 = {.hidden = 1, .level = 2, .collapsed = 0};
lxw_row_col_options options7 = {.hidden = 0, .level = 1, .collapsed = 1};
/* Set the row options with the outline level. */
worksheet_set_row_opt(worksheet3, 1, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 2, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 3, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 4, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 5, LXW_DEF_ROW_HEIGHT, NULL, &options7);
worksheet_set_row_opt(worksheet3, 6, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 7, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 8, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 9, LXW_DEF_ROW_HEIGHT, NULL, &options6);
worksheet_set_row_opt(worksheet3, 10, LXW_DEF_ROW_HEIGHT, NULL, &options7);
/* Write the sub-total data that is common to the row examples. */
create_row_example_data(worksheet3, bold);
/*
* Example 4: Create a worksheet with outlined rows. Same as the example 1
* except that the two sub-totals are collapsed.
*/
lxw_row_col_options options8 = {.hidden = 1, .level = 2, .collapsed = 0};
lxw_row_col_options options9 = {.hidden = 1, .level = 1, .collapsed = 1};
lxw_row_col_options options10 = {.hidden = 0, .level = 0, .collapsed = 1};
/* Set the row options with the outline level. */
worksheet_set_row_opt(worksheet4, 1, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 2, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 3, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 4, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 5, LXW_DEF_ROW_HEIGHT, NULL, &options9);
worksheet_set_row_opt(worksheet4, 6, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 7, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 8, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 9, LXW_DEF_ROW_HEIGHT, NULL, &options8);
worksheet_set_row_opt(worksheet4, 10, LXW_DEF_ROW_HEIGHT, NULL, &options9);
worksheet_set_row_opt(worksheet4, 11, LXW_DEF_ROW_HEIGHT, NULL, &options10);
/* Write the sub-total data that is common to the row examples. */
create_row_example_data(worksheet4, bold);
/*
* Example 5: Create a worksheet with outlined columns.
*/
lxw_row_col_options options11 = {.hidden = 0, .level = 1, .collapsed = 0};
/* Write the sub-total data that is common to the column examples. */
create_col_example_data(worksheet5, bold);
/* Add bold format to the first row. */
worksheet_set_row(worksheet5, 0, LXW_DEF_ROW_HEIGHT, bold);
/* Set column formatting and the outline level. */
worksheet_set_column( worksheet5, COLS("A:A"), 10, bold);
worksheet_set_column_opt(worksheet5, COLS("B:G"), 5, NULL, &options11);
worksheet_set_column( worksheet5, COLS("H:H"), 10, NULL);
/*
* Example 6: Create a worksheet with outlined columns.
*/
lxw_row_col_options options12 = {.hidden = 1, .level = 1, .collapsed = 0};
lxw_row_col_options options13 = {.hidden = 0, .level = 0, .collapsed = 1};
/* Write the sub-total data that is common to the column examples. */
create_col_example_data(worksheet6, bold);
/* Add bold format to the first row. */
worksheet_set_row(worksheet6, 0, LXW_DEF_ROW_HEIGHT, bold);
/* Set column formatting and the outline level. */
worksheet_set_column( worksheet6, COLS("A:A"), 10, bold);
worksheet_set_column_opt(worksheet6, COLS("B:G"), 5, NULL, &options12);
worksheet_set_column_opt(worksheet6, COLS("H:H"), 10, NULL, &options13);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,133 @@
/*
* A simple example using the libxlsxwriter library to create worksheets with
* panes.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
int row;
int col;
/* Create a new workbook and add some worksheets. */
lxw_workbook *workbook = workbook_new("panes.xlsx");
lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, "Panes 1");
lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, "Panes 2");
lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, "Panes 3");
lxw_worksheet *worksheet4 = workbook_add_worksheet(workbook, "Panes 4");
/* Set up some formatting and text to highlight the panes. */
lxw_format *header = workbook_add_format(workbook);
format_set_align(header, LXW_ALIGN_CENTER);
format_set_align(header, LXW_ALIGN_VERTICAL_CENTER);
format_set_fg_color(header, 0xD7E4BC);
format_set_bold(header);
format_set_border(header, LXW_BORDER_THIN);
lxw_format *center = workbook_add_format(workbook);
format_set_align(center, LXW_ALIGN_CENTER);
/*
* Example 1. Freeze pane on the top row.
*/
worksheet_freeze_panes(worksheet1, 1, 0);
/* Some sheet formatting. */
worksheet_set_column(worksheet1, 0, 8, 16, NULL);
worksheet_set_row(worksheet1, 0, 20, NULL);
worksheet_set_selection(worksheet1, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (col = 0; col < 9; col++) {
worksheet_write_string(worksheet1, 0, col, "Scroll down", header);
}
for (row = 1; row < 100; row++) {
for (col = 0; col < 9; col++) {
worksheet_write_number(worksheet1, row, col, row + 1, center);
}
}
/*
* Example 2. Freeze pane on the left column.
*/
worksheet_freeze_panes(worksheet2, 0, 1);
/* Some sheet formatting. */
worksheet_set_column(worksheet2, 0, 0, 16, NULL);
worksheet_set_selection(worksheet2, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (row = 0; row < 50; row++) {
worksheet_write_string(worksheet2, row, 0, "Scroll right", header);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet2, row, col, col, center);
}
}
/*
* Example 3. Freeze pane on the top row and left column.
*/
worksheet_freeze_panes(worksheet3, 1, 1);
/* Some sheet formatting. */
worksheet_set_column(worksheet3, 0, 25, 16, NULL);
worksheet_set_row(worksheet3, 0, 20, NULL);
worksheet_write_string(worksheet3, 0, 0, "", header);
worksheet_set_selection(worksheet3, 4, 3, 4, 3);
/* Some worksheet text to demonstrate scrolling. */
for (col = 1; col < 26; col++) {
worksheet_write_string(worksheet3, 0, col, "Scroll down", header);
}
for (row = 1; row < 50; row++) {
worksheet_write_string(worksheet3, row, 0, "Scroll right", header);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet3, row, col, col, center);
}
}
/*
* Example 4. Split pane on the top row and left column.
*
* The divisions must be specified in terms of row and column dimensions.
* The default row height is 15 and the default column width is 8.43
*/
worksheet_split_panes(worksheet4, 15, 8.43);
/* Some sheet formatting. */
/* Some worksheet text to demonstrate scrolling. */
for (col = 1; col < 26; col++) {
worksheet_write_string(worksheet4, 0, col, "Scroll", center);
}
for (row = 1; row < 50; row++) {
worksheet_write_string(worksheet4, row, 0, "Scroll", center);
for (col = 1; col < 26; col++) {
worksheet_write_number(worksheet4, row, col, col, center);
}
}
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,91 @@
/*
* An example of using the libxlsxwriter library to write some "rich strings",
* i.e., strings with multiple formats.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("rich_strings.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Set up some formats to use. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
lxw_format *italic = workbook_add_format(workbook);
format_set_italic(italic);
lxw_format *red = workbook_add_format(workbook);
format_set_font_color(red, LXW_COLOR_RED);
lxw_format *blue = workbook_add_format(workbook);
format_set_font_color(blue, LXW_COLOR_BLUE);
lxw_format *center = workbook_add_format(workbook);
format_set_align(center, LXW_ALIGN_CENTER);
lxw_format *superscript = workbook_add_format(workbook);
format_set_font_script(superscript, LXW_FONT_SUPERSCRIPT);
/* Make the first column wider for clarity. */
worksheet_set_column(worksheet, 0, 0, 30, NULL);
/*
* Create and write some rich strings with multiple formats.
*/
/* Example 1. Some bold and italic in the same string. */
lxw_rich_string_tuple fragment11 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment12 = {.format = bold, .string = "bold" };
lxw_rich_string_tuple fragment13 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment14 = {.format = italic, .string = "italic" };
lxw_rich_string_tuple *rich_string1[] = {&fragment11, &fragment12,
&fragment13, &fragment14, NULL};
worksheet_write_rich_string(worksheet, CELL("A1"), rich_string1, NULL);
/* Example 2. Some red and blue coloring in the same string. */
lxw_rich_string_tuple fragment21 = {.format = NULL, .string = "This is " };
lxw_rich_string_tuple fragment22 = {.format = red, .string = "red" };
lxw_rich_string_tuple fragment23 = {.format = NULL, .string = " and this is "};
lxw_rich_string_tuple fragment24 = {.format = blue, .string = "blue" };
lxw_rich_string_tuple *rich_string2[] = {&fragment21, &fragment22,
&fragment23, &fragment24, NULL};
worksheet_write_rich_string(worksheet, CELL("A3"), rich_string2, NULL);
/* Example 3. A rich string plus cell formatting. */
lxw_rich_string_tuple fragment31 = {.format = NULL, .string = "Some " };
lxw_rich_string_tuple fragment32 = {.format = bold, .string = "bold text"};
lxw_rich_string_tuple fragment33 = {.format = NULL, .string = " centered"};
lxw_rich_string_tuple *rich_string3[] = {&fragment31, &fragment32,
&fragment33, NULL};
/* Note that this example also has a "center" cell format. */
worksheet_write_rich_string(worksheet, CELL("A5"), rich_string3, center);
/* Example 4. A math example with a superscript. */
lxw_rich_string_tuple fragment41 = {.format = italic, .string = "j =k" };
lxw_rich_string_tuple fragment42 = {.format = superscript, .string = "(n-1)"};
lxw_rich_string_tuple *rich_string4[] = {&fragment41, &fragment42, NULL};
worksheet_write_rich_string(worksheet, CELL("A7"), rich_string4, center);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,32 @@
/*
* Example of how to set Excel worksheet tab colors using libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("tab_colors.xlsx");
/* Set up some worksheets. */
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);
/* Set the tab colors. */
worksheet_set_tab_color(worksheet1, LXW_COLOR_RED);
worksheet_set_tab_color(worksheet2, LXW_COLOR_GREEN);
worksheet_set_tab_color(worksheet3, 0xFF9900); /* Orange. */
/* worksheet4 will have the default color. */
worksheet_write_string(worksheet4, 0, 0, "Hello", NULL);
workbook_close(workbook);
return 0;
}

View File

@ -0,0 +1,50 @@
/*
* A simple program to write some data to an Excel file using the
* libxlsxwriter library.
*
* This program is shown, with explanations, in Tutorial 1 of the
* libxlsxwriter documentation.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Some data we want to write to the worksheet. */
struct expense {
char item[32];
int cost;
};
struct expense expenses[] = {
{"Rent", 1000},
{"Gas", 100},
{"Food", 300},
{"Gym", 50},
};
int main() {
/* Create a workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("tutorial01.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Start from the first cell. Rows and columns are zero indexed. */
int row = 0;
int col = 0;
/* Iterate over the data and write it out element by element. */
for (row = 0; row < 4; row++) {
worksheet_write_string(worksheet, row, col, expenses[row].item, NULL);
worksheet_write_number(worksheet, row, col + 1, expenses[row].cost, NULL);
}
/* Write a total using a formula. */
worksheet_write_string (worksheet, row, col, "Total", NULL);
worksheet_write_formula(worksheet, row, col + 1, "=SUM(B1:B4)", NULL);
/* Save the workbook and free any allocated memory. */
return workbook_close(workbook);
}

View File

@ -0,0 +1,63 @@
/*
* A simple program to write some data to an Excel file using the
* libxlsxwriter library.
*
* This program is shown, with explanations, in Tutorial 2 of the
* libxlsxwriter documentation.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Some data we want to write to the worksheet. */
struct expense {
char item[32];
int cost;
};
struct expense expenses[] = {
{"Rent", 1000},
{"Gas", 100},
{"Food", 300},
{"Gym", 50},
};
int main() {
/* Create a workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("tutorial02.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
int i;
/* Add a bold format to use to highlight cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Add a number format for cells with money. */
lxw_format *money = workbook_add_format(workbook);
format_set_num_format(money, "$#,##0");
/* Write some data header. */
worksheet_write_string(worksheet, row, col, "Item", bold);
worksheet_write_string(worksheet, row, col + 1, "Cost", bold);
/* Iterate over the data and write it out element by element. */
for (i = 0; i < 4; i++) {
/* Write from the first cell below the headers. */
row = i + 1;
worksheet_write_string(worksheet, row, col, expenses[i].item, NULL);
worksheet_write_number(worksheet, row, col + 1, expenses[i].cost, money);
}
/* Write a total using a formula. */
worksheet_write_string (worksheet, row + 1, col, "Total", bold);
worksheet_write_formula(worksheet, row + 1, col + 1, "=SUM(B2:B5)", money);
/* Save the workbook and free any allocated memory. */
return workbook_close(workbook);
}

View File

@ -0,0 +1,72 @@
/*
* A simple program to write some data to an Excel file using the
* libxlsxwriter library.
*
* This program is shown, with explanations, in Tutorial 3 of the
* libxlsxwriter documentation.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Some data we want to write to the worksheet. */
struct expense {
char item[32];
int cost;
lxw_datetime datetime;
};
struct expense expenses[] = {
{"Rent", 1000, { .year = 2013, .month = 1, .day = 13 } },
{"Gas", 100, { .year = 2013, .month = 1, .day = 14 } },
{"Food", 300, { .year = 2013, .month = 1, .day = 16 } },
{"Gym", 50, { .year = 2013, .month = 1, .day = 20 } },
};
int main() {
/* Create a workbook and add a worksheet. */
lxw_workbook *workbook = workbook_new("tutorial03.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
int i;
/* Add a bold format to use to highlight cells. */
lxw_format *bold = workbook_add_format(workbook);
format_set_bold(bold);
/* Add a number format for cells with money. */
lxw_format *money = workbook_add_format(workbook);
format_set_num_format(money, "$#,##0");
/* Add an Excel date format. */
lxw_format *date_format = workbook_add_format(workbook);
format_set_num_format(date_format, "mmmm d yyyy");
/* Adjust the column width. */
worksheet_set_column(worksheet, 0, 0, 15, NULL);
/* Write some data header. */
worksheet_write_string(worksheet, row, col, "Item", bold);
worksheet_write_string(worksheet, row, col + 1, "Cost", bold);
/* Iterate over the data and write it out element by element. */
for (i = 0; i < 4; i++) {
/* Write from the first cell below the headers. */
row = i + 1;
worksheet_write_string (worksheet, row, col, expenses[i].item, NULL);
worksheet_write_datetime(worksheet, row, col + 1, &expenses[i].datetime, date_format);
worksheet_write_number (worksheet, row, col + 2, expenses[i].cost, money);
}
/* Write a total using a formula. */
worksheet_write_string (worksheet, row + 1, col, "Total", bold);
worksheet_write_formula(worksheet, row + 1, col + 2, "=SUM(C2:C5)", money);
/* Save the workbook and free any allocated memory. */
return workbook_close(workbook);
}

View File

@ -0,0 +1,20 @@
/*
* A simple Unicode UTF-8 example using libxlsxwriter.
*
* Note: The source file must be UTF-8 encoded.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("utf8.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_write_string(worksheet, 2, 1, "Это фраза на русском!", NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,41 @@
/*
* Example of cell locking and formula hiding in an Excel worksheet using
* libxlsxwriter.
*
* Copyright 2014-2021, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("protection.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *unlocked = workbook_add_format(workbook);
format_set_unlocked(unlocked);
lxw_format *hidden = workbook_add_format(workbook);
format_set_hidden(hidden);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 40, NULL);
/* Turn worksheet protection on without a password. */
worksheet_protect(worksheet, NULL, NULL);
/* Write a locked, unlocked and hidden cell. */
worksheet_write_string(worksheet, 0, 0, "B1 is locked. It cannot be edited.", NULL);
worksheet_write_string(worksheet, 1, 0, "B2 is unlocked. It can be edited.", NULL);
worksheet_write_string(worksheet, 2, 0, "B3 is hidden. The formula isn't visible.", NULL);
worksheet_write_formula(worksheet, 0, 1, "=1+2", NULL); /* Locked by default. */
worksheet_write_formula(worksheet, 1, 1, "=1+2", unlocked);
worksheet_write_formula(worksheet, 2, 1, "=1+2", hidden);
workbook_close(workbook);
return 0;
}