OLE automation : unable to extract column data

M

Mahesh

Hi,

I was interested in been able to write a perl script to extract,for
example,all values from column 2 of an excel file, where column number
is specified as an argument.
Is there a way to extract all the values without explicitly putting
the range into the code.

Below is my code.One can see i need to specify the cells (B1:B24)
explicitly into the code.The problem is i know which column i need to
extract but the number of records in it changes from file to file.So i
wanted to automate the task and not code the value of cells in my
script.

## Start of code

use Win32::OLE;


$xlfile ="C:\\perl\\learning\\test.xls";
# Create OLE object - Excel Application Pointer
$xl_app = Win32::OLE->new('Excel.Application') or die $!;

# Set Application Visibility
# 0 = Not Visible
# 1 = Visible
$xl_app->{'Visible'} = 0;

# Open Excel File
$workbook = $xl_app->Workbooks->Open($xlfile);

# setup active worksheet
$worksheet = $workbook->Worksheets(1);

# retrieve value from worksheet
my $array = $worksheet->Range("B1:B24")->{'Value'};# get the contents

foreach my $ref_array (@$array) { # loop through the array
# referenced by $array
foreach my $scalar (@$ref_array) {
print "$scalar\t";
}
print "\n";
}


# Close It Up
$xl_app->ActiveWorkbook->Close(0);
$xl_app->Quit();

## End

Thanks,

Mahesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top