473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to extract data from excel that satifies a match condition

3 New Member
I will be obliged if anybody can help me with this problem:
I am trying to extract data from an excel sheet that matches IDs given in column 4 of the excel sheet.I have stored those query IDs in an array (@names). After I look for the match in this section of the code:
if ($value=~/^$names[$k]$/), I want to write out only those rows that satisfy the above natch condition. But currently the code I have here writes out everything. How do I get it to selectively write out data?

Expand|Select|Wrap|Line Numbers
  1. # Array for the IDs that I will be looking in the excel sheet
  2. @names = (1,3,4);
  3.  
  4. foreach my $ref (@names) {
  5.     chomp($ref);
  6.  
  7. }
  8. #===================================================
  9.  
  10. my $kk=@names;
  11. use strict;
  12. use Win32::OLE qw(in with);
  13. use Win32::OLE::Const 'Microsoft Excel';
  14. $Win32::OLE::Warn = 3; # die on errors...
  15.  
  16. # get already active Excel application or open new
  17. my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
  18. || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel
  19. # application or open new
  20.  
  21. # open Excel file 
  22.  
  23. my $file = "C:\\Data\\Projects\\iavarone\\data_extraction\\Samples_table.xls";
  24. my $Book = $Excel->Workbooks->Open($file); 
  25. # select worksheet number 4 (you can also select a worksheet by name)
  26. my $Sheet = $Book->Worksheets(5);
  27. #defining the row and column to look for the IDs stored in @names array
  28. foreach my $row (2..540) 
  29.     {
  30.     foreach my $col (4)
  31.         {
  32. # skip empty cells
  33.     next unless defined $Sheet->Cells($row,$col)->{'Value'};
  34.               $value = $Sheet->Cells($row,$col)->{'Value'};
  35.  
  36. for ($k=0;$k<$kk;$k++) 
  37. {if ($value=~/^$names[$k]$/)
  38. {
  39. my $array = $Sheet->Range("A1:H540")->{'Value'};
  40. foreach my $cell_ref (@$array) {
  41. foreach my $value1 (@$cell_ref) {
  42. print "$value1\t";
  43.         }
  44. print "\n";
  45. }
  46.  
  47. }
  48. }
  49. }
  50. }
Apr 3 '08 #1
1 5055
nithinpes
410 Recognized Expert Contributor
Should ideally work if you change:
Expand|Select|Wrap|Line Numbers
  1. for ($k=0;$k<$kk;$k++) 
  2. {if ($value=~/^$names[$k]$/)
  3. {
  4. my $array = $Sheet->Range("A1:H540")->{'Value'};
  5. foreach my $cell_ref (@$array) {
  6. foreach my $value1 (@$cell_ref) {
  7. print "$value1\t";
  8.         }
  9. print "\n";
  10. }
  11. }
  12. }
  13.  
  14.  
to:

Expand|Select|Wrap|Line Numbers
  1. my $array = $Sheet->Range("A1:H540")->{'Value'};
  2. foreach my $cell_ref (@$array) {
  3. foreach my $value1 (@$cell_ref) {
  4. my $found=0;
  5. for ($k=0;$k<$kk;$k++) {
  6. if($value1=~/^$names[$k]$/) {print "$value1\t" ; $found=1;} 
  7. }
  8. print "\t" if($found==0); ###leave blank cells if not matched
  9.  }
  10. print "\n";
  11. }
  12.  
  13.  
Apr 4 '08 #2

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

Similar topics

0
5484
by: Ashok | last post by:
Hi, is it possible to extract data from a web based java applet in order to enter that data in mysql? for example, something that would let me extract the data shown in applet on http://gcitrading.com/forex-quotes.htm and enter it in mysql. The data needs to be compared too so that any changes that this applet shows is entered with a time...
8
6040
by: nick | last post by:
Hi all can any one please tell me what is wrong in this code?? I'm new to deal with text files and extract data. i'm trying to look for data in a text file (3~4 pages) some lines start with a word "red" first if find(red) then print the last 5 letters of that string and if red is not found at the begining of the string then do nothing and...
1
1223
by: Gonzosez | last post by:
How can I extract data from an excel spread sheet? I know the sheet name but the range varies.
2
3832
by: missolsr | last post by:
hi, I am using jpcap to capture OLSR topology control (udp) packets. Does anyone know how to extract data (the way ethereal does it) from the olsr packet? There are methods to extract data from udp and IP packets in jpcap but the issue is that olsr packets have their own header-data and since jpcap can not dig that far, I get nonsense as...
5
3579
by: ElTipo | last post by:
Hello People, I made a data base with secure wizard to provide to users a PID and Passwords. I need to extract data from Crystal Reports 7 in this data base but Crystal Reports send me a message like I cant extract data because I don't have rights to this data base. I am the "Admin" I don't Know what happens in this case. I try to change the...
1
4321
by: fly2irfan | last post by:
Hi All, I am new to IT/Developer Network I have to create an application which has to Extract data from Excel Spreadsheet using C# or VB.net then save the data into SQL database. Can anybody help me out in this regard. Regards, Cool
5
2834
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements in it. 0-9 are related data, 10-19, 20-29 are related and so on. What is the best way of extracting groups of elements from the array into...
1
1449
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, is it possible to extract data from Pdf file, in several formats, like .txt or Excel. And from an aspx page (ASP.NET 2.0 - C#). Thanks in advance. -- Luigi
1
3061
by: veer | last post by:
Hi i am making a program in which i want to extract data from html file . Actually there are two dates on html file i want to extract these dates but the main probleum is that these dates are different on each file. A word "AKTIVA" is always comes before these dates. i made this by seaching the activa word but after this i am not getting any...
0
7479
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
7669
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. ...
0
7926
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3468
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
3450
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.