Current location: Hot Scripts Forums » Programming Languages » Perl » Suggestions wanted for code


Suggestions wanted for code

Reply
  #1 (permalink)  
Old 10-13-05, 12:10 PM
PerkD PerkD is offline
Newbie Coder
 
Join Date: Oct 2005
Location: Orlando Fl
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Suggestions wanted for code

Hello,

I've created some code to extract data from Excel that maintains the column headers and row count. I'm looking for suggestions on how to change the $Header call from Range to selecting values until null. I would also like to know of any suggestions for getting rid of "@Col", so that it is more dynamic.

Thanks





Code:
#!/usr/bin/perl
use OLE;
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;                                # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');  
# open Excel file
print("Opening Excel file\n");
my $Book = $Excel->Workbooks->Open("c:\\Master_DataFile.xls"); 
# select worksheet
print("Getting worksheet\n");
my $Sheet = $Book->Worksheets(1);
my @DataHeaders;    # array of data headers  # should look like ("Row_Num", "TC_Name", "NavPath" ... )
my $Headers;
my @Headers1;
my @DataTable;      # hash table for data
my @Col = ( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S","T",
"U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ");
my $NumHeaders;
print("Reading headers ...\n");
$Headers = $Sheet->Range("A2:AJ2")->{'Value'};
my $idx=0;
# Put the headers into a local array and print them out
for (@$Headers) {
    for (@$_) {
        @DataHeaders[$idx] = (defined($_) ? "$_" : "<undef>|");
        $idx++;
    }
}
# Determine the number of headers (actually this gives index of the last header read in)
$NumHeaders = $#DataHeaders;
print("NumHeaders = $NumHeaders\n");
# read in the first 2 rows of data and put them into the hash table using $Header as the key
my $j;
my $index;
my $Header;
my $k=0;
my $Cell;
my @Row;
my $Tidx=0;
my $RowName;
my %DT;
for ($j=3; $j <=400; $j++) {
    $k=0;                                               # need to know how many rows are in Excel sheet ???
    foreach $Header (@DataHeaders) {                                     # this traverses the columns
        $index = "$Col[$k]$j";  # for example "A2"                           # this gives us the cell number
            $Cell = $Sheet->Range($index)->{'Value'};
            if ("TC_Name" eq /$Header/i) { 
                $RowName = $Cell; 
            }
            $DataTable[$Tidx]{$Header} = $Cell;
            $DT{$RowName}{$Header} = $Cell;
        $k++;
    }
    $Tidx++;
}
our $variable="Build";
our $row="397";
# To access a specific value, say the 3rd row of TC_Name
if ($DataTable[$row]{$variable}eq"hooty"){
	print"Pass\n";
	print("$row row of $variable = ".$DataTable[$row]{$variable}."\n");
}
else {
	print"Fail\n";
	print($DataTable[$row]{$variable}."\n");
}

print("\nClosing book\n");
$Book->Close;
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Review & Suggestions Please gilly Website Reviews 0 07-07-05 07:02 AM
Help Wanted! SLAYER18 Job Offers & Assistance 2 01-12-05 05:57 PM
Open for suggestions? abadzhiev PHP 3 11-15-04 11:47 PM
New site needs suggestions A1nerd Website Reviews 1 04-24-04 12:53 AM
Just wanted to say hello! Tropical Tundra New Members & Introductions 3 02-05-04 12:28 AM


All times are GMT -5. The time now is 04:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.