WinXP Professional
MicroSoft Excel 2002 SP-1
ActivePerl 5.6.1 from activeState build 633
I am trying to use Perl to do the following:
1 - open an excel spreadsheet
2 - save the spreadsheet as a comma delimited file
3 - parse the csv file for certain info
4 - send appropriate emails based on that parsed info
Number 1 works. I can open/read the file contents.
Number 3 is trivial with a little splitting and regex's.
Number 4 is trivial with Mail::Sender.
I'm having trouble with Number 2. I can't get the thing to save to anything other than the Excel file format. I really want to put the content of the Excel file into a more easily
manipulated format.
The following chunk works without a the FileFormat ttributte.
It will save to the indicated name, but since the format is ommitted, it saves as an Excel file, not comma delimitted.
'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
MicroSoft Excel 2002 SP-1
ActivePerl 5.6.1 from activeState build 633
I am trying to use Perl to do the following:
1 - open an excel spreadsheet
2 - save the spreadsheet as a comma delimited file
3 - parse the csv file for certain info
4 - send appropriate emails based on that parsed info
Number 1 works. I can open/read the file contents.
Number 3 is trivial with a little splitting and regex's.
Number 4 is trivial with Mail::Sender.
I'm having trouble with Number 2. I can't get the thing to save to anything other than the Excel file format. I really want to put the content of the Excel file into a more easily
manipulated format.
The following chunk works without a the FileFormat ttributte.
It will save to the indicated name, but since the format is ommitted, it saves as an Excel file, not comma delimitted.
Code:
unless ($book->SaveAs({
Filename =>'C:\path\to\projects\data2.csv',
CreateBackup =>'False' } ))
{ warn "Failed to save data2.csv, $!\n"; }
[code]
I'd like to get the fileFormat attribute to work... something like...
[code]
my $format = 'xlCSVMSDOS';
unless ($book->SaveAs({
Filename =>'C:\path\to\projects\data2.csv',
Fileformat =>$format,
CreateBackup =>'False' } ))
{
warn "Failed to save data2.csv, $!\n";
}
[code]
Anybody have any idea what is wrong here?
Or, a better approach to solving this problem. Yeah, I know VB is the standard MicroSoft tool for this stuff. However, I'm sure that if I can get the Excel file contents into an easily handled form, the parsing and email tricks are trivial with Perl while they are not with VB.
Here is the enitre open...saveAs chunk.
[code]
#!perl -w
use strict;
use Win32::OLE;
my ($app, $xlfile, $book);
$app = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
$xlfile = 'c:\path\to\simple\excel_file\data.xls';
$book = $app->Workbooks->Open({FileName => $xlfile, ReadOnly=>1});
my $sheet = $book->Worksheets(1);
# get a ref to the range that has data
# print some data to show that we did, in fact
# get the correct file open
my $array = $sheet->Range("A1:F3")->{Value};
for (@$array) {
for (@$_) {
print defined($_) ? "$_|" : "<undef>|";
}
print "\n";
}
# save as comma delimited
my $format = 'xlCSVMSDOS';
unless ($book->SaveAs({ Filename =>'C:\path\to\projects\OLE\data2.csv',
Fileformat =>$format,
CreateBackup =>'False' } ))
{
warn "Failed to save data2.csv, $!\n";
}
#
$book -> Close();
$app -> Quit();
#_________________________________________________________________________
# VB macro saved from inside EXCEL
# Sub saveMe()
# '
# ' saveMe Macro
# ' Macro recorded 9/25/2002 by julmer
# '
# '
# ActiveWorkbook.SaveAs Filename:="C:\path\to\simple\excel_file\data.csv", _
# FileFormat:=xlCSVMSDOS, CreateBackup:=False
# End Sub
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.