writing the header content only once in excel using perl ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayarl
    New Member
    • Sep 2008
    • 65

    writing the header content only once in excel using perl ???

    Hi All,

    To Moderator :
    Sorry i have posted this querry to member rather putting in the forum.

    To All:

    Problem stmt:

    I am reading the data from the excel files & writing its content to another excel file.problem is that everytime it reads data it writes all the content.
    i want the script to write the header only once.

    output look like this:
    Code:
    DD/MM/YYYY	HH:MM:SS	r/s	w/s	kr/s	kw/s
    18/01/2008	18:00:00	0.5	2.9	15	31.5
    18/01/2008	18:00:00	5	0	0	0
    
    DD/MM/YYYY	HH:MM:SS	r/s	w/s	kr/s	kw/s
    18/01/2008	18:00:00	0.5	1.9	13	30.5
    18/01/2008	18:00:00	2	0	0	0
    
    DD/MM/YYYY	HH:MM:SS	r/s	w/s	kr/s	kw/s
    18/01/2008	18:00:00	0.5	3.9	11	32.5
    18/01/2008	18:00:00	0	0	1	0
    in the above output data, i need script to write header only once.

    script goes like this :
    Code:
    # Build an array of the stats type to be collated
    #my @stat_type=split(/:/, $PARAMS{COLLATE_STAT_TYPES});
    	@stat_type=@uniq;
      # for each of the stats type, read the xls of each hour and write into a consolidated xls.
      foreach my $stat_type_token (@stat_type){
    	
    			my $con_wb = Spreadsheet::WriteExcel::Big->new("$PARAMS{INPUT_FILE_BASE_PATH}$stat_type_token.xls");
    			my $con_excel = $con_wb->add_worksheet();
    			
    			my $con_row = 0;
    			    
    			    my @stat_files=split(/:/, $PARAMS{COLLATE_YYYYMMDD_HH});
    			    	    
    			      foreach my $stat_files_token (@stat_files) {
    				  
    			      	
    			      	my $source_xl_name = $PARAMS{INPUT_FILE_BASE_PATH}.$stat_type_token."-".$stat_files_token.".xls";
    			    	
    					
    			    	my $source_excel = new Spreadsheet::ParseExcel;
    			    	my $source_book = $source_excel->Parse($source_xl_name) or die "Could not open source Excel file $source_xl_name !";
    			    	
    			    	my $source_sheet = $source_book->{Worksheet}[0];
    			    	print "Processing $stat_type_token-$stat_files_token.xls please wait\n";
    			    	foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
      							
      							my $con_col = 0;
      							
      							foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
       									
       									my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
       									
       									
    		   									if (defined $source_cell) {
    		   									   $con_excel->write($con_row, $con_col, $source_cell->Value );
    		   									}
       									
    	       	 						
    	       	 						
    	       	 						$con_col++;
       									
              	                }
              	               
              	               $con_row++; 
              	               $consol_rows++;
              	               
              	               # max row count in Excel is 65536 rows.
              	               # open a new sheet if the max row is reached.
              	               if ( $consol_rows > 65000 ) {
              	               	
       										$consol_rows = 0;
       										$con_row = 0;
       										$con_col = 0;
       										$con_excel = $con_wb->add_worksheet();
       									}
    			        
    			    	
    			    	}
    	            
            }	
      
          $con_wb->close();
          
      			
      }
    Can anyone help me on this ???

    Sorry again kevin,jeff & moderator !!!!

    Regards,
    Vijayarl
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    try this, this code is untested.

    but this should be what you want...


    Replace:

    Code:
     if (defined $source_cell) {
                                                      $con_excel->write($con_row, $con_col, $source_cell->Value );
                                                   }

    With:

    Code:
     $a=1;
     if (defined $source_cell) {
    
     if (($source_cell->Value eq "DD/MM/YYYY") 
     or ($source_cell->Value eq "HH:MM:SS") 
     or ($source_cell->Value eq "r/s") 
     or ($source_cell->Value eq "w/s") 
     or ($source_cell->Value eq "kr/s") 
     or ($source_cell->Value eq "kw/s"))
    {
    if ($a < 6)
    {
    	$con_excel->write($con_row, $con_col, $source_cell->Value );
    	$a++;
           
    }
    }
    else
    {
    $con_excel->write($con_row, $con_col, $source_cell->Value );
    }  
    }
    Last edited by Icecrack; Oct 1 '08, 12:15 AM. Reason: Why use while if you can use if ... Doh'

    Comment

    • vijayarl
      New Member
      • Sep 2008
      • 65

      #3
      Thanks Icecrack for giving a try...

      i have followed what u said but still am getting the same result ie, multiple headers in output file.

      hey can we do like this, everytime script writes the content check for the first line content in the output file.if it exists then skip the line else append the data in the excel file.
      can we do this approach ??? fi so how to do that in the script ???

      Regards,
      vijayarl

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Originally posted by vijayarl
        Thanks Icecrack for giving a try...

        hey can we do like this, everytime script writes the content check for the first line content in the output file.if it exists then skip the line else append the data in the excel file.
        can we do this approach ??? fi so how to do that in the script ???

        Regards,
        vijayarl
        We can do that. For this, replace the following portion of script:
        Code:
        if (defined $source_cell) { 
                         $con_excel->write($con_row, $con_col, $source_cell->Value ); 
                                                }
        with:

        Code:
         my $head_cell = $source_sheet->{Cells}[0][$col_index];
          if (defined $source_cell) { 
        unless(($source_cell->Value  eq $head_cell -> Value) && ($source_cell ne $head_cell) ) {
                      $con_excel->write($con_row, $con_col, $source_cell->Value ) ; 
                                                       } }

        Comment

        • vijayarl
          New Member
          • Sep 2008
          • 65

          #5
          hi nithinpes !!!,

          I try what u have said, it works fine if we have only one <stat>.xls file.

          bu the problem is there will be a more than 1 <stat_type>.x ls file, the above code what i have pasted will collect all the data from the <stat_type>.x ls & writes them in one<stat_type>. xls file.

          i mean i will be having these *.xls file:
          Code:
          iostat-xn-20080118-1800.xls
          iostat-xn-20080118-1900.xls
          iostat-xn-20080118-2000.xls
          netstat-i-20080118-1800.xls
          netstat-i-20080118-1900.xls
          prstat-Ls-20080118-1800.xls
          prstat-Ls-20080118-1900.xls
          prstat-Lvs-20080118-2000.xls
          prstat-Lvs-20080118-2000.xls
          Each of these file will have header, while reading these file, everytime script writes the header in the output file.

          i will get the output file as , (after reading each of the stat_type.xls):
          Code:
          iostat-xn.xls
          netstat-i.xls
          prstat-Ls.xls
          prstat-Lvs.xls
          All i want is write header only once in each of the output file...

          can we do this ???

          Thanks
          Vijayarl

          Comment

          • mlpkumar
            New Member
            • Oct 2008
            • 4

            #6
            try like this
            Code:
             if (defined $source_cell) {
             if (!($source_cell->Value =~ /^DD/))   ### newly added
              { ### newly added 
             $con_excel->write($con_row, $con_col, $source_cell->Value );
              } ### newly added
             }
            Last edited by numberwhun; Oct 7 '08, 03:25 PM. Reason: Please use code tags

            Comment

            • vijayarl
              New Member
              • Sep 2008
              • 65

              #7
              No !!!! still didn't work...
              still i get the same result :-(

              Comment

              • Icecrack
                Recognized Expert New Member
                • Sep 2008
                • 174

                #8
                Originally posted by mlpkumar
                try like this

                Code:
                 if (defined $source_cell) {
                 if (!($source_cell->Value =~ /^DD/))   ### newly added
                  { ### newly added 
                 $con_excel->write($con_row, $con_col, $source_cell->Value );
                  } ### newly added
                }

                please use code tags,

                Comment

                • vijayarl
                  New Member
                  • Sep 2008
                  • 65

                  #9
                  Hi All,

                  I thought of doing like this now, just delete the first line(column) from the excel file then write the content in one excel file.

                  ie, if i have 8 stat excel file
                  1. open the 2nd file, delete the 1st column repeat the same for other 6 files.
                  2. start wiritng the content in 1 excel files.

                  but my problem is how to delete the 1st column (line) in excel file using perl ??
                  if i get this i guess i can achive other things .......


                  Thanks,
                  Vijayarl

                  Comment

                  • vijayarl
                    New Member
                    • Sep 2008
                    • 65

                    #10
                    Hi Everyone,

                    Going forward a step ahead... i was able to wirte the contents but without any header in the output file.

                    Code:
                    # Build an array of the stats type to be collated
                    #my @stat_type=split(/:/, $PARAMS{COLLATE_STAT_TYPES});
                    	@stat_type=@uniq;
                      # for each of the stats type, read the xls of each hour and write into a consolidated xls.
                      foreach my $stat_type_token (@stat_type){
                    	
                    			my $con_wb = Spreadsheet::WriteExcel::Big->new("$PARAMS{INPUT_FILE_BASE_PATH}$stat_type_token.xls");
                    			my $con_excel = $con_wb->add_worksheet();
                    			my $con_row = 0;
                    			my @stat_files=split(/:/, $PARAMS{COLLATE_YYYYMMDD_HH});
                    			my $first_file="yes";
                    			      foreach my $stat_files_token (@stat_files) {
                    			      	my $source_xl_name = $PARAMS{INPUT_FILE_BASE_PATH}.$stat_type_token."-".$stat_files_token.".xls";
                    			    	my $source_excel = new Spreadsheet::ParseExcel;
                    			    	my $source_book = $source_excel->Parse($source_xl_name) or die "Could not open source Excel file $source_xl_name !";
                    			    	my $source_sheet = $source_book->{Worksheet}[0];
                    			    	print "Processing $stat_type_token-$stat_files_token.xls please wait\n";
                    			    	foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
                    						next if $row_index eq (($source_sheet->{MinRow}) && ($first_file eq "no"));
                    						####$first_file="no";
                      							my $con_col = 0;
                      							foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
                    								#next if $row_index eq (($source_sheet->{MinRow}) && ($first_file eq "no"));
                       									my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
                    		   									#if (defined $source_cell) {	
                    											# if (($source_cell->Value =~ /^DD/)) 
                    											#	 { 
                    											#		 $con_excel->write($con_row, $con_col, $source_cell->Value );
                    											#	 }
                    		   									  ###$con_excel->write($con_row, $con_col, $source_cell->Value );
                    		   									#}
                       									my $head_cell = $source_sheet->{Cells}[0][$col_index]; 
                    									if (defined $source_cell) {
                    									#print "$source_cell\n";
                    									###unless(!($source_cell->Value  eq $head_cell -> Value) && ($source_cell ne $head_cell)) { 
                    									###unless(($source_cell->Value eq $head_cell -> Value) && ($source_cell ne $head_cell)) { 
                    									$con_excel->write($con_row, $con_col, $source_cell->Value ) ;  
                                                            } 
                    									#} 
                    	       	 						$con_col++;	
                              	                }
                              	               $con_row++; 
                              	               $consol_rows++;
                              	               # max row count in Excel is 65536 rows.
                              	               # open a new sheet if the max row is reached.
                              	               if ( $consol_rows > 65000 ) {
                       										$consol_rows = 0;
                       										$con_row = 0;
                       										$con_col = 0;
                       										$con_excel = $con_wb->add_worksheet();
                       									}
                    							$first_file="no";
                    						
                    					} 
                            }	
                          $con_wb->close();
                      }
                      print "Processing Done. Time to analyse\n";
                    all i did is, introduce a flag variable, to check for the 2nd file name & then used next if statment to skip the header content.

                    Code:
                    next if $row_index eq (($source_sheet->{MinRow}) && ($first_file eq "no"));
                    now what should i change in the script, so that i will get the output file having only once header content.

                    any help plz !!!!!

                    Regards,
                    Vijayarl

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      The logic is perfectly right, but for the line where you modify the value of $first_file. This should be after the first iteration of row index, at the end of foreach my $row_index() block

                      Code:
                      foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow})  {
                      .
                      .
                      .  foreach my $col_index(....) {
                      .
                      .
                      ;  }
                      
                      $first_file = "no";
                         }

                      Comment

                      • vijayarl
                        New Member
                        • Sep 2008
                        • 65

                        #12
                        hi nithinpes,

                        i tried what u said..but still i get the output file without header content..

                        Code:
                        foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
                        						next if $row_index eq (($source_sheet->{MinRow}) && ($first_file ne "no"));
                          							my $con_col = 0;
                          							foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
                           									my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
                           									my $head_cell = $source_sheet->{Cells}[0][$col_index]; 
                        									if (defined $source_cell) {
                        									unless(($source_cell->Value ne $head_cell -> Value) && ($source_cell eq $head_cell)) { 
                        									$con_excel->write($con_row, $con_col, $source_cell->Value );
                                                                } 
                        									} 
                        	       	 						$con_col++;
                        									$first_file="no";
                                  	                }
                        even tried placing the $first_file variable outside the forloop
                        Code:
                        foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) {
                        						next if $row_index eq (($source_sheet->{MinRow}) && ($first_file ne "no"));
                          							my $con_col = 0;
                          							foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) {
                           									my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
                           									my $head_cell = $source_sheet->{Cells}[0][$col_index]; 
                        									if (defined $source_cell) {
                        									unless(($source_cell->Value ne $head_cell -> Value) && ($source_cell eq $head_cell)) { 
                        									$con_excel->write($con_row, $con_col, $source_cell->Value );
                                                                } 
                        									} 
                        	       	 						$con_col++;
                                  	                }
                        $first_file="no";
                        but i got the same result :-(
                        don't knw where am doing mistake ??? not getting any idea ... look like am blocked...

                        plz help me..

                        Regards,
                        Vijayarl

                        Comment

                        • vijayarl
                          New Member
                          • Sep 2008
                          • 65

                          #13
                          Hi All,

                          Thanks for your patience reply.....

                          At last i got the desired result..

                          Thanks to sycoogtit for his reply...

                          Thanks,
                          Vijayarl

                          Working Code:
                          Code:
                          # Build an array of the stats type to be collated
                          #my @stat_type=split(/:/, $PARAMS{COLLATE_STAT_TYPES});
                          @stat_type=@uniq;
                          # for each of the stats type, read the xls of each hour and write into a consolidated xls.
                          
                          foreach my $stat_type_token (@stat_type){
                              
                              my $con_wb = Spreadsheet::WriteExcel::Big->new("$PARAMS{INPUT_FILE_BASE_PATH}$stat_type_token.xls");
                              my $con_excel = $con_wb->add_worksheet();
                              my $con_row = 0;
                              my @stat_files=split(/:/, $PARAMS{COLLATE_YYYYMMDD_HH});
                              my $first_file="yes";
                              
                              foreach my $stat_files_token (@stat_files) {
                              
                                  my $source_xl_name = $PARAMS{INPUT_FILE_BASE_PATH}.$stat_type_token."-".$stat_files_token.".xls";
                                  my $source_excel = new Spreadsheet::ParseExcel;
                                  my $source_book = $source_excel->Parse($source_xl_name) or die "Could not open source Excel file $source_xl_name !";
                                  my $source_sheet = $source_book->{Worksheet}[0];
                                  print "Processing $stat_type_token-$stat_files_token.xls please wait\n";
                              
                                  foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}){
                                  
                                      #next if $row_index eq (($source_sheet->{MinRow}) && ($first_file ne "no"));
                                      next if $row_index == $source_sheet->{MinRow} && $first_file eq "no";
                                      my $con_col = 0;
                                  
                                      foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}){
                                  
                                          my $source_cell = $source_sheet->{Cells}[$row_index][$col_index];
                                          my $head_cell = $source_sheet->{Cells}[$row_index][$col_index];
                                          if (defined $source_cell) {
                                              $con_excel->write($con_row, $con_col, $source_cell->Value );
                                          }
                                          $con_col++;    
                                      }
                                      $con_row++;
                                      $consol_rows++;
                                      # max row count in Excel is 65536 rows.
                                      # open a new sheet if the max row is reached.
                                      if ( $consol_rows > 65000 ) {
                                          $consol_rows = 0;
                                          $con_row = 0;
                                          $con_col = 0;
                                          $con_excel = $con_wb->add_worksheet();
                                      }
                                  }
                                  $first_file="no";
                              }
                              $con_wb->close();
                          }
                          &write_output;
                          #&call_sendmail;
                          print "Processing Done. Time to analyse\n";

                          Comment

                          Working...