Subject: | Create a damaged xls file |
I'm creating multi-sheet workbook using a XML source file with XML::Parser(2.30) and Spreeadsheet::WriteExcel (2.03). On the distribution MDK10.0 work fine, but on MDK8.2 kernel 2.4.18-6mdk create a unreadable damaged file.
Perl version v5.6.1
the code:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::WriteExcel;
use XML::Parser;
#
# Imposto da parametro il file XML di input e l'output XLS
#
my $XML_filename = shift or die "Missing the output file name\nUse: " . $0 . " input_xml_filename out_xls_filename\n";
my $XLS_filename = shift or die "Missing the input file name\nUse: " . $0 . " input_filename out_xls_filename\n";
#
# Inizializzo le variabili private ed imposto la variabile della prima riga per i subtotali
#
my ($workbook, $worksheet, $f_header, $f_header1, $f_body, $f_body_d);
my $first_row = 3;
#
# Imposto gli handler per il parse del file XML
#
my $parser = XML::Parser->new( Style => 'Subs',
Pkg => 'MySubs',
ErrorContext => 2);
#
# Eseguo il parse
#
$parser->parsefile($XML_filename);
#
# Sub che calcola e inserisce i subtotali
#
sub subtot {
$_[0]->write($_[3], 0, $_[2] . $_[1]);
$_[0]->write($_[3], 1, '');
$_[0]->write($_[3], 2, '=SUBTOTAL(9,C' . $_[4] . ':C' . $_[5] . ')');
$_[0]->write($_[3], 3, '=SUBTOTAL(9,D' . $_[4] . ':D' . $_[5] . ')');
$_[0]->write($_[3], 4, '=SUBTOTAL(9,E' . $_[4] . ':E' . $_[5] . ')');
$_[0]->write($_[3], 5, '=SUBTOTAL(9,F' . $_[4] . ':F' . $_[5] . ')');
$_[0]->write($_[3], 6, '=SUBTOTAL(9,G' . $_[4] . ':G' . $_[5] . ')');
$_[0]->write($_[3], 7, '=SUBTOTAL(9,H' . $_[4] . ':H' . $_[5] . ')');
$_[0]->write($_[3], 8, '=SUBTOTAL(9,I' . $_[4] . ':I' . $_[5] . ')');
$_[0]->write($_[3], 9, '=SUBTOTAL(9,J' . $_[4] . ':J' . $_[5] . ')');
$_[0]->write($_[3], 10, '=SUBTOTAL(9,K' . $_[4] . ':K' . $_[5] . ')');
$_[0]->write($_[3], 11, '=SUBTOTAL(9,L' . $_[4] . ':L' . $_[5] . ')');
$_[0]->write($_[3], 12, '=SUBTOTAL(9,M' . $_[4] . ':M' . $_[5] . ')');
$_[0]->set_row($_[3], undef, undef, 0, $_[6]);
}
#
# Definisco il package richiamato nel parser
#
{ package MySubs;
#
# Sub richiamata dall'apertura del tag report_audit
#
sub report_audit {
shift;
my ($att, $val, $element, %attr);
$element = shift;
while (@_) {
$att = shift;
$val = shift;
$attr{$att} = $val;
}
$workbook = Spreadsheet::WriteExcel->new($XLS_filename);
$f_header = $workbook->add_format(align => 'center',
valign => 'top',
border => 1,
font => 'Arial',
size => 10,
text_wrap => 1);
$f_header1 = $workbook->add_format(align => 'center',
valign => 'top',
border => 1,
font => 'Arial',
size => 10,
text_wrap => 0);
$f_body = $workbook->add_format(font => 'Arial',
size => 10,
text_wrap => 0);
$f_body_d = $workbook->add_format(font => 'Arial',
size => 10,
text_wrap => 0);
$f_body_d->set_num_format('d mmmm yyyy');
$::state::store::nomerep = $attr{'name'};
$::state::store::mese = $attr{'mese'};
$::state::store::anno = $attr{'anno'};
}
#
# Sub richiamata dalla chiusura del tag report_audit
#
sub report_audit_ {
shift;
my $element;
$element = shift;
$workbook->close() or die "Error closing file: $!";
}
#
# Sub richiamata dall'apertura del tag foglio
#
sub foglio {
shift;
my ($att, $val, $element, %attr);
$element = shift;
while (@_) {
$att = shift;
$val = shift;
$attr{$att} = $val;
}
$::state::store::banca = $attr{'banca'};
$::state::store::row = '1';
$::state::store::frow = 3;
$worksheet = $workbook->add_worksheet('Clone ' . $::state::store::banca);
$worksheet->set_column('A:A', 6.5, $f_body);
$worksheet->set_column('B:B', 6.5, $f_body);
$worksheet->set_column('C:M', 10, $f_body);
$worksheet->merge_range('A1:A2', 'Codice IPI', $f_header);
$worksheet->merge_range('B1:B2', 'Data', $f_header);
$worksheet->merge_range('C1:D1', 'Emergenza MIL normale', $f_header);
$worksheet->merge_range('E1:F1', 'Emergenza MIL immediata', $f_header);
$worksheet->merge_range('G1:H1', 'Emergenza da ambiente E', $f_header);
$worksheet->merge_range('I1:K1', 'Standard', $f_header);
$worksheet->merge_range('L1:M1', 'Totali', $f_header);
my @sheet_header = ('Packages', 'Programmi', 'Packages', 'Programmi', 'Packages', 'Programmi', 'Packages', 'Programmi','JCL','Packages','Oggetti');
$worksheet->write_row(1,2, \@sheet_header,$f_header1);
$worksheet->set_row(0, 25.50);
$worksheet->set_row(1, 12.75);
$::state::store::curripi = '';
}
#
# Sub richiamata dalla chiusura del tag foglio
#
sub foglio_ {
shift;
my $element;
$element = shift;
&main::subtot($worksheet, $::state::store::curripi, 'Totale ', ++$::state::store::row, $::state::store::frow, $::state::store::row, 1);
&main::subtot($worksheet, '', 'Totale Complessivo', ++$::state::store::row, $first_row, $::state::store::row, 0);
$worksheet->set_landscape();
$worksheet->center_horizontally();
$worksheet->repeat_rows(0, 1);
$worksheet->set_header('&C&"Arial,Bold Italic"&20' . $::state::store::nomerep . ':' . ' ' . $::state::store::mese . ' ' . $::state::store::anno);
$worksheet->set_footer('&L&"Arial,Bold Italic"&16&F&C&"Arial,Bold Italic"&16Page &P of &N&R&16&"Arial,Bold Italic"&A');
undef $::state::store::banca;
}
#
# Sub richiamata dall'apertura del tag datarow
#
sub datarow {
shift;
my ($att, $val, $element, %attr, @sheet_row);
$element = shift;
while (@_) {
$att = shift;
$val = shift;
$attr{$att} = $val;
}
@sheet_row = split(/,/,$attr{'data'});
$::state::store::row++;
if ($sheet_row[0] ne $::state::store::curripi and $::state::store::curripi ne '') {
&main::subtot($worksheet,$::state::store::curripi,'Totale ', $::state::store::row, $::state::store::frow, $::state::store::row,1);
$::state::store::row++;
$::state::store::frow = $::state::store::row+1;
}
$::state::store::curripi = $sheet_row[0];
$worksheet->write_row($::state::store::row, 0, \@sheet_row);
$worksheet->set_row($::state::store::row, undef, undef, 0, 2);
}
#
# Sub richiamata dalla chiusura del tag datarow
#
sub datarow_ {
shift;
my ($att, $val, $element, %attr, @sheet_row);
$element = shift;
while (@_) {
$att = shift;
$val = shift;
$attr{$att} = $val;
}
}
}
exit;
XML input:
<report_audit mese="giugno" anno="2004" name="Passaggi in Produzione">
<foglio banca="AV">
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
</foglio>
<foglio banca="A1">
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AC,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AE,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
<datarow data="AG,16,2,3,4,0,5,6,1,6,0,1,6"/>
</foglio>
</report_audit