use strict; use warnings; use Excel::Writer::XLSX; my $in_file = "filein.xlsx"; my $out_file = "fileout.xlsx"; # Read in all rows from existing file my $rows = read_excel($in_file); my $AL_zip = '35007'; my $AK_zip = '99501'; my $rowcount = 0; # Update the rows for my $row (@$rows) { if($row->[6] eq 'AL' && $row->[7]) { $row->[7] = $AL_zip; } elsif(length($row->[8]) > 10 || (length $row->[8]) < 10) { ($row->[0], $row->[1], $row->[3], $row->[4]) = undef; } } # add headers as first row unshift(@$rows, $headers); # Write the updated rows to new file my $col_num = scalar @$headers - 1; write_excel($out_file, $rows, $col_num); sub read_excel { my ( $file, $sheet ) = @_; $sheet ||= 0; my $parser = Spreadsheet::ParseXLSX->new(); my $workbook = $parser->parse($file); if ( not defined $workbook ) { die $parser->error; } my $worksheet = $workbook->worksheet($sheet); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my @rows; for my $row ( $row_min .. $row_max ) { my @cells; for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); if (not $cell) { push(@cells,''); next; } my $value = $cell->value(); push(@cells,$value); } push(@rows,\@cells); } return \@rows; } sub write_excel { my ( $file, $rows, $col_max ) = @_; my $workbook = Excel::Writer::XLSX->new( $file ); if ( not defined $workbook ) { die "Could not open file: $!"; } my $worksheet = $workbook->add_worksheet(); my $worksheet2 = $workbook->add_worksheet(); my $row_num = 0; for my $row ( @$rows ) { for my $col (0 .. $col_max) { $worksheet->write( $row_num, $col, $row->[$col] ); } $row_num++; } $workbook->close(); return; }