如何将.xls文件转换为.csv文件?

时间:2014-02-14 05:45:06

标签: perl csv xls

如何在perl中将.xls转换为.csv?这是什么模块?这有什么例子吗? 什么是转换的最佳方式?

use Spreadsheet::ParseExcel;
my $xlsparser = Spreadsheet::ParseExcel->new();
my $xlsbook = $xlsparser->parse('/home/Admin/Downloads/abc.xls');
my $xls = $xlsbook->worksheet(0);
my ( $row_first, $row_last ) = $xls->row_range();
my ( $col_first, $col_last ) = $xls->col_range();
my $csv = '/home/Admin/Downloads/ram.csv';
for my $row ( $row_first .. $row_last ) {        # Step through each row
    for my $col ( $col_first .. $col_last ) {    # Step through each column
        my $cell = $xls->get_cell( $row, $col ); # Get the current cell
        next unless $cell;
        $csv .= $cell->unformatted(); # Get the cell's raw data -- no border 
                                      # colors or anything like that
        if ($col == $col_last) {
            $csv .= "\n"; 
        } else {
            $csv .= ","; 
        }
    }
}
open(my$FH ,'>',"$csv") or die "oops!";

while (my$line = <$xlsbook>){
    print $FH $line;
}

糟糕!在csv.pl第23行。

2 个答案:

答案 0 :(得分:1)

使用perl脚本。使用CPAN中的Spreadsheet :: ParseExcel perl模块解析xls文件,然后输出为csv应该可以正常工作。

Here is the link

and this link

答案 1 :(得分:1)

您的代码中存在拼写错误。

open(my $FH ,'>',"$csv") or die "oops!";

while (my $line = <$FH>){
    print $FH $line;
}