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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| #!/usr/bin/perl
#~
#~ Copyleft 2010 VelC.
#~ This program is free software; you can redistribute it and/or
#~ modify it under the same terms as Perl itself.
#~
#~ This program was originally written in french, therefore a lot of
#~ variables have french names. Do not hesitate to change them if you
#~ like.
#~
#~ For the CSS I choose to write it directly in each markup. That's
#~ ugly and usually to ban but it was much more easier for me to do
#~ so, since I don't know which cells have the same style, and so
#~ which cells should have the same class.
#~
#~ This program was encoded in ISO-8859-1 (Western), so that it could
#~ produce HTML files encoded in ISO-8859-1, as written in the header
#~ I wrote. To change this, simply copy-paste the source code in a
#~ UTF-8 file (for example) and change the HTML charset at line 199
#~ (look for "charset" in the file and you'll find the relevant line).
#~
#~ See http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.57/lib/Spreadsheet/ParseExcel.pm
#~ for further details about the possibilities of SpreadSheet:ParseExcel
#~
use strict;
use warnings;
use Spreadsheet::ParseExcel qw (new Parse worksheets);
use Getopt::Long;
use Encode;
use File::Basename;
use XML::Writer;
use IO::File;
#~ The -s option define the normal font-size used in the page. For
#~ example, -s 10 will define that the Excel font-size 10 equals 1em,
#~ and thus that a font-size equals to 12 will be represented by 1.2em.
#~ If left empty, the normal font-size will be 14.
#~ The -n option define the sheets that must be treated. If left empty
#~ or equals to zero, all sheets will be treated. If this value
#~ contains greater numbers than the real number of sheets in the
#~ Excel File, these numbers will be ignored. The first sheet is
#~ the sheet 1 and not 0.
#~ xls2html -n 1,2 file.xls file.html; # feuilles 1 et 2
#~ xls2html -n 1..3,5 file.xls file.html; # feuilles 1 à 3 et 5
#~ xls2html -n 2.. file.xls file.html; # toutes les feuilles à partir de la 2e
#~ xls2html -n ..4 file.xls file.html; # les 4 premières feuilles
#~ xls2html -n -1 file.xls file.html; # la dernière feuille
#~ xls2html -n 1,2 file.xls file.html; # sheets 1 and 2
#~ xls2html -n 1..3,5 file.xls file.html; # sheets 1 to 3 and 5
#~ xls2html -n 2.. file.xls file.html; # every sheets from 2nd
#~ xls2html -n ..4 file.xls file.html; # sheets 1 to 4
#~ xls2html -n -1 file.xls file.html; # last sheet
my ( $fileIn, $fileOut, $opt_s, $opt_n ) = ();
GetOptions( 'in=s' => \$fileIn, 'out=s' => \$fileOut, 's=i' => \$opt_s, 'n=s' => \$opt_n );
my $Usage = <<USAGE;
Usage : $0 [-s number][-n number][-in File_in.xls][-out File_out.html]
USAGE
unless ( $fileIn =~ /\.xls$/ and $fileOut =~ /\.html?/ ) {
die $Usage;
}
my $output;
my $normal_size = 10;
my @n_sheets;
my $nb_sheets = 0;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($fileIn);
my @worksheets = $workbook->worksheets();
my $sheets_count = $workbook->worksheet_count();
if ( defined($opt_s) ) {
( $opt_s =~ /^(\d+\.?\d*|\.\d+)$/ && $opt_s > 0 && $opt_s < 100 )
|| die "The -s option has to be followed by a valid number !";
$normal_size = $opt_s;
}
if ( defined($opt_n) ) {
for ( my $i = 0; $i < $sheets_count; $i++ ) {
$n_sheets[$i] = 0;
}
my @regtab;
my $i = 0;
while ( $opt_n =~ /(^\.\.\d+)|(\d+\.\.$)|(\d+\.\.\d+)|(\d+)|,/g ) {
$regtab[ $i++ ] = $&;
}
if (@regtab) {
for ( my $j = 0; $j < $i; $j++ ) {
if ( $regtab[$j] =~ /^\.\.(\d+)$/ ) {
for ( my $k = 0; $k < $1; $k++ ) {
$n_sheets[$k] = 1;
}
$nb_sheets += $1;
}
elsif ( $regtab[$j] =~ /^(\d+)\.\.$/ ) {
for ( my $k = $1 - 1; $k < $sheets_count; $k++ ) {
$n_sheets[$k] = 1;
}
$nb_sheets += $sheets_count - $1 + 1;
}
elsif ( $regtab[$j] =~ /^(\d+)\.\.(\d+)$/ ) {
for ( my $k = $1 - 1; $k < $2; $k++ ) {
$n_sheets[$k] = 1;
}
$nb_sheets += $2 - $1 + 1;
}
elsif ( $regtab[$j] =~ /^(\d+)$/ ) {
$n_sheets[ $1 - 1 ] = 1;
$nb_sheets++;
}
elsif ( $regtab[$j] =~ /^,$/ ) {
#~ does nothing, but ',' is a correct value
}
else {
die "An error occured, incorrect option -n.";
}
}
}
else {
die "The -n option has to be followed by a valid format ! Example : ..2,4,6..8,10..";
}
}
else {
for ( my $i = 0; $i < $sheets_count; $i++ ) {
$n_sheets[$i] = 1;
$nb_sheets++;
}
}
my $filename;
my $worksheet;
for ( my $k = 0; $k < $sheets_count; $k++ ) {
if ( $n_sheets[$k] ) {
$worksheet = $workbook->worksheet($k);
my $ligmax = $worksheet->row_range();
my $colmax = $worksheet->col_range();
if ( $fileIn =~ /^(.*)\.xls$/ && $nb_sheets == 1 ) {
$filename = $1;
}
else {
$filename = $worksheet->get_name();
}
#~ File where to write the result
#~ If there's only one sheet to produce, the name of this sheet
#~ isn't include in the name of the output file. Otherwise, it is
#~ for each produced sheet.
my ( $filename, $directories, $fileOutSuffix ) = fileparse( $fileOut, qr/\.[^.]*/ );
if ( $nb_sheets == 1 ) {
$output = new IO::File(">$directories/$filename$fileOutSuffix");
}
else {
$output = new IO::File( ">$directories/$filename" . "-" . $fileOutSuffix );
}
my $writer = new XML::Writer(
OUTPUT => $output,
DATA_INDENT => 3, # indentation, 3 espace
DATA_MODE => 1, # changement ligne.
ENCODING => 'utf-8',
);
$writer->doctype("html");
#~ Beginning of the HTML file
$writer->startTag(
"html",
"xmlns" => "http://www.w3.org/1999/xhtml",
"xml:lang" => "fr",
);
$writer->startTag("head");
$writer->startTag("title");
$writer->characters($filename);
$writer->endTag("title");
$writer->emptyTag(
'meta',
'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=iso-8859-1'
);
$writer->endTag("head");
$writer->startTag("body");
$writer->startTag("table");
#~ Iteration on each line and each column of the worksheet, writing down each cell from left to right and from top to bottom
my $bloc = '';
my @cells = $worksheet->{Cells};
for ( my $i = 0; $i <= $ligmax; $i++ ) {
$writer->startTag("tr");
for ( my $j = 0; $j <= $colmax; $j++ ) {
my $cell = $cells[0][$i][$j];
if ( defined($cell) ) {
my $contenu = $cell->value();
my $format = $cell->get_format();
my $gras = $format->{Font}->{Bold} ? "bold" : "normal";
my $italique = $format->{Font}->{Italic} ? "italic" : "normal";
my %font = (
"nom" => $format->{Font}->{Name},
"gras" => $gras,
"italique" => $italique,
"taille" => $format->{Font}->{Height},
"couleur" => $format->{Font}->{Color}
);
#~ 0 => No alignment
#~ 1 => Left
#~ 2 => Center
#~ 3 => Right
#~ 4 => Fill
#~ 5 => Justify
#~ 6 => Center across
#~ 7 => Distributed/Equal spaced
my $alignH
= $format->{AlignH} == 3 ? "right"
: ( $format->{AlignH} == 2 || $format->{AlignH} == 6 ) ? "center"
: ( $format->{AlignH} == 5 || $format->{AlignH} == 4 ) ? "justify"
: "left";
my @fill = $format->{Fill};
my $couleur = $parser->ColorIdxToRGB( $fill[0][1] );
$writer->startTag( "td",
"style" => "font-family:"
. $font{"nom"} . ';'
. 'font-size:'
. ( $font{"taille"} / $normal_size ) . 'em;'
. 'color:'
. $font{"couleur"} . ';'
. 'font-weight:'
. $font{"gras"} . ';'
. 'font-style:'
. $font{"italic"} . ';'
. 'text-align:'
. $alignH . ';'
. 'background:#'
. $couleur
. ';' );
$writer->characters($contenu);
$writer->endTag("td");
}
}
$writer->endTag("tr");
}
$writer->endTag("table");
$writer->endTag("body");
$writer->endTag("html");
$output->close();
}
}
print "\nHTML successfully generated !\n"; |
Partager