在 Linux 上将 CSV 转换为 XLS 文件

mai*_*ash 14 linux bash awk perl shell-script

以下 Perl 脚本可以将 CSV 文件转换为 XLS 文件

问题是我需要在客户 Linux 机器上安装许多 Perl 模块

为了运行这个 Perl 脚本,实际上我不能,因为 Linux 机器是客户机器(不允许安装模块)

所以我需要为这个 Perl 脚本找到一些其他的替代方案

第一个客户有 Linux red-hat 机器版本 5.X

我想找到一些bash/ksh/sh/awk脚本,它们可以像 perl 脚本一样完成这项工作

所以我想找到其他将 CSV 转换为 XLS 文件的替代方法

请建议如何找到这个脚本?或其他建议在 Linux 机器上将 CSV 转换为 XLS

#!/usr/bin/perl -w

###############################################################################
#
# Example of how to use the WriteExcel module
#
# Simple program to convert a CSV comma-separated value file to an Excel file.
# This is more or less an non-op since Excel can read CSV files.
# The program uses Text::CSV_XS to parse the CSV.
#
# Usage: csv2xls.pl file.csv newfile.xls
#
#
# NOTE: This is only a simple conversion utility for illustrative purposes.
# For converting a CSV or Tab separated or any other type of delimited
# text file to Excel I recommend the more rigorous csv2xls program that is
# part of H.Merijn Brand's Text::CSV_XS module distro.
#
# See the examples/csv2xls link here:
#     L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
#
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
#

use strict;
use Spreadsheet::WriteExcel;
use Text::CSV_XS;

# Check for valid number of arguments
if ( ( $#ARGV < 1 ) || ( $#ARGV > 2 ) ) {
    die("Usage: csv2xls csvfile.txt newfile.xls\n");
}

# Open the Comma Separated Variable file
open( CSVFILE, $ARGV[0] ) or die "$ARGV[0]: $!";

# Create a new Excel workbook
my $workbook  = Spreadsheet::WriteExcel->new( $ARGV[1] );
my $worksheet = $workbook->add_worksheet();

# Create a new CSV parsing object
my $csv = Text::CSV_XS->new;

# Row and column are zero indexed
my $row = 0;

while (<CSVFILE>) {
    if ( $csv->parse($_) ) {
        my @Fld = $csv->fields;

        my $col = 0;
        foreach my $token (@Fld) {
            $worksheet->write( $row, $col, $token );
            $col++;
        }
        $row++;
    } else {
        my $err = $csv->error_input;
        print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

max*_*zig 30

要将 CSV 文件自动转换为 XLS/XLSX 文件,您还可以使用ssconvert(随 Gnumeric 一起提供)或unoconv(使用 LibreOffice)。

SSConvert 示例

$ echo -e 'surname,name,age\nCarlo,Smith,23\nJohn,Doe,46\nJane,Doe,69\nSarah,Meyer,23\n' \
     > example.csv
$ unix2dos example.csv
$ ssconvert example.csv example.xlsx
$ ssconvert example.csv example.xls
Run Code Online (Sandbox Code Playgroud)

第一个ssconvert调用创建一个 MS Excel 2007/2010 文件,第二个调用创建一个老式 Excel 2007 文件。

您可以通过file以下方式检查文件:

$ file example.csv
example.csv: ASCII text, with CRLF line terminators
$ file example.xls
example.xls: Composite Document File V2 Document, Little Endian, Os: Windows, Version 4.10,
   Code page: 1252, Create Time/Date: Tue Sep 30 20:23:18 2014
$ file example.xlsx 
example.xlsx: Microsoft Excel 2007+
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式列出所有支持的输出文件格式:

$ ssconvert --list-exporters
ID                           | Description
[..]
Gnumeric_Excel:xlsx2         | ISO/IEC 29500:2008 & ECMA 376 2nd edition (2008);
                               [MS Excel™ 2010]
Gnumeric_Excel:xlsx          | ECMA 376 1st edition (2006); [MS Excel™ 2007]
Gnumeric_Excel:excel_dsf     | MS Excel™ 97/2000/XP & 5.0/95
Gnumeric_Excel:excel_biff7   | MS Excel™ 5.0/95
Gnumeric_Excel:excel_biff8   | MS Excel™ 97/2000/XP
[..]
Run Code Online (Sandbox Code Playgroud)

Unoconv 示例

$ unoconv --format  xls example.csv
Run Code Online (Sandbox Code Playgroud)

这将创建 example.xls,这是一个 Excel 97/2000/XP 文件。

通过文件检查:

$ file example.xls 
example.xls: Composite Document File V2 Document, Little Endian, Os: Windows, Version 1.0,
  Code page: -535, Revision Number: 0
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式列出所有支持的文件格式:

$ unoconv --show
[..]
The following list of spreadsheet formats are currently available:

  csv      - Text CSV [.csv]
  dbf      - dBASE [.dbf]
[..]
  ooxml    - Microsoft Excel 2003 XML [.xml]
[..]
  xls      - Microsoft Excel 97/2000/XP [.xls]
  xls5     - Microsoft Excel 5.0 [.xls]
  xls95    - Microsoft Excel 95 [.xls]
[..]
Run Code Online (Sandbox Code Playgroud)

  • 这个:`soffice --convert-to xlsx:"Calc MS Excel 2007 XML" filename.csv --headless` 也非常有用。 (2认同)