title: Overwriting the existing Worksheet of a Excel File in Perl
body: <p>With the reference to <a href="https://stackoverflow.com/questions/12026229/overwrite-the-existing-worksheet-in-perl">the question I had asked before</a> is:</p>
  
  <p>My Code is as below:</p>
  
  <pre><code>        if ( !-e $xls_file ) #Checking whether to create a new Excel file or not
          {
                  #if yes create a new excel file and 4 worksheets
                  print OUTFILE "Creatin a new sheet.\n";
                  $excelFileHandle= Spreadsheet::WriteExcel-&gt;new($xls_file) ;
                  &amp;InitFormatting ($excelFileHandle);#For format of each cell.    
                  ($handle, $row, $col)    = &amp;AddSheet("$monthYearStr", "$SHEET_TITLE", @ReportColLabels);
                  ($handle1, $row1, $col1) = &amp;AddSheet("$STATUS_CODE_SHEET_NAME", "$STATUS_CODE_SHEET_TITLE", @Status_Code_labels);
                  ($handle2, $row2, $col2) = &amp;AddSheet("$SUCCESS_COUNT_SHEET_NAME", "$SUCCESS_SHEET_TITLE", @Success_count_labels);
                  ($handle3, $row3, $col3) = &amp;AddSheet("$FAILURE_COUNT_SHEET_NAME", "$FAILURE_SHEET_TITLE", @Failure_count_labels);
                  $new = 1;
          }
  
          else  #File exists needs to parsed and append the data.
          {
                  print OUTFILE "Opening the existing sheet.\n";
                  my $parser   = Spreadsheet::ParseExcel-&gt;new();
                  my $workbook = $parser-&gt;parse($xls_file);
                  if ( !defined $workbook )
                  {   
                          die $parser-&gt;error(), ".\n";
                  }   
                  my $worksheet = $workbook-&gt;worksheet($monthYearStr);
                  ( $row_min, $row_max ) = $worksheet-&gt;row_range();
                  ( $col_min, $col_max ) = $worksheet-&gt;col_range();
  
                  my $worksheet1 = $workbook-&gt;worksheet($monthYearStr);
                  ( $row_min1, $row_max1 ) = $worksheet1-&gt;row_range();
                  ( $col_min1, $col_max1 ) = $worksheet1-&gt;col_range();
  
                  $new = 0;
          }
  
          if ( $new = 1 ) 
          {
                  $handle-&gt;write($row, $col++, "$present_date", $num_field_format);
                  ------Contiuned for remainin row and col for $monthYearStr $STATUS_CODE_SHEET_NAME $SUCCESS_COUNT_SHEET_NAME $FAILURE_COUNT_SHEET_NAME-------
          }
  
          elsif ( $new == 0)
          {
                  ###Appending the data####
  
                  $parser   = Spreadsheet::ParseExcel::SaveParser-&gt;new();
             $template = $parser-&gt;Parse($xls_file);
              if ( ! defined $template )
              {
                      die $parser-&gt;error(), ".\n";
              }
  
              my $worksheet = $template-&gt;worksheet($monthYearStr);
              my $row = $row_max + 1;
  
              my $cell = $worksheet-&gt;get_cell( $row_max, $row_max );
              my $format_number = $cell-&gt;{FormatNo};
  
              $worksheet-&gt;AddCell($row, $col++, "$present_date", $format_number);
              ---------Continued for remaining row and col only for $monthYearStr $STATUS_CODE_SHEET_NAME -----------------------------
  
   }
  </code></pre>
  
  <p>Now, how do I over-write the worksheet sheet success_count and failure_count each time when the excel file exists.
  Please suggest me how to do this.</p>