Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I create excel files with ActiveState Perl OLE interface?? 1

Status
Not open for further replies.

perl8rookie

Programmer
Apr 25, 2008
4
0
0
US
I found the following code to read an existing excel file:

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');

# open Excel file
my $Book = $Excel->Workbooks->Open("C:/Temp/Book1.xls");

How do I write code to create a new file??? Please help.

 
You're using the Open method, which opens an existing file.
You want to just Add a workbook, and then save it...

Code:
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;          # die on errors...

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
       || Win32::OLE->new('Excel.Application', 'Quit');

## open Excel file
my $Book = $Excel->Workbooks->Add();

## Prove you did it
$Book->Worksheets("Sheet1")->{Name} = "Created With Perl";

## and now save it
$Book->SaveAs("c:/scratch/temp.xls");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top