简体   繁体   中英

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?

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.

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 .

将电子表格导出到csv文件,并在有或没有Text::CSV情况下进行解析。

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) {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM