如何为xlsx文件中的字符串着色

时间:2019-02-20 22:17:37

标签: perl

我有一个xls文件,我想用不同的颜色为一些字符串

我的xlsx文件如下所示,将其命名为 sample.xlsx

>sp|Q96B97|SH3K1_HUMAN SH3 domain-containing kinase-binding protein 1 OS=Homo sapiens OX=9606 GN=SH3KBP1 PE=1 SV=2
MVEAIVEFDYQAQHDDELTISVGEIITNIRKEDGGWW
>sp|Q9UBS4|DJB11_HUMAN DnaJ homolog subfamily B member 11 OS=Homo sapiens OX=9606 GN=DNAJB11 PE=1 SV=1
KLALQLHPDRNPDDPQAQEKFQDLGAAYEVLSDSEKRKQYD
>sp|P61916|NPC2_HUMAN NPC intracellular cholesterol transporter 2 OS=Homo sapiens OX=9606 GN=NPC2 PE=1 SV=1
CQLSKGQSYSVNVTFTSNIQSKSSKAVVHGILMGVP

我想先阅读一下,然后在给一些字母上色时保存。我想将 D 涂成黄色

>sp|Q96B97|SH3K1_HUMAN SH3 domain-containing kinase-binding protein 1 OS=Homo sapiens OX=9606 GN=SH3KBP1 PE=1 SV=2
MVEAIVEF**D**YQAQH**DD**ELTISVGEIITNIRKE**D**GGWW
>sp|Q9UBS4|DJB11_HUMAN DnaJ homolog subfamily B member 11 OS=Homo sapiens OX=9606 GN=DNAJB11 PE=1 SV=1
KLALQLHPDRNP**DD**PQAQEKFQ**D**LGAAYEVLS**D**SEKRKQY**D**
>sp|P61916|NPC2_HUMAN NPC intracellular cholesterol transporter 2 OS=Homo sapiens OX=9606 GN=NPC2 PE=1 SV=1
CQLSKGQSYSVNVTFTSNIQSKSSKAVVHGILMGVP

我试图使其失败

#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel; 
use Excel::Writer::XLSX;

# Create an Excel workbook and worksheet
my $workbook = Spreadsheet::WriteExcel->new('output.xls'); 
my $workbook  = Excel::Writer::XLSX->new( 'sample.xlsx' );
if ( !defined $workbook ) {
    die $parser->error(), ".\n";
}
my $worksheet = $workbook->add_worksheet();
my $highlight = $workbook->add_format();
$highlight->set_bg_color('yellow');
my $row = 0;
while (<sample>) {
  chomp;
  next unless /$search_pattern/D;
  my $col = 0;
  my @fields = split;
  foreach my $field (@fields) {
    if ( $field =~ /$search_pattern/D ) {
      $worksheet->write_string($row, $col, $field, $highlight);
    }
    else {
      $worksheet->write_string($row, $col, $field);
    }
    $col++;
  }
  $row++;
}
$workbook->close()

每隔一行以>sp开头,应将其忽略。搜索模式应应用于没有>sp|

的其他行

已编辑

现在我可以阅读xls并进行打印

#!/usr/bin/perl

use strict;
use warnings;
use Excel::Reader::XLSX;

    my $reader   = Excel::Reader::XLSX->new();
    my $workbook = $reader->read_file( 'sample.xlsx' );
    if ( !defined $workbook ) {
        die $reader->error(), "\n";
    }
    for my $worksheet ( $workbook->worksheets() ) {
        my $sheetname = $worksheet->name();
        print "Sheet = $sheetname\n";
        while ( my $row = $worksheet->next_row() ) {
            while ( my $cell = $row->next_cell() ) {
                my $row   = $cell->row();
                my $col   = $cell->col();
                my $value = $cell->value();
                print "  Cell ($row, $col) = $value\n";
            }
        }
    }

#Sheet = Sheet1
#  Cell (0, 0) = >sp|Q96B97|SH3K1_HUMAN SH3 domain-containing kinase-binding protein 1 OS=Homo sapiens OX=9606 GN=SH3KBP1 PE=1 SV=2
#  Cell (1, 0) = MVEAIVEFDYQAQHDDELTISVGEIITNIRKEDGGWW
#  Cell (2, 0) = >sp|Q9UBS4|DJB11_HUMAN DnaJ homolog subfamily B member 11 OS=Homo sapiens OX=9606 GN=DNAJB11 PE=1 SV=1
#  Cell (3, 0) = KLALQLHPDRNPDDPQAQEKFQDLGAAYEVLSDSEKRKQYD
#  Cell (4, 0) = >sp|P61916|NPC2_HUMAN NPC intracellular cholesterol transporter 2 OS=Homo sapiens OX=9606 GN=NPC2 PE=1 SV=1
#  Cell (5, 0) = CQLSKGQSYSVNVTFTSNIQSKSSKAVVHGILMGVP

现在,我要集中讨论单元格(1,0)单元格(3,0)单元格(5,0)

1 个答案:

答案 0 :(得分:1)

对于Excel :: Writer :: XLSX,如果要更改单个单元格的单个部分而不是整个单元格,则需要使用write_rich_string method

语法有点奇怪。

my $highlight = $workbook->add_format;
$highlight->set_bg_color('yellow');

$worksheet->write_rich_string( 'A1', 'ABC', $highlight, 'D', 'EFG' );

将字符串分成多个段,并在每个要突出显示的段之前加上$highlight格式对象。前面没有对象的段不会被格式化。

这种书写方式使其更加清晰。请记住,Perl已使列表变平,因此空列表()和多余的逗号会消失,而=> fat逗号,与,相同。 / p>

$worksheet->write_rich_string(
    'A1',                  # cell
     ()         => 'ABC',  # unformatted segment
     $highlight => 'D',    # highlighted
     ()         => 'EFG',  # unformatted
);

我在Linux上,无法访问真正的Excel。看来Libre Office不支持此功能,或者我做错了,因此未经测试。