Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert .xls file to .csv file?

Tags:

csv

perl

xls

How to convert .xls to .csv in perl? what is the module for this? Is there any example for this? what is the best way to convert?

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;
}

oops! at csv.pl line 23.

like image 681
Ram Avatar asked Nov 30 '25 03:11

Ram


2 Answers

Use a perl script. Using the Spreadsheet::ParseExcel perl module from CPAN to parse the xls file followed by output as csv should work fine.

Here is the link

and this link

like image 90
Bug Avatar answered Dec 02 '25 18:12

Bug


You have typos in your code.

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

while (my $line = <$FH>){
    print $FH $line;
}
like image 43
user1126070 Avatar answered Dec 02 '25 20:12

user1126070



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!