I am trying to update an already existing Excel file “Test.xls” Now, instead of giving the complete path of the Test.xls file, I wish to know how to read the Test.xls file from current directory. I am using Eclipse in Windows environment. Below is my code:
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
my $Book = $Excel->Workbooks->Open("Test.xls"); #This line throws an error but works if I give complete path name as D:/eclipse/workspace/testing/Test.xls
my $Sheet = $Book->Worksheets(1);
foreach my $data (@ifrules)
{
$Sheet->Cells($row,$col)->{'Value'} =$data;
$row++;
}
$Book->Close;
I have tried various options like ./Test.xls, .\Test.xls and others too.
From what I found, you can use
getcwd()to get the current working directory. You can then concatenate that with the name of the file within$Excel->Workbooks->Open()to give the current directory plus the name of the file.Source Link
Explanation: The basic issue here is that you are passing the file name to Excel, which doesn’t know about Perl’s current working directory. That is why you have to find out the full path name and give it to
$Excel->Workbooks->Open().