isJustMe
isJustMe

Reputation: 5470

how to read an excel file with Perl?

The Spreadsheet::ParseExcel does the work fine, however I need a method to read a file without it, lets say wih "out of the box Perl" as I'm unable to install any PM or CPAN module. Does anyone has a suggestion to get me started?

Upvotes: 1

Views: 1552

Answers (4)

Drew Taylor
Drew Taylor

Reputation: 524

I'll build on the answer above from @mob regarding Text::CSV. A while back I found Text:CSV::Slurp on CPAN and was an instant convert. It takes a CSV file with header rows and returns an arrayref of hashrefs where the keys are the names from the header rows. Obviously this won't work in all cases, but if it does your code is simple:

my $slurp = Text::CSV::Slurp->new;
my $data = $slurp->load(file => $filename);
for my $record (@$data) {
    ...
}

Upvotes: 0

jmcnamara
jmcnamara

Reputation: 41554

What is a relatively easy task using CPAN modules is actually very difficult without them.

For a start the Excel binary data (BIFF) is stored in another binary file format called an OLE compound document. This is like a file system within a file and the BIFF data might not be stored sequentially. So to start you would have to write a parser to get the data out.

Once the raw BIFF data is extracted you have to parse it to find cell data. That is a little easier but still contains difficulties such as the strings being stored in a hash table away from the cell data. And dates that are indistinguishable from plain numbers. And data in merged cells. And everything is still in binary and bitmasks control the meaning of data structures.

Fortunately all these headaches have been suffered by someone else* and wrapped up in a module so no-one else has to endure them.

So, even if your admins won't install modules for you there are lots of ways to install modules or even install perl locally so that you don't have to bother them. In the end that will probably be an easier solution.

* Me partially.

Upvotes: 5

mob
mob

Reputation: 118595

Export the spreadsheet to a csv file and parse it with or without Text::CSV.

Upvotes: 0

Quentin
Quentin

Reputation: 943217

OpenDocument is an ISO standard so you could read the specification and write your own parser for it.

CPAN modules exist because there are things that lots of things (some simple, some complex) that people want to do that are inappropriate to be part of the core language. Parsing Excel spreadsheets is one of these (one of the more complex ones).

You should fix whatever barrier is preventing you from installing a module to help. It may be managerial (in which case you need to lobby to get the policy changed), it may be technical (in which case you may just need to learn about local::lib.

Upvotes: 2

Related Questions