在现有的Excel电子表格中获取单元格的背景颜色

我正在尝试读取现有的Excel电子表格,并将其写入具有所有相同格式选项的新Excel文档中。

我能够读取单元格内容并将它们写入新的Excel表单中,但无法获取单元格的背景颜色。

如果我使用$cell->get_format->{Fill}[2]方法,那么如果单元格具有背景颜色,则会给出数字,如64或65。

如何获得单元格的实际颜色,并将相同的背景颜色应用于新Excel表格中的单元格?

我正在做所有这些事情,因为Spreadsheet::ParseExcel模块中没有方法可以在将数据附加到现有Excel工作表之后设置单元格的背景颜色。

这是我的代码

 use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook_parse = $parser->Parse( 'Report.xls' ); my $worksheet_parse = $workbook_parse->Worksheet( "Health_Report" ); my ( $col_min, $col_max ) = $worksheet_parse->col_range(); my ( $row_min, $row_max ) = $worksheet_parse->row_range(); my $workbook = Spreadsheet::WriteExcel->new( "Report_new.xls" ); my $worksheet = $workbook->addworksheet( "Health_Report" ); my $bkgd_color = $workbook->addformat(); 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 $backgroundcolor = $format->{Fill}->[2]; print "Row, Col = ($row, $col) "; print "Value = $value\n"; print "Format = $backgroundcolor\n"; $bkgd_color->set_bg_color( $backgroundcolor ); ### Here trying to rewrite into Excel and apply the ### same background color which the cell had previously $worksheet->write( $row, $col, $value, $bkgd_color ); } } 

我从print语句输出的一部分:

 Format = 65 Row, Col = (25, 4) Value = -115966 Format = 65 Row, Col = (10, 5) Value = 20170417 Format = 65 Row, Col = (11, 5) Value = 0 Format = 64 Row, Col = (16, 5) Value = 0 Format = 64 

更新

看来文档是错误的。 工作簿对象方法的名称与类方法的名称不同,需要调用

 $workbook->color_idx_to_rgb($color_index) 

代替


Spreadsheet::ParseExcel的文档说明了这一点

$字体- > {}颜色

返回字体的颜色索引。 映射到RGB颜色由每个工作簿定义。

可以使用$workbook->ColorIdxToRGB()parsing器方法将索引转换为RGBstring。

(较旧版本的Spreadsheet::ParseExcel提供了ColorIdxToRGB类方法,不推荐使用该方法。)

我期望$format->{Fill}数组中的颜色索引可以被类似地转换

你好Venkatesh k ,

尝试从( Spreadsheet :: WriteExcel )的set_color()方法。

从文档:

  Default state: Excels default color, usually black Default action: Set the default color Valid args: Integers from 8..63 or the following strings: 'black' 'blue' 'brown' 'cyan' 'gray' 'green' 'lime' 'magenta' 'navy' 'orange' 'pink' 'purple' 'red' 'silver' 'white' 'yellow' 

更新:尝试这个,它似乎是我的示例电子表格工作。

 #!/usr/bin/perl use strict; use warnings; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; sub create_new_worksheet { my ($row, $col, $value, $backgroundcolor) = @_; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new('perl.xls'); # Add a worksheet my $worksheet = $workbook->add_worksheet(); # Add and define a format my $format = $workbook->add_format(bg_color => $backgroundcolor); $format->set_align('center'); # Write a formatted and unformatted string, row and column notation. $worksheet->write($row, $col, $value, $format); } my $parser = Spreadsheet::ParseExcel->new(); my $workbook_parse = $parser->Parse( 'Report.xls' ); my $worksheet_parse = $workbook_parse->Worksheet("Sheet1"); my ( $col_min, $col_max ) = $worksheet_parse->col_range(); my ( $row_min, $row_max ) = $worksheet_parse->row_range(); 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 $backgroundcolor = $format->{Fill}->[2]; print "Row, Col = ($row, $col) "; print "Value = $value\n"; print "Format = $backgroundcolor\n"; create_new_worksheet($row, $col, $value, $backgroundcolor); } }