473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compare excel sheets

4 New Member
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
4 2870
miller
1,089 Recognized Expert Top Contributor
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
einstein
4 New Member
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
RonB
589 Recognized Expert Moderator Contributor
See:
DBI - Database independent interface for Perl
DBD::ODBC - ODBC Driver for DBI
Apr 25 '14 #4
Rabbit
12,516 Recognized Expert Moderator MVP
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
8340
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 the records are sorted by row numbers. (I had to split the fields to different sheets because Excel has a limit of 256 fields in each sheet) My...
2
2270
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 attachments are currently being stored manualll in a folder and then imported into the database using the transferworksheet command. I'm facing 2...
1
1819
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. Exception Details: System.UnauthorizedAccessException: Access is denied. The ASP.NET process is not authorized to access the requested resource. For...
0
1027
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
7793
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 facing is that in order to get the updated totals in the Rollup File, I have to open all the primary excel files one by one, click on update, save and...
1
1866
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
1809
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 SSRS 2005.. Please give me the Suggestions for Doing this.. Thank You..
1
2985
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 SQl Server Integration Services 2008 r2 version, Bussiness Intelligence Development Studio and MS SQL Server 2008. please guide me.
5
8930
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
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.