Combine CSV files to one spreadsheet

June 13, 2020 - Reading time: 11 minutes

Link --> https://askubuntu.com/questions/1080394/how-to-combine-multiple-csv-files-to-one-ods-file-on-the-command-line-one-sheet

perl code:

With perl:

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
# CPAN modules required:
use Spreadsheet::Write;
use Text::CSV;

my $xlsx_file = shift @ARGV;
$xlsx_file .= ".xlsx" unless $xlsx_file =~ /\.xlsx$/;
my $xlsx = Spreadsheet::Write->new(file => $xlsx_file);
my $csv = Text::CSV->new({binary => 1});

for my $csv_file (@ARGV) {
    my @rows = ();
    open my $fh, "<:encoding(utf8)", $csv_file;
    while (my $row = $csv->getline($fh)) {
        push @rows, $row;
    }
    $csv->eof or $csv->error_diag();
    close $fh;  

    (my $sheet_name = $csv_file) =~ s/\.[^.]+$//;   # strip extension
    $xlsx->addsheet($sheet_name);
    $xlsx->addrows(@rows);
}
$xlsx->close();

And use it like:

/path/to/create_xlsx.pl file.xlsx *.csv

If perl is not your thing, a bit of googling reveals:

LazyCoderOZ

I am a Linux guy, been around for 20+ years using Linux as my daily driver.
This is my blog on my discoveries and notes so I don't forget how I have done things :)