473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

compare excel sheets

Hi,
I'm new to perl.

I want to compare two excel sheets the second excel file is just the revised version of the first one. There are only two columns each file with a certain number of Rows

The following code is just printing all the values in it


Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use Spreadsheet::ParseExcel;
  4. use Spreadsheet::WriteExcel;
  5.      my $parser   = Spreadsheet::ParseExcel->new();
  6.      my $workbook = $parser->parse('C:\\perl\\Data dictionary\\first.xls');
  7.  
  8.      print $workbook ,"\n";
  9.      my $parser1   = Spreadsheet::ParseExcel->new();
  10.      my $workbook1 = $parser->parse('C:\\perl\\Data dictionary\\second.xls');
  11.  
  12.      print $workbook ,"\n";
  13.      print $workbook1 ,"\n";
  14.  
  15.      if ( !defined $workbook ) {
  16.          die $parser->error(), ".\n";
  17.      }
  18.  
  19.     for my $worksheet ( $workbook1->worksheets())
  20.           {
  21.          my ( $row_min, $row_max ) = $worksheet->row_range();
  22.          my ( $col_min, $col_max ) = $worksheet->col_range();
  23.  
  24.          for my $worksheet ( $workbook->worksheets())
  25.           {
  26.          my ( $row_min, $row_max ) = $worksheet->row_range();
  27.          my ( $col_min, $col_max ) = $worksheet->col_range();
  28.  
  29.  
  30.             for my $row ( $row_min .. $row_max ) {
  31.              for my $col ( $col_min .. $col_max ) {
  32.  
  33.                  my $cell = $worksheet->get_cell( $row, $col );
  34.  
  35.                  my $cell1 = $worksheet->get_cell( $row, $col );
  36.                  next unless $cell and $cell1;
  37.  
  38.          my $data = $cell->value();
  39.          #print $data;
  40.          my $search = $cell1->value();
  41.  print $search;
  42.  
  43.  
  44.  
  45.                 }
  46.             }
  47.         }
  48.     }
Apr 7 '14 #1

✓ answered by miller

You have a bug in your script because you reuse the $worksheet variable.

It honestly looks like you just copy and pasted this code from somewhere without actually reading it.

The following will get you closer to where you want to be, but I haven't not yet finished the script. It will be up to you to put the final touches on it:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. use Spreadsheet::ParseExcel;
  5. use List::Util qw(max min);
  6.  
  7. my $file1 = 'C:\\perl\\Data dictionary\\first.xls';
  8. my $file2 = 'C:\\perl\\Data dictionary\\second.xls';
  9.  
  10. my $parser   = Spreadsheet::ParseExcel->new();
  11.  
  12. my $book1 = $parser->parse($file1) // die $parser->error();
  13. my $book2 = $parser->parse($file2) // die $parser->error();
  14.  
  15. my @sheets1 = $book1->worksheets();
  16. my @sheets2 = $book2->worksheets();
  17.  
  18. if (@sheets1 != @sheets2) {
  19.     warn "Unequal number of sheets: " . @sheets1 . " vs " . @sheets2 . "\n";
  20. }
  21.  
  22. for my $i (0..min($#sheets1, $#sheets2)) {
  23.     my $sheet1 = $sheets1[$i];
  24.     my $sheet2 = $sheets2[$i];
  25.  
  26.     my ( $sheet1_row_min, $sheet1_row_max ) = $sheet1->row_range();
  27.     my ( $sheet2_row_min, $sheet2_row_max ) = $sheet2->row_range();
  28.  
  29.     warn "Sheet $i: Unequal minimum row: $sheet1_row_min vs $sheet2_row_min\n"
  30.         if ($sheet1_row_min != $sheet2_row_min;
  31.     warn "Sheet $i: Unequal maximum row: $sheet1_row_max vs $sheet2_row_max\n"
  32.         if ($sheet1_row_max != $sheet2_row_max;
  33.  
  34.     for my $row (max($sheet1_row_min, $sheet2_row_min) .. min($sheet1_row_max, $sheet2_row_max)) {
  35.         my ( $sheet1_col_min, $sheet1_col_max ) = $sheet1->col_range();
  36.  
  37.         ...
  38.     }
  39. }
  40.  
- Miller

4 2829
miller
1,089 Expert 1GB
You have a bug in your script because you reuse the $worksheet variable.

It honestly looks like you just copy and pasted this code from somewhere without actually reading it.

The following will get you closer to where you want to be, but I haven't not yet finished the script. It will be up to you to put the final touches on it:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. use Spreadsheet::ParseExcel;
  5. use List::Util qw(max min);
  6.  
  7. my $file1 = 'C:\\perl\\Data dictionary\\first.xls';
  8. my $file2 = 'C:\\perl\\Data dictionary\\second.xls';
  9.  
  10. my $parser   = Spreadsheet::ParseExcel->new();
  11.  
  12. my $book1 = $parser->parse($file1) // die $parser->error();
  13. my $book2 = $parser->parse($file2) // die $parser->error();
  14.  
  15. my @sheets1 = $book1->worksheets();
  16. my @sheets2 = $book2->worksheets();
  17.  
  18. if (@sheets1 != @sheets2) {
  19.     warn "Unequal number of sheets: " . @sheets1 . " vs " . @sheets2 . "\n";
  20. }
  21.  
  22. for my $i (0..min($#sheets1, $#sheets2)) {
  23.     my $sheet1 = $sheets1[$i];
  24.     my $sheet2 = $sheets2[$i];
  25.  
  26.     my ( $sheet1_row_min, $sheet1_row_max ) = $sheet1->row_range();
  27.     my ( $sheet2_row_min, $sheet2_row_max ) = $sheet2->row_range();
  28.  
  29.     warn "Sheet $i: Unequal minimum row: $sheet1_row_min vs $sheet2_row_min\n"
  30.         if ($sheet1_row_min != $sheet2_row_min;
  31.     warn "Sheet $i: Unequal maximum row: $sheet1_row_max vs $sheet2_row_max\n"
  32.         if ($sheet1_row_max != $sheet2_row_max;
  33.  
  34.     for my $row (max($sheet1_row_min, $sheet2_row_min) .. min($sheet1_row_max, $sheet2_row_max)) {
  35.         my ( $sheet1_col_min, $sheet1_col_max ) = $sheet1->col_range();
  36.  
  37.         ...
  38.     }
  39. }
  40.  
- Miller
Apr 8 '14 #2
sorry for the late reply Miller, but i was messed up pretty bad in the same stuff. Now I'm through and thanks to you I got the initiation atleast.

Do you by any chance how to use perl to fetch tables from microsoft access database. I ve been thinking to advance this a level up now. rather than comparing the excel if i directly fetch from access database and compare. Any ideas you can share would be very useful.
Apr 25 '14 #3
Rabbit
12,516 Expert Mod 8TB
We try to limit threads to one question each. If you need further assistance with perl, please post a new thread in that forum.
Apr 25 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
2
by: Osiris Sawiris | last post by:
I inherited an Inventory control application that updates the stock from the branches at the end of each day. We receive the stock transactions via e-mail attachments (Excel Sheets). Those...
1
by: sk | last post by:
Please forgive me if the same topic is already posted. But i havent found I have got an error while accessing the excel file as below. Can somebody reply if there is a solution Access is denied....
0
by: bero81 | last post by:
Hi to everyboby, how can i read the names of excel sheets with vb6?i'm using from my references Microsoft excel 11.0 object library.. could someone help me,please?
1
by: msanger | last post by:
Hi, I have several excel sheets in Office 2007, which has all kinds of standard calculations, and a consolidated Excel Sheet with totals and rollup of all the other excel files. The issue I am...
1
by: Rohan | last post by:
Hello, I would like to write a script which does the following job. Take column1 and 7 from 10 different excel sheets and pasthe them into a new excel worksheet. Any ideas on how to do it Thanks,
1
by: Aswanth | last post by:
I'm Using Asp.Net with C# & SSRS 2005 for Generating Reports.. I'm Having HUGE Data in Microsoft Excel Sheets .. I want to Get this Data from this Microsoft Excel Sheets & to Generate REPORTS in...
1
by: Sekhar C | last post by:
I have 100 excel sheets with same field name,with same number of fields,with similar datatype. i want to transfer data from all excel sheets to one Sql database Table using one Package, i am using...
5
by: Anuja T | last post by:
I have to compare values of two excel sheets cell by cell. can anybody suggest me how to do it? In advance Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.