| 1 | package App::Tables::Excel; | ||
| 2 | BEGIN { die "Your perl version is old, see README for instructions" if $] < 5.005; } | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use Data::Table; | ||
| 6 | use Spreadsheet::WriteExcel; | ||
| 7 | use Spreadsheet::ParseExcel; | ||
| 8 | use Spreadsheet::XLSX; | ||
| 9 | use Excel::Writer::XLSX; | ||
| 10 | use vars qw(@ISA @EXPORT @EXPORT_OK); | ||
| 11 | use Carp; | ||
| 12 | |||
| 13 | use Exporter 'import'; | ||
| 14 | |||
| 15 | @ISA = qw(Exporter AutoLoader); | ||
| 16 | # Items to export into callers namespace by default. Note: do not export | ||
| 17 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 18 | # Do not simply export all your public functions/methods/constants. | ||
| 19 | @EXPORT = (); | ||
| 20 | @EXPORT_OK = qw( | ||
| 21 | tables2xls xls2tables tables2xlsx xlsx2tables | ||
| 22 | tables_from_file | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub xls2tables { | ||
| 26 | my ($fileName, $sheetNames, $sheetIndices) = @_; | ||
| 27 | return excelFileToTable($fileName, $sheetNames, $sheetIndices, '2003'); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub xlsx2tables { | ||
| 31 | my ($fileName, $sheetNames, $sheetIndices) = @_; | ||
| 32 | return excelFileToTable($fileName, $sheetNames, $sheetIndices, '2007'); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub H_BUILT { 1 } | ||
| 36 | sub H_READ { 2 } | ||
| 37 | sub H_GUESS { 3 } | ||
| 38 | |||
| 39 | sub excelFileToTable { | ||
| 40 | my ($fileName, $sheetNames, $sheetIndices, $excelFormat, $headers_are ) = @_; | ||
| 41 | for my $h ($headers_are) { | ||
| 42 | $h = | ||
| 43 | $h eq 'built' ? H_BUILT : | ||
| 44 | $h eq 'read' ? H_READ : | ||
| 45 | $h eq 'guess' ? H_GUESS : | ||
| 46 | $h; | ||
| 47 | ($h > 0) && ($h < 4) or die; | ||
| 48 | } | ||
| 49 | |||
| 50 | my %sheetsName = (); | ||
| 51 | my %sheetsIndex = (); | ||
| 52 | if (defined($sheetNames) && ref($sheetNames) eq 'ARRAY') { | ||
| 53 | foreach my $name (@$sheetNames) { | ||
| 54 | $sheetsName{$name} = 1; | ||
| 55 | } | ||
| 56 | } elsif (defined($sheetIndices) && ref($sheetIndices) eq 'ARRAY') { | ||
| 57 | foreach my $idx (@$sheetIndices) { | ||
| 58 | $sheetsIndex{$idx} = 1; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | my $excel = undef; | ||
| 62 | if ($excelFormat eq '2003') { | ||
| 63 | $excel = Spreadsheet::ParseExcel::Workbook->Parse($fileName); | ||
| 64 | } elsif ($excelFormat eq '2007') { | ||
| 65 | $excel = Spreadsheet::XLSX->new($fileName); | ||
| 66 | } else { | ||
| 67 | croak "Unrecognized Excel format, must be either 2003 or 2007!"; | ||
| 68 | } | ||
| 69 | my @tables = (); | ||
| 70 | my @sheets = (); | ||
| 71 | my $num = 0; | ||
| 72 | foreach my $sheet (@{$excel->{Worksheet}}) { | ||
| 73 | $num++; | ||
| 74 | next if ((scalar keys %sheetsName) && !defined($sheetsName{$sheet->{Name}})); | ||
| 75 | next if ((scalar keys %sheetsIndex) && !defined($sheetsIndex{$num})); | ||
| 76 | next unless defined($sheet->{MinRow}) && defined($sheet->{MaxRow}) && defined($sheet->{MinCol}) && defined($sheet->{MaxRow}); | ||
| 77 | push @sheets, $sheet->{Name}; | ||
| 78 | #printf("Sheet: %s\n", $sheet->{Name}); | ||
| 79 | $sheet->{MaxRow} ||= $sheet->{MinRow}; | ||
| 80 | $sheet->{MaxCol} ||= $sheet->{MinCol}; | ||
| 81 | my @header = (); | ||
| 82 | foreach my $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { | ||
| 83 | my $cel=$sheet->{Cells}[$sheet->{MinRow}][$col]; | ||
| 84 | push @header, defined($cel)?$cel->{Val}:undef; | ||
| 85 | } | ||
| 86 | |||
| 87 | my $t = do { | ||
| 88 | my $h = $headers_are; | ||
| 89 | $h == H_GUESS and $h = do { | ||
| 90 | my $d = $Data::Table::DEFAULTS{CSV_DELIMITER}; | ||
| 91 | my $s = join $d, map {Data::Table::csvEscape($_)} @header; | ||
| 92 | (Data::Table::fromFileIsHeader $s, $d) | ||
| 93 | ? H_READ : H_BUILT | ||
| 94 | }; | ||
| 95 | if ( $h == H_READ ) { Data::Table->new( [], \@header, 0) } | ||
| 96 | elsif ( $h == H_BUILT ) { | ||
| 97 | Data::Table->new | ||
| 98 | ( [\@header] | ||
| 99 | , [ map "col$_", 1..($sheet->{MaxCol}-$sheet->{MinCol}+1) ] | ||
| 100 | , 0 ); | ||
| 101 | } | ||
| 102 | else { die } | ||
| 103 | }; | ||
| 104 | |||
| 105 | foreach my $row (($sheet->{MinRow}+1) .. $sheet->{MaxRow}) { | ||
| 106 | my @one = (); | ||
| 107 | foreach my $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { | ||
| 108 | my $cel=$sheet->{Cells}[$row][$col]; | ||
| 109 | push @one, defined($cel)?$cel->{Val}:undef; | ||
| 110 | } | ||
| 111 | $t->addRow(\@one); | ||
| 112 | } | ||
| 113 | push @tables, $t; | ||
| 114 | } | ||
| 115 | return (\@tables, \@sheets); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub tables_from_file { | ||
| 119 | my ( $file, %with ) = @_; | ||
| 120 | $with{headers_are} ||= 'built'; | ||
| 121 | $with{format} ||= do { | ||
| 122 | $file =~ /[.]((xls)x)$/; | ||
| 123 | $1 ? 2007 : | ||
| 124 | $2 ? 2003 : die "can't guess the excel version"; | ||
| 125 | }; | ||
| 126 | for (qw( names indices )) { $with{$_} ||= undef } | ||
| 127 | excelFileToTable $file | ||
| 128 | , @with{qw( names indices format headers_are )}; | ||
| 129 | } | ||
| 130 | |||
| 131 | # color palette is defined in | ||
| 132 | # http://search.cpan.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.20/doc/palette.html | ||
| 133 | sub oneTable2Worksheet { | ||
| 134 | my ($workbook, $t, $name, $colors, $portrait) = @_; | ||
| 135 | # Add a worksheet | ||
| 136 | my $worksheet = $workbook->add_worksheet($name); | ||
| 137 | $portrait=1 unless defined($portrait); | ||
| 138 | #my @BG_COLOR=(26,47,44); | ||
| 139 | my @BG_COLOR=(44, 9, 30); | ||
| 140 | @BG_COLOR=@$colors if ((ref($colors) eq "ARRAY") && (scalar @$colors==3)); | ||
| 141 | my $fmt_header= $workbook->add_format(); | ||
| 142 | $fmt_header->set_bg_color($BG_COLOR[2]); | ||
| 143 | $fmt_header->set_bold(); | ||
| 144 | $fmt_header->set_color('white'); | ||
| 145 | my $fmt_odd= $workbook->add_format(); | ||
| 146 | $fmt_odd->set_bg_color($BG_COLOR[0]); | ||
| 147 | my $fmt_even= $workbook->add_format(); | ||
| 148 | $fmt_even->set_bg_color($BG_COLOR[1]); | ||
| 149 | my @FORMAT = ($fmt_odd, $fmt_even); | ||
| 150 | |||
| 151 | my @header=$t->header; | ||
| 152 | if ($portrait) { | ||
| 153 | for (my $i=0; $i<@header; $i++) { | ||
| 154 | $worksheet->write(0, $i, $header[$i], $fmt_header); | ||
| 155 | } | ||
| 156 | for (my $i=0; $i<$t->nofRow; $i++) { | ||
| 157 | for (my $j=0; $j<$t->nofCol; $j++) { | ||
| 158 | $worksheet->write($i+1, $j, $t->elm($i,$j), $FORMAT[$i%2]); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } else { | ||
| 162 | for (my $i=0; $i<@header; $i++) { | ||
| 163 | $worksheet->write($i, 0, $header[$i], $fmt_header); | ||
| 164 | } | ||
| 165 | for (my $i=0; $i<$t->nofRow; $i++) { | ||
| 166 | for (my $j=0; $j<$t->nofCol; $j++) { | ||
| 167 | $worksheet->write($j, $i+1, $t->elm($i,$j), $FORMAT[$i%2]); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | sub tables2excelFile { | ||
| 174 | my ($fileName, $tables, $names, $colors, $portrait, $excelFormat) = @_; | ||
| 175 | confess("No table is specified!\n") unless (defined($tables)&&(scalar @$tables)); | ||
| 176 | $names =[] unless defined($names); | ||
| 177 | $colors=[] unless defined($colors); | ||
| 178 | $portrait=[] unless defined($portrait); | ||
| 179 | my $workbook = undef; | ||
| 180 | if ($excelFormat eq '2003') { | ||
| 181 | $workbook = Spreadsheet::WriteExcel->new($fileName); | ||
| 182 | } elsif ($excelFormat eq '2007') { | ||
| 183 | $workbook = Excel::Writer::XLSX->new($fileName); | ||
| 184 | } else { | ||
| 185 | croak "Unrecognized Excel format, must be either 2003 or 2007!"; | ||
| 186 | } | ||
| 187 | $portrait=[] unless defined($portrait); | ||
| 188 | my ($prevColors, $prevPortrait) = (undef, undef); | ||
| 189 | for (my $i=0; $i<@$tables; $i++) { | ||
| 190 | my $myColor=$colors->[$i]; | ||
| 191 | $myColor=$prevColors if (!defined($myColor) && defined($prevColors)); | ||
| 192 | $prevColors=$myColor; | ||
| 193 | my $myPortrait=$portrait->[$i]; | ||
| 194 | $myPortrait=$prevPortrait if (!defined($myPortrait) && defined($prevPortrait)); | ||
| 195 | $prevPortrait=$myPortrait; | ||
| 196 | my $mySheet = $names->[$i] ? $names->[$i]:"Sheet".($i+1); | ||
| 197 | oneTable2Worksheet($workbook, $tables->[$i], $mySheet, $myColor, $myPortrait); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | sub tables2xls { | ||
| 202 | my ($fileName, $tables, $names, $colors, $portrait) = @_; | ||
| 203 | tables2excelFile($fileName, $tables, $names, $colors, $portrait, '2003'); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub tables2xlsx { | ||
| 207 | my ($fileName, $tables, $names, $colors, $portrait) = @_; | ||
| 208 | tables2excelFile($fileName, $tables, $names, $colors, $portrait, '2007'); | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | __END__ |