libxlsxwriter
dates_and_times02.c
<< dates_and_times01.c dates_and_times03.c >>

Example of writing dates and times in Excel using an lxw_datetime struct and date formatting.

/*
* Example of writing dates and times in Excel using an lxw_datetime struct
* and date formatting.
*
* Copyright 2014-2018, 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);
}
workbook_close
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
workbook_new
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
format_set_num_format
void format_set_num_format(lxw_format *format, const char *num_format)
Set the number format for a cell.
lxw_worksheet
Struct to represent an Excel worksheet.
Definition: worksheet.h:1603
lxw_format
Struct to represent the formatting properties of an Excel format.
Definition: format.h:358
worksheet_write_datetime
lxw_error worksheet_write_datetime(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_datetime *datetime, lxw_format *format)
Write a date or time to a worksheet cell.
lxw_datetime
Struct to represent a date and time in Excel.
Definition: common.h:152
lxw_workbook
Struct to represent an Excel workbook.
Definition: workbook.h:273
worksheet_set_column
lxw_error worksheet_set_column(lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, double width, lxw_format *format)
Set the properties for one or more columns of cells.
workbook_add_worksheet
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
workbook_add_format
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.