在Perl中编写Excel电子表格

时间:2015-07-24 15:11:08

标签: excel perl spreadsheet

我正在编写一个来自Perl脚本的excel电子表格,如果没有数据,则需要将特定行的行高设置为“0”。这是我到目前为止所写的一些内容。它适用于基于文本文件写入数据,但如果该项目不在文本文件中,则它会留下我想要消除的空行。任何帮助表示赞赏。

my $file = 'forecast.txt';
open my $fh, '<', $file or die "Could not open '$file' $!\n";

while (my $line =<$fh>) {
  if (index($line, 'M1501235') != -1) {

  $worksheet->write(11,0, "M1501", $print2);
  $worksheet->write(11,1, "FUND TO FUND TRANSFERS", $print);
  $worksheet->write(11,2, "DES", $print);
  $worksheet->write(11,3, "0001", $print);
  $worksheet->write(11,4, "", $print);
  $worksheet->write(11,5, "NA", $print);
  $worksheet->write(11,6, "SEE RH", $print);
  }
#  else $worksheet->set_row (11, 0);
  if (index($line, 'M2201235') != -1) {
  $worksheet->write(12,0, "M2201", $print2);
  $worksheet->write(12,1, "BPS FUND BALANCE ERROR RPT", $print);
  $worksheet->write(12,2, "L&I", $print);
  $worksheet->write(12,3, "0010", $print);
  $worksheet->write(12,4, "", $print);
  $worksheet->write(12,5, "NA", $print);
  $worksheet->write(12,6, "2 COPIES 37", $print);
  }
  else $worksheet->set_row ($_, 0) for (0..11);

elsif (index($line, 'M2301235') != -1) {
  $worksheet->write(13,0, "M2201", $print2);
  $worksheet->write(13,1, "BPS/ARC RECOCILIATION RPT/BPS CREDIT BALANCE RPT", $print);
  $worksheet->write(13,2, "L&I", $print);
  $worksheet->write(13,3, "0010", $print);
  $worksheet->write(13,4, "", $print);
  $worksheet->write(13,5, "NA", $print);
  $worksheet->write(13,6, "2 COPIES 37", $print);
  }
}

1 个答案:

答案 0 :(得分:2)

最好将行设置为隐藏而不是0。

set_row有以下参数:

set_row( $row, $height, $format, $hidden, $level, $collapsed )

第四个参数是一个切换行可见性的标志。您不想明确设置的任何参数都可以作为undef

传递

这意味着,您可以更改

$worksheet->set_row ($_, 0) for (0..11);

$worksheet->set_row ($_, undef,undef,1) for (0..11);

set_row适用于Excel::Writer::XLSXSpreadsheet::WriteExcel