标签:
今天打算用Perl实现以下读取Excel,并将之转化成html的table。
网上搜索了一下,读取Excel的包Spreadsheet::ParseExcel比较好,强大的cpan,让我一条命令就下载安装了它:cpan Spreadsheet::ParseExcel
#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::FmtUnicode; my $parser = Spreadsheet::ParseExcel->new(); my $file = $ARGV[0]; my $formmater = Spreadsheet::ParseExcel::FmtUnicode->new(Unicode_Map => "CP936"); my $workbook = $parser->parse($file, $formmater); if ( !defined $workbook ) { die $parser->error(), ".\n"; } open my $html, ">" ,"table.html" or die; print $html "<table border=1 style=\"white-space:nowrap\">"; for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); for my $row ( $row_min .. $row_max ) { print $html "<tr>"; for my $col ( $col_min .. $col_max ) { print $html "<td>"; my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; print $html $cell->value(); print $html "</td>"; } print $html "</tr>"; } } print $html "</table>"; close $html;
标签:
原文地址:http://www.cnblogs.com/xufeiyang/p/3944914.html