如何使用perl在excel中的特定单词中应用斜体格式

时间:2016-05-03 11:06:01

标签: perl-module

下面的代码以斜体显示所有文本,但我想将特定的单词应用为斜体。

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";

            }
        }

1 个答案:

答案 0 :(得分:4)

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");
            }
        }