| 1 |
#!/pro/bin/perl |
| 2 |
|
| 3 |
# csv2xlsx: Convert csv to xlsx |
| 4 |
# (m)'26 Copyright H.M.Brand 2007-2026 |
| 5 |
|
| 6 |
use 5.014000; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
our $VERSION = "1.24 - 2026-05-19"; |
| 10 |
|
| 11 |
sub usage { |
| 12 |
my $err = shift and select STDERR; |
| 13 |
print <<"EOU"; |
| 14 |
usage: csv2xlsx [options] [-o <xlsx>] [file.csv] |
| 15 |
csv2xlsx --help | --man | --info |
| 16 |
-s <sep> use <sep> as separator char, auto-detect, default = ',' |
| 17 |
The string "tab" is allowed. |
| 18 |
-e <esc> use <esc> as escape char, auto-detect, default = '"' |
| 19 |
The string "undef" is allowed. |
| 20 |
-q <quot> use <quot> as quotation char, default = '"' |
| 21 |
The string "undef" will disable quotation. |
| 22 |
-w <width> use <width> as default minimum column width default = 4 |
| 23 |
-o <xlsx> write output to file named <xlsx>, defaults |
| 24 |
to input file name with .csv replaced with .xlsx |
| 25 |
if from standard input, defaults to csv2xlsx.xlsx |
| 26 |
-F allow formula's. Otherwise fields starting with |
| 27 |
an equal sign are forced to string |
| 28 |
--Fa=aaa Define formula action: none/die/croak/diag/empty/undef |
| 29 |
--Ft Formula's will be stored as text (formula actions: none) |
| 30 |
--Fd Formula's will cause a die |
| 31 |
--Fc Formula's will cause a croak |
| 32 |
--FD Formula's will cause a warning (this is the default) |
| 33 |
--Fe Formula's will be replaced by the empty string |
| 34 |
--Fu Formula's will be replaced with an undefined cell |
| 35 |
-f force usage of <xlsx> if already exists (unlink before use) |
| 36 |
-d <dtfmt> use <dtfmt> as date formats. Default = 'dd-mm-yyyy' |
| 37 |
-C <C:fmt> use <fmt> as currency formats for currency <C>, no default |
| 38 |
--font=F set default font (default Calibri) |
| 39 |
--fs=N set font size (default 10) |
| 40 |
'M' = 'mono', 'D' = 'DejaVu Sans', 'DM' = 'DejaVu Sans Mono' |
| 41 |
-D cols only convert dates in columns <cols>. |
| 42 |
Default is everywhere. -D0 is disable |
| 43 |
-L N limit export to N rows |
| 44 |
-u CSV is UTF8 |
| 45 |
--de Some CSV fields might be double-encoded. Try to fix that. |
| 46 |
-m merge multiple CSV's into a single xlsx (separate sheets) |
| 47 |
-o is required, all arguments should be existing files |
| 48 |
--skip-empty Skip empty files on merge |
| 49 |
-S <cp> Split CSV on COLUMNxPAT into separate sheets. See --man |
| 50 |
or --info for options/features and examples. May repeat. |
| 51 |
--sl=C Use column C as sheet label when splitting with -S |
| 52 |
-v [<lvl>] verbosity (default = 1) |
| 53 |
EOU |
| 54 |
exit $err; |
| 55 |
} # usage |
| 56 |
|
| 57 |
use Text::CSV_XS; |
| 58 |
use Excel::Writer::XLSX; |
| 59 |
use List::Util qw( first ); |
| 60 |
use Date::Calc qw( Delta_Days Days_in_Month ); |
| 61 |
use Encode qw( from_to ); |
| 62 |
use Getopt::Long qw(:config bundling passthrough noignorecase ); |
| 63 |
my $quo = '"'; |
| 64 |
my $esc = '"'; |
| 65 |
my $wdt = 4; # Default minimal column width |
| 66 |
my $fac = "diag"; # Formula action (default is warn only) |
| 67 |
my $dtf = "dd-mm-yyyy"; # Date format |
| 68 |
my $crf = ""; # Currency format, e.g.: $:### ### ##0.00 |
| 69 |
my $opt_v = 1; |
| 70 |
my $dtc; |
| 71 |
|
| 72 |
GetOptions ( |
| 73 |
"V|version" => sub { say $0 =~ s{.*/}{}r, " [$VERSION]"; exit 0; }, |
| 74 |
"help|?" => sub { usage (0); }, |
| 75 |
"man" => sub { pod_nroff (); }, |
| 76 |
"info" => sub { pod_text (); }, |
| 77 |
|
| 78 |
"c|s|sep=s" => \my $sep, # Set after reading first line in attempt to auto-detect |
| 79 |
"q|quo=s" => \$quo, |
| 80 |
"e|esc=s" => \$esc, |
| 81 |
"w|width=i" => \$wdt, |
| 82 |
"o|x|out=s" => \my $xls, |
| 83 |
"d|date-fmt=s" => \$dtf, |
| 84 |
"D|date-col=s" => \$dtc, |
| 85 |
"C|curr-fmt=s" => \$crf, |
| 86 |
"font=s" => \my $font, |
| 87 |
"fs|font-size=i" => \my $font_size, |
| 88 |
"f|force!" => \my $frc, |
| 89 |
"F|formulas!" => \my $frm, |
| 90 |
"Fa=s" => \$fac, |
| 91 |
"Ft" => sub { $fac = "none"; }, |
| 92 |
"Fd" => sub { $fac = "die"; }, |
| 93 |
"Fc" => sub { $fac = "croak"; }, |
| 94 |
"FD" => sub { $fac = "diag"; }, |
| 95 |
"Fe" => sub { $fac = "empty"; }, |
| 96 |
"Fu" => sub { $fac = "undef"; }, |
| 97 |
"u|utf-8|utf8!" => \my $utf, |
| 98 |
"de|fix-utf8!" => \my $dutf, # double encoded? \x{c3}\x{ab} => \x{100} |
| 99 |
"m|merge!" => \my $mrg, |
| 100 |
"skip-empty!" => \my $skip_empty, |
| 101 |
"S|split=s" => \my @split, |
| 102 |
"sl|split-label=s"=> \my $split_sl, |
| 103 |
"L|row-limit=i" => \my $row_limit, |
| 104 |
"v|verbose:2" => \$opt_v, |
| 105 |
) or usage (1); |
| 106 |
|
| 107 |
sub pod_text { |
| 108 |
require Pod::Text::Color; |
| 109 |
my $m = $ENV{NO_COLOR} ? "Pod::Text" : "Pod::Text::Color"; |
| 110 |
my $p = $m->new (); |
| 111 |
open my $fh, ">", \my $out or die "Cannot generate manual: $!\n"; |
| 112 |
$p->parse_from_file ($0, $fh); |
| 113 |
close $fh; |
| 114 |
print $out; |
| 115 |
exit 0; |
| 116 |
} # pod_text |
| 117 |
|
| 118 |
sub pod_nroff { |
| 119 |
first { -x "$_/nroff" } grep { -d } split m/:+/ => $ENV{PATH} or pod_text (); |
| 120 |
|
| 121 |
require Pod::Man; |
| 122 |
my $p = Pod::Man->new (); |
| 123 |
open my $fh, "|-", "nroff", "-man" or die "Cannot generate manual: $!\n"; |
| 124 |
$p->parse_from_file ($0, $fh); |
| 125 |
close $fh; |
| 126 |
exit 0; |
| 127 |
} # pod_nroff |
| 128 |
|
| 129 |
if ($mrg) { |
| 130 |
my @csv; |
| 131 |
for (@ARGV) { |
| 132 |
if (m/\.xlsx?$/i) { |
| 133 |
$xls and usage (1); |
| 134 |
$xls = $_; |
| 135 |
next; |
| 136 |
} |
| 137 |
if (m/\.(?:csv|png|jpe?g|bmp|gif|tiff|xpm)$/i && -s) { |
| 138 |
push @csv => $_; |
| 139 |
next; |
| 140 |
} |
| 141 |
unless ($skip_empty) { |
| 142 |
warn "Argument $_ is not an existing (CSV) file\n"; |
| 143 |
usage (1); |
| 144 |
} |
| 145 |
} |
| 146 |
$xls && @csv or usage (1); |
| 147 |
@ARGV = @csv; |
| 148 |
} |
| 149 |
|
| 150 |
sub col2col { |
| 151 |
my $l = shift; |
| 152 |
$l =~ m/^[0-9]/ and return $l; |
| 153 |
my $c = 0; |
| 154 |
while ($l =~ s/^([A-Za-z])//) { |
| 155 |
$c = 26 * $c + 1 + ord (uc $1) - ord ("A"); |
| 156 |
} |
| 157 |
$c; |
| 158 |
} # col2col |
| 159 |
|
| 160 |
foreach my $split (@split) { |
| 161 |
my ($col, $operator, $pat) = ($split =~ m{^ |
| 162 |
([0-9]+|[A-Z]+|[a-z]+) # Column: A, AB, 1, 14 |
| 163 |
([=/uU<>]) # Operator |
| 164 |
(.*) # Pattern/string |
| 165 |
\z}x) or usage (1); |
| 166 |
my $case = $col =~ m/^[a-z]/ ? 1 : 0; |
| 167 |
$split = { |
| 168 |
col => col2col ($col), |
| 169 |
op => $operator, |
| 170 |
str => $pat, |
| 171 |
ic => $case, |
| 172 |
lbl => undef, |
| 173 |
}; |
| 174 |
if ($split_sl) { |
| 175 |
($col) = ($split_sl =~ m/^([0-9]+|[A-Z]+)$/) or usage (1); |
| 176 |
$split->{lbl} = col2col ($col); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
my $base = @ARGV && -f $ARGV[0] ? $ARGV[0] : "csv2xlsx"; |
| 181 |
$xls ||= $base =~ s/(?:\.csv)?$/.xlsx/ir; |
| 182 |
|
| 183 |
-s $xls && $frc and unlink $xls; |
| 184 |
if (-s $xls) { |
| 185 |
print STDERR "File '$xls' already exists. Overwrite? [y/N] > N\b"; |
| 186 |
scalar <STDIN> =~ m/^[yj](?:es|a)?$/i or exit; |
| 187 |
} |
| 188 |
# Test if the file can be created |
| 189 |
# The error from Excel::Writer::XLSX is not very informative if it fails |
| 190 |
# e.g. when the folder cannot be used for whatever reason |
| 191 |
{ open my $fh, ">", $xls or die "$xls: $!\n"; |
| 192 |
close $fh; |
| 193 |
unlink $xls; |
| 194 |
} |
| 195 |
|
| 196 |
if ($dutf) { |
| 197 |
eval { require Encode::DoubleEncodedUTF8; }; |
| 198 |
if ($@) { |
| 199 |
$dutf = 0; |
| 200 |
warn "Cannot load Encode::DoubleEncodedUTF8; --de ignored\n"; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
my $wbk = Excel::Writer::XLSX->new ($xls); |
| 205 |
$dtf =~ s/j/y/g; |
| 206 |
my %fmt = ( |
| 207 |
dflt => $wbk->add_format (), |
| 208 |
date => $wbk->add_format (align => "center", num_format => $dtf), |
| 209 |
rest => $wbk->add_format (align => "left"), |
| 210 |
wrap => $wbk->add_format (text_wrap => 1), |
| 211 |
); |
| 212 |
$crf =~ s/^([^:]+):(.*)/$1/ and $fmt{currency} = $wbk->add_format ( |
| 213 |
num_format => "$1 $2", |
| 214 |
align => "right", |
| 215 |
); |
| 216 |
if ($font) { |
| 217 |
$font eq "M" and $font = "mono"; |
| 218 |
$font eq "D" and $font = "DejaVu Sans"; |
| 219 |
$font eq "DM" and $font = "DejaVu Sans Mono"; |
| 220 |
$_->set_font ($font) for values %fmt; |
| 221 |
} |
| 222 |
if ($font_size) { |
| 223 |
$_->set_size ($font_size) for values %fmt; |
| 224 |
} |
| 225 |
|
| 226 |
my @args = @ARGV ? @ARGV : (""); |
| 227 |
foreach my $csvf (@args) { |
| 228 |
my $sheetname = $csvf =~ s{\.\w+$}{}ir =~ s{.*/}{}r || "Sheet 1"; |
| 229 |
($_ = length $sheetname) > 31 and substr $sheetname, 31, $_ - 31, ""; |
| 230 |
my ($wks, $w, $h); |
| 231 |
|
| 232 |
$opt_v > 7 and warn "Parsing $csvf into $xls.$sheetname ...\n"; |
| 233 |
|
| 234 |
if ($csvf =~ m/\.(png|jpe?g|bmp|gif|tiff|xpm)$/i) { |
| 235 |
$wks = $wbk->add_worksheet ($sheetname); |
| 236 |
$wks->insert_image (1, 1, $csvf); |
| 237 |
next; |
| 238 |
} |
| 239 |
|
| 240 |
my $row; |
| 241 |
my $firstline; |
| 242 |
my $fh; |
| 243 |
if (-f $csvf) { |
| 244 |
$opt_v and say "Reading $csvf"; |
| 245 |
open $fh, "<", $csvf or die "$csvf: $!\n"; |
| 246 |
} |
| 247 |
else { |
| 248 |
$opt_v and say "Reading STDIN"; |
| 249 |
$fh = *ARGV; |
| 250 |
} |
| 251 |
my $Sep = $sep; |
| 252 |
unless ($Sep) { # No sep char passed, try to auto-detect; |
| 253 |
while (<$fh>) { |
| 254 |
m/\S/ or next; # Skip empty leading blank lines |
| 255 |
$Sep = # start auto-detect with quoted strings |
| 256 |
m/["\d];["\d;]/ ? ";" : |
| 257 |
m/["\d],["\d,]/ ? "," : |
| 258 |
m/["\d]\t["\d,]/ ? "\t" : |
| 259 |
# If neither, then for unquoted strings |
| 260 |
m/\w;[\w;]/ ? ";" : |
| 261 |
m/\w,[\w,]/ ? "," : |
| 262 |
m/\w\t[\w,]/ ? "\t" : |
| 263 |
# And pipes (lowest prio) |
| 264 |
m/["\d]\|["\d,]/ ? "|" : |
| 265 |
m/\w\|[\w,]/ ? "|" : |
| 266 |
m/,/ && !m/[;\t]/ ? "," : |
| 267 |
m/;/ && !m/[,\t]/ ? ";" : |
| 268 |
m/\t/ && !m/[,;]/ ? "\t" : |
| 269 |
"," ; |
| 270 |
$firstline = $_; |
| 271 |
last; |
| 272 |
} |
| 273 |
$firstline or die "The sourcefile does not contain any usable data\n"; |
| 274 |
} |
| 275 |
my $csv = Text::CSV_XS-> new ({ |
| 276 |
sep_char => $Sep eq "tab" ? "\t" : $Sep, |
| 277 |
quote_char => $quo eq "undef" ? undef : $quo, |
| 278 |
escape_char => $esc eq "undef" ? undef : $esc, |
| 279 |
binary => 1, |
| 280 |
keep_meta_info => 1, |
| 281 |
auto_diag => 1, |
| 282 |
formula => $fac, |
| 283 |
}); |
| 284 |
if ($firstline) { |
| 285 |
$csv->parse ($firstline) or die $csv->error_diag (); |
| 286 |
$row = [ $csv->fields ]; |
| 287 |
} |
| 288 |
if ($opt_v > 3) { |
| 289 |
foreach my $k (qw( sep_char quote_char escape_char )) { |
| 290 |
my $c = $csv->$k () || "undef"; |
| 291 |
$c =~ s/\t/\\t/g; |
| 292 |
$c =~ s/\r/\\r/g; |
| 293 |
$c =~ s/\n/\\n/g; |
| 294 |
$c =~ s/\0/\\0/g; |
| 295 |
$c =~ s/([\x00-\x1f\x7f-\xff])/sprintf"\\x{%02x}",ord$1/ge; |
| 296 |
printf STDERR "%-11s = %s\n", $k, $c; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (length $dtc) { |
| 301 |
if ($dtc eq "0") { |
| 302 |
$dtc = { -2 => 0 }; |
| 303 |
} |
| 304 |
else { |
| 305 |
my $rows = $dtc; |
| 306 |
$rows =~ s/-$/-999/; # 3,6- |
| 307 |
$rows =~ s/-/../g; |
| 308 |
eval "\$dtc = { map { \$_ => 1 } $rows }"; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
my @w; # data height, -width, and default column widths |
| 313 |
while ($row && @$row or $row = $csv->getline ($fh)) { |
| 314 |
$row_limit and $csv->record_number > $row_limit and last; |
| 315 |
my @row = @$row; |
| 316 |
|
| 317 |
$opt_v > 8 and warn "@row\n"; |
| 318 |
if (@split) { |
| 319 |
my $ns = 0; |
| 320 |
foreach my $split (@split) { |
| 321 |
my ($i, $op, $str, $case) = @{$split}{qw( col op str ic )}; |
| 322 |
my $v = $row[$i - 1]; # Column-index is one-based |
| 323 |
if (defined $v) { |
| 324 |
$op eq "U" and $ns += 1; |
| 325 |
if ($case) { |
| 326 |
$op eq "=" and $ns += ( lc $v eq lc $v ); |
| 327 |
$op eq "/" and $ns += ( $v =~ m/$str/i ); |
| 328 |
} |
| 329 |
else { |
| 330 |
$op eq "=" and $ns += ( $v eq $str ); |
| 331 |
$op eq "/" and $ns += ( $v =~ m/$str/ ); |
| 332 |
if ($v =~ m/^[0-9]+$/) { |
| 333 |
$op eq "<" and $ns += ( $v < $str ); |
| 334 |
$op eq ">" and $ns += ( $v > $str ); |
| 335 |
} |
| 336 |
else { |
| 337 |
$op eq "<" and $ns += ( $v lt $str ); |
| 338 |
$op eq ">" and $ns += ( $v gt $str ); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
else { |
| 343 |
$op eq "u" and $ns += 1; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
$opt_v > 8 and warn join " " => "Record", $csv->record_number, |
| 348 |
"matched", $ns, "out of", scalar @split, "criteria\n"; |
| 349 |
if ($ns == @split) { # All criteria PASS |
| 350 |
if (@w) { |
| 351 |
$wks->set_column ($_, $_, $w[$_]) for 0 .. $#w; |
| 352 |
$wks = undef; |
| 353 |
} |
| 354 |
$split[0]{lbl} and $sheetname = $row[$split[0]{lbl} - 1]; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
unless ($wks) { |
| 359 |
# characters []:*/\ are not allowed in sheet names |
| 360 |
my $sn = $sheetname =~ tr{[]:*?/\\}{_}r =~ s{__+}{_}gr; |
| 361 |
$wks = $wbk->add_worksheet ($sn); |
| 362 |
$utf && !$wks->can ("write_unicode") and $utf = 0; |
| 363 |
($h, $w, @w) = (0, 1); |
| 364 |
} |
| 365 |
|
| 366 |
@row > $w and push @w => ($wdt) x (($w = @row) - @w); |
| 367 |
foreach my $c (0 .. $#row) { |
| 368 |
my $val = $row[$c] // ""; |
| 369 |
my $l = length $val; |
| 370 |
$l > ($w[$c] // -1) and $w[$c] = $l; |
| 371 |
|
| 372 |
$dutf and $csv->is_binary ($c) and utf8::valid ($val) and |
| 373 |
$val = Encode::decode ("utf-8-de", $val); |
| 374 |
|
| 375 |
if ($utf and $csv->is_binary ($c)) { |
| 376 |
from_to ($val, "utf-8", "ucs2"); |
| 377 |
$wks->write_unicode ($h, $c, $val); |
| 378 |
next; |
| 379 |
} |
| 380 |
|
| 381 |
if ($csv->is_quoted ($c)) { |
| 382 |
$val =~ s/\r\n/\n/g; |
| 383 |
if ($utf) { |
| 384 |
from_to ($val, "utf-8", "ucs2"); |
| 385 |
$val =~ m/\n/ |
| 386 |
? $wks->write_unicode ($h, $c, $val, $fmt{wrap}) |
| 387 |
: $wks->write_unicode ($h, $c, $val, $fmt{dflt}); |
| 388 |
} |
| 389 |
else { |
| 390 |
$val =~ m/\n/ |
| 391 |
? $wks->write_string ($h, $c, $val, $fmt{wrap}) |
| 392 |
: $wks->write_string ($h, $c, $val, $fmt{dflt}); |
| 393 |
} |
| 394 |
next; |
| 395 |
} |
| 396 |
|
| 397 |
if (!$dtc or $dtc->{$c + 1}) { |
| 398 |
my @d = (0, 0, 0); # Y, M, D |
| 399 |
$val =~ m/^(\d{4})(\d{2})(\d{2})$/ and @d = ($1, $2, $3); |
| 400 |
$val =~ m/^(\d{2})-(\d{2})-(\d{4})$/ and @d = ($3, $2, $1); |
| 401 |
if ( $d[2] >= 1 && $d[2] <= 31 && |
| 402 |
$d[1] >= 1 && $d[1] <= 12 && |
| 403 |
$d[0] >= 1900 && $d[0] <= 2199) { |
| 404 |
my $dm = Days_in_Month (@d[0,1]); |
| 405 |
$d[2] < 1 and $d[2] = 1; |
| 406 |
$d[2] > $dm and $d[2] = $dm; |
| 407 |
my $dt = 2 + Delta_Days (1900, 1, 1, @d); |
| 408 |
$wks->write ($h, $c, $dt, $fmt{date}); |
| 409 |
next; |
| 410 |
} |
| 411 |
} |
| 412 |
if ($crf and $val =~ m/^\s*\Q$crf\E\s*([0-9.]+)$/) { |
| 413 |
$wks->write ($h, $c, $1 + 0, $fmt{currency}); |
| 414 |
next; |
| 415 |
} |
| 416 |
|
| 417 |
if (!$frm && $val =~ m/^=/) { |
| 418 |
$wks->write_string ($h, $c, $val); |
| 419 |
} |
| 420 |
else { |
| 421 |
$wks->write ($h, $c, $val, $fmt{dflt}); |
| 422 |
} |
| 423 |
} |
| 424 |
++$h % 100 or $opt_v && printf STDERR "%6d x %6d\r", $w, $h; |
| 425 |
} continue { $row = undef } |
| 426 |
close $fh; |
| 427 |
$opt_v && printf STDERR "%6d x %6d\n", $w, $h; |
| 428 |
|
| 429 |
$wks->set_column ($_, $_, $w[$_]) for 0 .. $#w; |
| 430 |
} |
| 431 |
$opt_v and say "Writing $xls"; |
| 432 |
$wbk->close (); |
| 433 |
|
| 434 |
__END__ |
| 435 |
|
| 436 |
=head1 NAME |
| 437 |
|
| 438 |
csv2xlsx - Convert CSV to Excel 2007+ |
| 439 |
|
| 440 |
=head1 SYNOPSIS |
| 441 |
|
| 442 |
csv2xlsx [options] [file.csv] |
| 443 |
|
| 444 |
csv2xlsx test.csv |
| 445 |
|
| 446 |
csv2xlsx -f -o merged.xlsx -m foobar*.csv |
| 447 |
|
| 448 |
=head1 DESCRIPTION |
| 449 |
|
| 450 |
This tool converts CSV data to Excel-2007+. It can convert the CSV into a |
| 451 |
single sheet, merge CSV files into multiple sheets in a workbook or split |
| 452 |
a single CSV into multiple sheets. |
| 453 |
|
| 454 |
The tool supports encoding, formula handle, date conversion and some more. |
| 455 |
|
| 456 |
=head1 OPTIONS |
| 457 |
|
| 458 |
=over 2 |
| 459 |
|
| 460 |
=item -s S |
| 461 |
|
| 462 |
=item --sep=S |
| 463 |
|
| 464 |
use C<S> as separator character, auto-detect, default = C<,>. |
| 465 |
|
| 466 |
The literal string C<tab> is allowed. |
| 467 |
|
| 468 |
=item -q Q |
| 469 |
|
| 470 |
=item --quo=Q |
| 471 |
|
| 472 |
use C<Q> as quotation character, auto-detect, default = C<">. |
| 473 |
|
| 474 |
The literal string C<undef> is allowed to disable quotation. |
| 475 |
|
| 476 |
=item -e E |
| 477 |
|
| 478 |
=item --esc=E |
| 479 |
|
| 480 |
use C<E> as escape character, auto-detect, default = C<">. |
| 481 |
|
| 482 |
The literal string C<undef> is allowed to disable escapes. |
| 483 |
|
| 484 |
=item -w W |
| 485 |
|
| 486 |
=item --width=W |
| 487 |
|
| 488 |
Set column width. Default is to auto-size per column per sheet with a |
| 489 |
minimum width of C<4>. |
| 490 |
|
| 491 |
=item -o FILE.xlsx |
| 492 |
|
| 493 |
=item -x FILE.xlsx |
| 494 |
|
| 495 |
=item --out=FILE.xlsx |
| 496 |
|
| 497 |
Specify the output filename. Default it the same name as the input-file |
| 498 |
where the trailing C<.csv> will be replaced with C<.xlsx>. |
| 499 |
|
| 500 |
If a filename can not be automatically generated, it will default to |
| 501 |
C<csv2xlsx.xlsx>. |
| 502 |
|
| 503 |
=item -d format |
| 504 |
|
| 505 |
=item --date-fmt=format |
| 506 |
|
| 507 |
Use C<format> as date formats. Default = C<dd-mm-yyyy>. |
| 508 |
|
| 509 |
=item -D range |
| 510 |
|
| 511 |
=item --date-col=range |
| 512 |
|
| 513 |
Only convert dates in columns C<range>. Default is everywhere. |
| 514 |
|
| 515 |
Ranges are numeric, where the first column has index C<1>. A range is a |
| 516 |
definition of sections joined by C<,>s. A section is either a single column |
| 517 |
or a start/finish-pair joined with a dash. A missing finish index on the |
| 518 |
last segment is an open range (till the end of the line). |
| 519 |
|
| 520 |
If the range is C<0>, date conversion is disabled. |
| 521 |
|
| 522 |
-D 0 |
| 523 |
-D 1,2,6 |
| 524 |
-D 2,4-8,12- |
| 525 |
|
| 526 |
=item -C format |
| 527 |
|
| 528 |
=item --curr-fmt=format |
| 529 |
|
| 530 |
Use C<format> as currency formats for currency <C>, no default. |
| 531 |
|
| 532 |
-C '$:### ### ##0.00' |
| 533 |
|
| 534 |
=item --font=Calibri |
| 535 |
|
| 536 |
You can set the default font. The XLSX writer used sets the default font |
| 537 |
face to C<Calibri>. |
| 538 |
|
| 539 |
--font=Arial |
| 540 |
|
| 541 |
There are 3 default shortcuts |
| 542 |
|
| 543 |
--font=M |
| 544 |
--font=D |
| 545 |
--font=DM |
| 546 |
|
| 547 |
Setting the font to C<mono>, C<DejaVu Sans>, or C<DejaVu Sans Mono>. |
| 548 |
|
| 549 |
=item --fs=10 |
| 550 |
|
| 551 |
=item --font-size=10 |
| 552 |
|
| 553 |
Optional set the default font-size to anything other than the default 10. |
| 554 |
Must be a positive integer. No checks. |
| 555 |
|
| 556 |
=item -f |
| 557 |
|
| 558 |
=item --force |
| 559 |
|
| 560 |
Force usage of the output file if it already exists (unlink before use). |
| 561 |
|
| 562 |
=item -F |
| 563 |
|
| 564 |
=item --formulas |
| 565 |
|
| 566 |
Allow formula's. Otherwise fields starting with an equal sign C<=> are |
| 567 |
forced to string type. |
| 568 |
|
| 569 |
There are several shortcuts here to specify different behavior: |
| 570 |
|
| 571 |
=over 2 |
| 572 |
|
| 573 |
=item --Fa=a |
| 574 |
|
| 575 |
Define formula action: C<none>/C<die>/C<croak>/C<diag>/C<empty>/C<undef> |
| 576 |
|
| 577 |
=item --Ft |
| 578 |
|
| 579 |
Formula's will be stored as text (formula actions: none) |
| 580 |
|
| 581 |
=item --Fd |
| 582 |
|
| 583 |
Formula's will cause a die |
| 584 |
|
| 585 |
=item --Fc |
| 586 |
|
| 587 |
Formula's will cause a croak |
| 588 |
|
| 589 |
=item --FD |
| 590 |
|
| 591 |
Formula's will cause a warning (this is the default) |
| 592 |
|
| 593 |
=item --Fe |
| 594 |
|
| 595 |
Formula's will be replaced by the empty string |
| 596 |
|
| 597 |
=item --Fu |
| 598 |
|
| 599 |
Formula's will be replaced with an undefined cell |
| 600 |
|
| 601 |
=back |
| 602 |
|
| 603 |
=item -u |
| 604 |
|
| 605 |
=item --utf-8 |
| 606 |
|
| 607 |
=item --utf8 |
| 608 |
|
| 609 |
CSV is UTF-8 encoded. Likely not needed, as most is auto-detected. |
| 610 |
|
| 611 |
=item --de |
| 612 |
|
| 613 |
=item --fix-utf8 |
| 614 |
|
| 615 |
Attempt to fix double-encoded UTF-8. e.g. C<\x{00c3}\x{00ab}> should have |
| 616 |
been C<\x0100}>. YMMV. |
| 617 |
|
| 618 |
=item -m |
| 619 |
|
| 620 |
=item --merge |
| 621 |
|
| 622 |
Merge multiple CSV's into a single Excel (separate sheets). |
| 623 |
|
| 624 |
With this option, the option C<-o> is required. All arguments should be |
| 625 |
existing files. Piping is not supported. |
| 626 |
|
| 627 |
=item --skip-empty |
| 628 |
|
| 629 |
When merging CSV files with C<-m>, skip empty files. The default is to exit |
| 630 |
with an error message. |
| 631 |
|
| 632 |
=item -L N |
| 633 |
|
| 634 |
=item --row-limit=N |
| 635 |
|
| 636 |
Limit export to C<N> rows. |
| 637 |
|
| 638 |
=item -S CxP |
| 639 |
|
| 640 |
=item --split=CxP |
| 641 |
|
| 642 |
When dealing with big CSV datasets, this option enables you to split the |
| 643 |
data over several sheets. When all the C<-S> options match in a single row, |
| 644 |
that row will be the first row of a new sheet. (see also C<--sl=C>) |
| 645 |
|
| 646 |
=over 2 |
| 647 |
|
| 648 |
=item C |
| 649 |
|
| 650 |
The column that should be examined. C<A> = C<1>. If lower case, the value |
| 651 |
of that column is matched case insensitive when appropriate. |
| 652 |
|
| 653 |
-S 7=ab Column G is literal "ab" |
| 654 |
-S G=ab Column G is literal "ab" |
| 655 |
-S g=ab Column G is literal "ab", or "aB", or "Ab", or "AB" |
| 656 |
|
| 657 |
=item x |
| 658 |
|
| 659 |
Defines the operation on the column |
| 660 |
|
| 661 |
=over 2 |
| 662 |
|
| 663 |
=item = |
| 664 |
|
| 665 |
Literal match |
| 666 |
|
| 667 |
-S G=ab Column G is literal "ab" |
| 668 |
-S g=ab Column G is literal "ab", or "aB", or "Ab", or "AB" |
| 669 |
|
| 670 |
=item / |
| 671 |
|
| 672 |
Regex match |
| 673 |
|
| 674 |
-S G/b[a-z] Column G matches /b[a-z]/ |
| 675 |
-S g/b[a-z] Column G matches /b[a-z]/i |
| 676 |
|
| 677 |
=item u U |
| 678 |
|
| 679 |
Check for defined |
| 680 |
|
| 681 |
-S Gu Column G is undefined |
| 682 |
-S GU Column G is defined |
| 683 |
|
| 684 |
Similar for emptiness |
| 685 |
|
| 686 |
-S G= Column G is defined but empty |
| 687 |
-S G/. Column G is defined and not empty |
| 688 |
|
| 689 |
=item < > |
| 690 |
|
| 691 |
Compare. If the value in the column is defined compare. If the values only |
| 692 |
holds digits, do a numeric compare, otherwise do a string compare. |
| 693 |
|
| 694 |
-S G<42 Column G is defined and less than 42 (numeric) |
| 695 |
-S G<ab Column G is defined and less than 42 (string) |
| 696 |
|
| 697 |
-S G>42 Column G is defined and greater than 42 (numeric) |
| 698 |
-S G>ab Column G is defined and greater than 42 (string) |
| 699 |
|
| 700 |
=back |
| 701 |
|
| 702 |
=item P |
| 703 |
|
| 704 |
pattern or literal string. Quotation might be required differing per OS and |
| 705 |
shell-environment. |
| 706 |
|
| 707 |
=back |
| 708 |
|
| 709 |
=item --sl=C |
| 710 |
|
| 711 |
When splitting with C<-S> / C<--split>, if all criteria match and a new |
| 712 |
sheet is to be created, use the value in column C<C> of the matching row |
| 713 |
as the new sheet label. |
| 714 |
|
| 715 |
=item -v [V] |
| 716 |
|
| 717 |
=item --verbose[=V] |
| 718 |
|
| 719 |
Set verbosity level. Default = 1. No argument will set to 2. |
| 720 |
|
| 721 |
=back |
| 722 |
|
| 723 |
=head1 SEE ALSO |
| 724 |
|
| 725 |
csv2xls - Convert CSV to old Excel |
| 726 |
|
| 727 |
=head1 AUTHOR |
| 728 |
|
| 729 |
H.Merijn Brand |
| 730 |
|
| 731 |
=head1 COPYRIGHT AND LICENSE |
| 732 |
|
| 733 |
Copyright (C) 2016-2023 H.Merijn Brand. All rights reserved. |
| 734 |
|
| 735 |
This library is free software; you can redistribute and/or modify it under |
| 736 |
the same terms as Perl itself. |
| 737 |
|
| 738 |
=cut |