Linux example: parsing
This section applies to UNIX, and specifically Linux. It has not been tested under Windows.
It would be difficult to give a better example of parsing with Linux than the one provided in the documentation for the Spreadsheet::ParseExcel
module, so I will show that example and then explain how it works.
|
This example was tested with Excel 97. If it does not work, try converting to the Excel 97 format. The perldoc page for Spreadsheet::ParseExcel
claims Excel 95 and 2000 compatibility as well.
The
spreadsheet is parsed into a top-level object called $oBook. $oBook has
properties to aid the program, such as "File," "SheetCount," and
"Author." The properties are documented in the perldoc page for Spreadsheet::ParseExcel
, in the workbook section.
The
workbook contains several worksheets; iterate through them by using the
workbook SheetCount property. Each worksheet has a MinRow and MinCol
and corresponding MaxRow and MaxCol properties, which can be used to
figure out the range the worksheet can access. The properties are
documented in the perldoc page for Spreadsheet::ParseExcel
, in the worksheet section.
Cells
can be obtained from a worksheet through the Cells property; that's how
the $oWkC object is obtained in Listing 3. Cell properties are
documented in the perldoc page for Spreadsheet::ParseExcel
,
in the Cell section. There does not seem to be a way, according to the
documentation, to obtain the formula listed in a particular cell.
Linux example: writing
This section applies to UNIX, and specifically Linux. It has not been tested under Windows.
Spreadsheet::WriteExcel
comes with a lot of example
scripts in the Examples directory, usually found under
/usr/lib/perl5/site_perl/5.6.0/Spreadsheet/WriteExcel/examples. It may
have been installed elsewhere; consult with your local Perl
administrator if you can't find that directory.
The bad news is that Spreadsheet::WriteExcel
can not be used to write to an existing Excel file. You have to import data from an existing Excel file yourself, using Spreadsheet::ParseExcel
. The good news is that Spreadsheet::WriteExcel
is compatible with Excel 5 up to Excel 2000.
Here's a program that will demonstrate how data can be extracted from an Excel file, modified (all the numbers are multiplied by 2), and written to a new Excel file. Only the data is preserved, without formatting or any properties. Formulas are dropped.
Listing 4: excel-x2.pl
|
It is noteworthy that the data extraction and storage parts of the program are forcibly separated. They could have been done at the same time, but by separating them, bug fixes and improvements can be easily made.
A much better solution to the problem above could be achieved with the XML::Excel
CPAN module, but a special converter from XML back to Excel would have
to be written. You can also use the DBI interface through the DBD::Excel
module, if you want to import data that way. Finally, Spreadsheet::ParseExcel
comes with the
Spreadsheet::ParseExcel::SaveParser
module, which claims to convert between two Excel files but comes with no documentation or examples. My Web site (see Resources) shows an example of using SaveParser
. Be forewarned that this is experimental and highly combustible.
Conclusion
If you are using a Windows machine, stick with the Win32::OLE
modules unless you don't have Excel at all on your machine. Win32::OLE
is the easiest way to get Excel data right now, although the Spreadsheet::WriteExcel
and Spreadsheet::ParseExcel
modules are catching up.
On UNIX, especially Linux, go with the Spreadsheet::WriteExcel
and Spreadsheet::ParseExcel
modules for programmatic access to Excel data. But be forewarned that
these are fairly young modules, and they may not be perfect for you if
you need stability.
You may also consider packages like Gnumeric and StarOffice (see Resources), which are freely available and offer a full GUI interface and import/export capabilities for Excel files. These are useful if you don't need programmatic access to the Excel data. I have used both applications and find them wonderful for day-to-day tasks.
View Cultured Perl: Reading and writing Excel files with Perl Discussion
Page: 1 2 3 Next Page: Resources