-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
200 lines (156 loc) · 7.23 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
NAME
Spreadsheet::Write - Writer for spreadsheet files (CSV, XLS, XLSX, ...)
SYNOPSIS
Basic usage:
use Spreadsheet::Write;
my $sp=Spreadsheet::Write->new(file => 'test.xlsx');
$sp->addrow('hello','world');
$sp->close();
More possibilities:
use Spreadsheet::Write;
my $sp=Spreadsheet::Write->new(
file => $ARGV[0], # eg. test.xls, test.xlsx, or test.csv
sheet => 'Test Data',
styles => {
money => {
format => '$#,##0.00;-$#,##0.00',
},
bright => {
font_weight => 'bold',
font_color => 'blue',
font_style => 'italic',
},
},
);
$sp->addrow(
'col1',
{ content => [ 'col2', 'col3', 'col4' ], style => 'bright' },
{ content => 'col5', bg_color => 'gray' },
'col6',
);
$sp->freeze(1,0);
$sp->addrow(
{ content => [ 1, 1.23, 123.45, -234.56 ], style => 'money' },
);
my @data=(
[ qw(1 2 3 4) ],
[ qw(a s d f) ],
[ qw(z x c v b) ],
# ...
);
foreach my $row (@data) {
$sp->addrow({ style => 'ntext', content => $row });
}
$sp->close();
DESCRIPTION
"Spreadsheet::Write" writes files in CSV, XLS (Microsoft Excel 97), XLSX
(Microsoft Excel 2007), and other formats if their drivers exist. It is
especially suitable for building various dumps and reports where rows
are built in sequence, one after another.
The same calling format and options can be used with any output file
format. Unsupported options are ignored where possible allowing for easy
run-time selection of the output format by file name.
METHODS
new ()
$spreadsheet = Spreadsheet::Write->new(
file => 'table.xls',
styles => {
mynumber => '#,##0.00',
}
);
Creates a new spreadsheet object. It takes a list of options. The
following are valid:
file filename of the new spreadsheet or an IO handle (mandatory)
encoding encoding of output file (optional, csv format only)
format format of spreadsheet - 'csv', 'xls', 'xlsx', or 'auto' (default)
sheet Sheet name (optional, not supported by some formats)
styles Defines cell formatting shortcuts (optional)
If file format is 'auto' (or omitted), the format is guessed from the
filename extention. If impossible to guess the format defaults to 'csv'.
An IO-like handle can be given as 'file' argument (IO::File, IO::Scalar,
etc). In this case the format argument is mandatory.
Default styles are: header => { font_weight => 'bold', type => 'string',
}, ntext => { format => '@', type => 'string', }, money => { format =>
'$#,##0.00;-$#,##0.00', },
addrow(arg1,arg2,...)
Adds a row into the spreadsheet. Takes arbitrary number of arguments.
Arguments represent cell values and may be strings or hash references.
If an argument is a hash reference, it takes the following structure:
content value to put into the cell
style formatting style, as defined in new(), scalar or array-ref
type type of the content (defaults to 'auto')
format number format (see Spreadsheet::WriteExcel for details)
font_weight weight of font. Only valid value is 'bold'
font_style style of font. Only valid value is 'italic'
font_decoration 'underline' or 'strikeout' (or both, space separated)
font_face font of column; default is 'Arial'
font_color color of font (see Spreadsheet::WriteExcel for color values)
font_size size of font
bg_color color of background (see Spreadsheet::WriteExcel for color values)
align alignment
valign vertical alignment
width column width, excel units (only makes sense once per column)
height row height, excel units (only makes sense once per row)
comment hidden comment for the cell, where supported
Styles can be used to assign default values for any of these formatting
parameters thus allowing easy global changes. Other parameters specified
override style definitions.
Example:
my $sp=Spreadsheet::Write->new(
file => 'employees.xlsx',
styles => {
header => { font_weight => 'bold' },
},
);
$sp->addrow(
{ content => 'First Name', font_weight => 'bold' },
{ content => 'Last Name', font_weight => 'bold' },
{ content => 'Age', style => 'header' },
);
$sp->addrow("John","Doe",34);
$sp->addrow("Susan","Smith",28);
Note that in this example all header cells will have identical
formatting even though some use direct formats and one uses style.
If you want to store text that looks like a number you might want to use
{ type => 'string', format => '@' } arguments. By default the type
detection is automatic, as done by for instance Spreadsheet::WriteExcel
write() method.
It is also possible to supply an array reference in the 'content'
parameter of the extended format. It means to use the same formatting
for as many cells as there are elements in this array. Useful for
creating header rows. For instance, the above example can be rewritten
as:
$sp->addrow(
{ style => 'header',
content => [ 'First Name','Last Name','Age' ],
}
);
Not all styling options are supported in all formats. Where they are not
supported they are safely ignored.
addrows([$cell1A,$cell1B,...],[$cell2A,$cell2B,...],...)
Shortcut for adding multiple rows.
Each argument is an arrayref representing a row.
Any argument that is not a reference (i.e. a scalar) is taken to be the
title of a new worksheet.
addsheet(name)
Adds a new sheet into the document and makes it active. Subsequent
addrow() calls will add rows to that new sheet.
For CSV format this call is NOT ignored, but produces a fatal error
currently.
freeze($row, $col, $top_row, $left_col))
Sets a freeze-pane at the given position, equivalent to
Spreadsheet::WriteExcel->freeze_panes(). Ignored for CSV files.
close ()
Finalizes the spreadsheet and closes the file. It is a good idea to call
this method explicitly instead of relying on perl's garbage collector
because for many formats the file may be in an unusable state until this
method is called.
Once a spreadsheet is closed, calls to addrow() will fail.
LICENSE
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
AUTHORS
Written by Nick Eremeev <nick.eremeev@gmail.com>; Andrew Maltsev
<am@ejelta.com>; <https://ejelta.com/>
Multiple backends implementation and other patches by Toby Inkster
<tobyink@cpan.org> (see also a full fork at Spreadsheet::Wright).