473,396 Members | 2,002 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,396 software developers and data experts.

how to extract data from excel that satifies a match condition

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 5036
nithinpes
410 Expert 256MB
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
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...
8
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...
1
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
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...
5
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...
1
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...
5
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...
1
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
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...

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.