3
###############################################################################
5
# Spreadsheet::WriteExcel example of writing dates and times using the
6
# write_date_time() Worksheet method.
8
# reverse('�'), August 2004, John McNamara, jmcnamara@cpan.org
12
use Spreadsheet::WriteExcel;
15
# Create a new workbook and add a worksheet
16
my $workbook = Spreadsheet::WriteExcel->new("date_time.xls");
17
my $worksheet = $workbook->add_worksheet();
18
my $bold = $workbook->add_format(bold => 1);
22
# Expand the first column so that the date is visible.
23
$worksheet->set_column("A:B", 30);
26
# Write the column headers
27
$worksheet->write('A1', 'Formatted date', $bold);
28
$worksheet->write('B1', 'Format', $bold);
31
# Examples date and time formats. In the output file compare how changing
32
# the format codes change the appearance of the date.
55
'dd/mm/yy hh:mm:ss.000',
63
# Write the same date and time using each of the above formats. The empty
64
# string formats create a blank line to make the example clearer.
66
for my $date_format (@date_formats) {
68
next if $date_format eq '';
70
# Create a format for the date or time.
71
my $format = $workbook->add_format(
72
num_format => $date_format,
76
# Write the same date using different formats.
77
$worksheet->write_date_time($row, 0, '2004-08-01T12:30:45.123', $format);
78
$worksheet->write ($row, 1, $date_format);
82
# The following is an example of an invalid date. It is written as a string instead
83
# of a number. This is also Excel's default behaviour.
86
$worksheet->write_date_time($row, 0, '2004-13-01T12:30:45.123');
87
$worksheet->write ($row, 1, 'Invalid date. Written as string.', $bold);