简体   繁体   中英

Writing Excel Spreadsheet in Perl

I'm working on writing an excel spreadsheet from a Perl script and need to set the row height of specific rows to "0" if no data exists. Here is some of what I have written so far. It works on writing data based on the text file, but if the item is not in the text file then it leaves an empty row which I want to eliminate. Any help is appreciated.

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

It would be better to set the row to hidden instead of 0.

set_row has the following arguments:

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

The fourth argument is a flag that toggles the visibility of the row. Any arguments that you don't want to explicitly set can be passed in as undef

This means, you can change

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

to

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

set_row works the same for Excel::Writer::XLSX and Spreadsheet::WriteExcel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM