excelperlperl-module

how to apply italic format in specific words in excel using perl


Below code apply all text in italic, but i want to apply particular words to be italic.

use Win32::OLE;
    foreach my $key (keys %main){ 
            my @cnt = @{$main{$key}};
            my $count = 1;
            foreach my $cnt (@cnt){
                ++$count;
                $cnt =~ s{\n}{}g;
                $sheet -> Range("$key$count") -> {NumberFormat} = "\@"; 
                $sheet->Range("$key$count")->{Value} = "$cnt";
                $sheet -> Range("$key$count:${range}1") -> Columns -> {AutoFit} = "True";
                $sheet->Range("A1:${range}1")->Font->{Italic} = "True";

            }
        }

Solution

  • use Excel::Writer::XLSX;
    foreach my $key (keys %main){ 
            my @cnt = @{$main{$key}};
            my $count = 1;
    
            foreach my $cnt (@cnt){
                ++$count;
                $cnt =~ s{\n}{}g;
                $cnt = decode_entities($cnt);
                $cnt =~ s{&del;}{}igs;
    
                $worksheet->write_string( "$key$count", "$cnt");
                $worksheet->set_column( "$key$count", undef);
                my $bold   = $workbook->add_format( bold   => 1 );
                my $italic = $workbook->add_format( italic => 1 );
                while($cnt =~ m{^(.*)<emphasis role="italic">(.*?)</emphasis>(.*)}igs){
                    my $f =$1; my $val = $2; my $l = $3; 
                    my $gls1 = $gls; 
                    $gls1 =~ s{\\}{\/}ig;
                    $val ="<i>$val</i>" if($file =~ m{^\Q$gls1\E$}i);
                    $worksheet->write_rich_string( "$key$count","$f" ,$italic,$val,  "$l");
                }
            }