【问题标题】:How to convert .xls file to .csv file?如何将 .xls 文件转换为 .csv 文件?
【发布时间】:2014-03-13 08:41:59
【问题描述】:

如何在 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 行。

【问题讨论】:

标签: perl csv xls


【解决方案1】:

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

Here is the link

and this link

【讨论】:

    【解决方案2】:

    您的代码中有拼写错误。

    open(my $FH ,'>',"$csv") or die "oops!";
    
    while (my $line = <$FH>){
        print $FH $line;
    }
    

    【讨论】:

    • 我在 csv.pl 第 26 行收到 Not a GLOB reference。这个错误
    • 修正了变量名,请重试。
    • 我认为是错误的open(my $FH ,'&gt;',"$csv") or die "oops!"; while (my $line = &lt;$FH&gt;){
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多