格式在Spreadsheet :: WriteExcel中是偶发的

时间:2017-11-10 21:19:29

标签: perl

在尝试使用先前定义的单元属性创建电子表格时,我遇到的问题是单元格格式化只是偶尔写入单元格中。值存在,但字体颜色等并不总是存在。

这是我的代码:

$result_file = $toxls . ".excel_props.xls";
$property_file = "properties.txt";
open (PROP, ">$property_file");

my $parser          = Spreadsheet::ParseExcel->new();
my $workbook_parse  = $parser->parse( $toxls );

my $worksheet_parse = $workbook_parse->Worksheet( $totab );
my ( $col_min, $col_max ) = $worksheet_parse->col_range();
my ( $row_min, $row_max ) = $worksheet_parse->row_range();

my $workbook   = Spreadsheet::WriteExcel->new( $result_file );
my $worksheet  = $workbook->addworksheet( $totab );
my %bkgd_color = ();

$worksheet->set_column('A:A', 12);
$worksheet->set_column('C:C', 15);
$worksheet->set_column('D:D', 30);
$worksheet->set_column('E:E', 30);
$worksheet->set_column('F:F', 35);
$worksheet->set_column('G:G', 40);
$worksheet->set_column('I:I', 40);

for my $col ( $col_min .. $col_max ) {

    for my $row ( $row_min .. $row_max ) {

        # Return the cell object at $row and $col

        my $cell = $worksheet_parse->get_cell( $row, $col );
        next unless $cell;

        my $value     = $cell->value();
        my $format    = $cell->get_format();

        my $pattern   = $format->{Fill}->[0];         
        my $color1    = $format->{Fill}->[1];
        my $color2    = $format->{Fill}->[2];

        my $font      = $format->{Font};
        my $fontcolor = $font->{Color};

        ## Change value if font color = black
        if ($fontcolor eq '32767') {
            $fontcolor = "8";
        }

        my $wrap      = $format->{Wrap};
        my $bold      = $font->{Bold};

        print PROP "\nRow, Col = ($row, $col)\n";
        print PROP "Pattern    = $pattern\n";
        print PROP "Value      = $value\n";
        print PROP "Fill       = $pattern $color1 $color2\n";
        print PROP "Wrap       = $wrap\n";
        print PROP "Font       = $fontcolor\n";
        print PROP "Bold       = $bold\n";

        if (index($value,"B\=baseline") > -1 || index($value,"B\=Baseline") > -1) { 
           $worksheet->set_row($row, 150); 
        }

        if ($pattern == 1 ){

          if ( ! exists $bkgd_color{$color1} ){

             $bkgd_color{$color1}    =  $workbook->add_format(
                                           pattern  => $pattern,
                                           bg_color => $color1,
                                           align => 'left',
                                           valign => 'top',
                                           text_wrap => $wrap,
                                           border   => 1,
                                           color  => $fontcolor,
                                           bold    => $bold
                                     );
             print PROP "Print segment = not exist bkgd_color\n";

          }

          $worksheet->write( $row, $col, $value, $bkgd_color{$color1});

        } else {

          $format = $workbook->add_format();
          $format->set_pattern($pattern);
          $format->set_align('left');
          $format->set_valign('top');
          $format->set_bg_color($color2);
          $format->set_fg_color($color1);
          $format->set_color($fontcolor);
          $format->set_text_wrap($wrap);
          $format->set_border();
          $format->set_bold($bold); 

          print PROP "Fontcolor  = $fontcolor\n";

          $worksheet->write( $row, $col, $value, $format);

       }        
    }  
}

properties.txt文件显示我在解析$ toxls电子表格时所期望的,我用它来测试。然而,当我写回$ result_file时,它并不总是产生从解析中保存的格式(字体颜色,对齐和AlignH)。有什么想法吗?

properties.txt文件看起来不错:

Row, Col = (2, 3)
Pattern    = 1
Value      = Password Requirements
Fill       = 1 22 31
Wrap       = 1
Font       = 10  <-- red text
Bold       = 1

Row, Col = (3, 3)
Pattern    = 1
Value      = Logging 
Fill       = 1 22 31
Wrap       = 1
Font       = 12   <--- blue text
Bold       = 0

Row, Col = (4, 3)
Pattern    = 0
Value      = Logging 
Fill       = 0 64 65
Wrap       = 1
Font       = 12
Bold       = 0
Fontcolor  = 12

Row, Col = (5, 3)
Pattern    = 1
Value      = AntiVirus
Fill       = 1 22 31
Wrap       = 1
Font       = 8  <-- black text 
Bold       = 0

我的目标只是阅读现有的电子表格,然后创建一个新的电子表格,但更改某些单元格的字体颜色或填充颜色除外。我尝试了这个片段,它甚至错过了许多单元格的格式化:

for my $col ( $col_min .. $col_max ) {
    for my $row ( $row_min .. $row_max ) {

       # Return the cell object at $row and $col

       my $cell = $worksheet_parse->get_cell( $row, $col );
       next unless $cell;

       my $value     = $cell->value();
       my $format    = $cell->get_format();

       $worksheet->write( $row, $col, $value, $format);

    }
}

2 个答案:

答案 0 :(得分:0)

我不太确定为什么要将格式保存到$bkgd_color{$color1},除了节省一些处理时间,但我怀疑这是你的问题所在。尝试将代码的该部分更改为:

if ( $pattern == 1 ) {
    $format = $workbook->add_format(
        pattern => $pattern,
        bg_color => $color1,
        align => 'left',
        valign => 'top',
        text_wrap => $wrap,
        border => 1,
        color => $fontcolor,
        bold => $bold
    );
    $worksheet->write( $row, $col, $value, $format);
}

答案 1 :(得分:0)

请参阅此新代码,删除bgcolor逻辑。但是,生成的电子表格只是部分格式正确:

my $parser          = Spreadsheet::ParseExcel->new();
my $workbook_parse  = $parser->parse( $toxls );

my $worksheet_parse = $workbook_parse->Worksheet( $totab );
my ( $col_min, $col_max ) = $worksheet_parse->col_range();
my ( $row_min, $row_max ) = $worksheet_parse->row_range();

my $workbook   = Spreadsheet::WriteExcel->new( $result_file );
my $worksheet  = $workbook->addworksheet( $totab );

$worksheet->set_column('A:A', 12);
$worksheet->set_column('C:C', 15);
$worksheet->set_column('D:D', 30);
$worksheet->set_column('E:E', 30);
$worksheet->set_column('F:F', 35);
$worksheet->set_column('G:G', 40);
$worksheet->set_column('I:I', 40);

for my $row ( $row_min .. $row_max ) {
    for my $col ( $col_min .. $col_max ) {

    # Return the cell object at $row and $col

    my $cell = $worksheet_parse->get_cell($row,$col);
    next unless $cell;

    my $value      = $cell->value();
    my $format     = $cell->get_format();

    my $pattern    = $format->{Fill}->[0];         
    my $color1     = $format->{Fill}->[1];
    my $color2     = $format->{Fill}->[2];
    my $wrap       = $format->{Wrap};       
    my $font       = $format->{Font};
    my $fontcolor  = $font->{Color};
    my $bold       = $font->{Bold};

    ## Change value if font color = black
    if ($fontcolor eq '32767') {
        $fontcolor = "8";
    }

    ##  Freeze after the header row

    if (index($value,"B\=baseline") > -1 || index($value,"B\=Baseline") > -1) {
       $worksheet->set_row($row, 150);
       $freezerow = $row + 1;
       $worksheet->freeze_panes($freezerow, 0); 
    }

    my $updformat = $workbook->add_format(
              pattern => $pattern,
              fg_color => $color1,
              bg_color => $color2,
              align => 'left',
              valign => 'top',
              text_wrap => $wrap,
              border => 1,
              color => $fontcolor,
              bold => $bold
    );

    $worksheet->write( $row, $col, $value, $updformat);

    }  
}