【问题标题】:Getting the background color of a cell in an existing Excel spreadsheet获取现有 Excel 电子表格中单元格的背景颜色
【发布时间】:2017-09-14 13:39:39
【问题描述】:

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

我可以读取单元格内容并将其写入新的 Excel 工作表,但无法获取单元格的背景颜色。

如果我使用 $cell->get_format->{Fill}[2] 方法,如果单元格有背景颜色,它会给我像 64 或 65 这样的数字。

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

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

这是我的代码

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

打印语句的部分输出:

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

【问题讨论】:

  • 请尽量保持代码整洁,尤其是在寻求帮助之前。如果程序的缩进和布局正确,它将极大地帮助遵循程序。这次我给你做了,希望你能看到改进?也请always use strictuse warnings 'all' 在您编写的每个 Perl 程序的顶部。它是您防止基本错误和错误的第一道防线,即使在最简单的程序中也不应省略。

标签: excel perl scripting report


【解决方案1】:

更新

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

$workbook->color_idx_to_rgb($color_index)

改为



documentation for Spreadsheet::ParseExcel 这么说

$font->{颜色}

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

可以使用$workbook->ColorIdxToRGB() Parser 方法将索引转换为 RGB 字符串。

(旧版本的Spreadsheet::ParseExcel 提供了ColorIdxToRGB 类方法,已弃用。)

我希望$format->{Fill} 数组中的颜色索引可以类似地翻译

【讨论】:

  • Borodin:我尝试使用 ColorIdxToRGB,但出现错误:无法通过包“Spreadsheet::ParseExcel::Workbook”定位对象方法“ColorIdxToRGB”
  • @Venkateshk:我想你有一个Spreadsheet::ParseExcel 的旧副本。 perl -MSpreadsheet::ParseExcel -E 'say Spreadsheet::ParseExcel->VERSION' 的输出是什么?
  • @Venkateshk:请查看我的答案的更新。我查看了模块的来源,似乎文档是错误的。您需要改为致电$workbook->color_idx_to_rgb($color_index)
【解决方案2】:

你好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);
    }
}

【讨论】:

  • Hello Thanos:Thanx 的答案。但我不是在寻找设置字体颜色。我正在寻找读取单元格的背景颜色并将相同的背景颜色应用于新电子表格中的单元格。单元格的背景颜色可以通过 Spreadsheet::WriteExcel 的 set_bg_color() 设置。事情是当我我正在通过 $format->{Fill}[2] 方法读取背景颜色,无论背景颜色如何,我都会得到 65(如果没有背景颜色)或 64。
  • @Venkateshk 看看更新解决方案。试试看,让我知道它是否也适合你。
  • “调色板”——颜色索引和实际颜色值之间的映射——特定于每个电子表格。您不能只是将颜色索引从一个电子表格复制到另一个电子表格并期望得到相同的结果。
  • @Borodin 正确,如果我错了,但 OP 想要读取和解析一个 excel 文件并创建一个相同 xls 文件格式的“替代/副本”。那么为什么不使用相同的索引呢?
  • @Thanos:这就是我想要解释的。您可以使用相同的索引,但无法知道它将映射到新 Excel 工作簿中的实际颜色。它可能是相同的颜色,但不能保证。这就是模块从使用类方法Spreadsheet::ParseExcel->ColorIdxToRGB($colour_index) 更改为工作簿对象方法$workbook->color_idx_to_rgb($color_index) 的原因。
猜你喜欢
  • 2010-09-21
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
相关资源
最近更新 更多