Excel Dateien unter Unix mit perl(Spreadsheet::WriteExcel) erstellen

Das CPAN Modul perl(Spreadsheet::WriteExcel) ermöglicht es, direkt von der Befehlszeile Excel-Dateien zu erstellen.

RPM Pakete von Dag Wieers

Beispiele

csv-Datei in ein excel-Sheet überführen

create_excel.pl

#!/usr/bin/perl

use Spreadsheet::WriteExcel;

# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new("auswertung.xls");

# Add a worksheet
$worksheet = $workbook->add_worksheet('auswertung');

$col = $row = 0;

$col = $row = 0;
while($line=<STDIN>)    {
        @data = split("\t",$line);
        $worksheet->write_row($row,0,\@data);
        $row++;
}

$workbook->close;

Das Programm ist mittels cat filename.csv | create_excel.pl aufzurufen.

UP

write_row($row, $column, $array_ref, $format)

The write_row() method can be used to write a 1D or 2D array of data in one go. This is useful for converting the results of a database query into an Excel worksheet. You must pass a reference to the array of data rather than the array itself. The write() method is then called for each element of the data. For example:

example1

    @array      = ('awk', 'gawk', 'mawk');
    $array_ref  = \@array;

    $worksheet->write_row(0, 0, $array_ref);

    # The above example is equivalent to:
    $worksheet->write(0, 0, $array[0]);
    $worksheet->write(0, 1, $array[1]);
    $worksheet->write(0, 2, $array[2]);

Note: For convenience the write() method behaves in the same way as write_row() if it is passed an array reference. Therefore the following two method calls are equivalent:

    $worksheet->write_row('A1', $array_ref); # Write a row of data
    $worksheet->write(    'A1', $array_ref); # Same thing

As with all of the write methods the $format parameter is optional. If a format is specified it is applied to all the elements of the data array.

Array references within the data will be treated as columns. This allows you to write 2D arrays of data in one go. For example:

2D-Array

    @eec =  (
                ['maggie', 'milly', 'molly', 'may'  ],
                [13,       14,      15,      16     ],
                ['shell',  'star',  'crab',  'stone']
            );

    $worksheet->write_row('A1', \@eec);

Would produce a worksheet as follows:

     -----------------------------------------------------------
    |   |    A    |    B    |    C    |    D    |    E    | ...
     -----------------------------------------------------------
    | 1 | maggie  | 13      | shell   | ...     |  ...    | ...
    | 2 | milly   | 14      | star    | ...     |  ...    | ...
    | 3 | molly   | 15      | crab    | ...     |  ...    | ...
    | 4 | may     | 16      | stone   | ...     |  ...    | ...
    | 5 | ...     | ...     | ...     | ...     |  ...    | ...
    | 6 | ...     | ...     | ...     | ...     |  ...    | ...

To write the data in a row-column order refer to the write_col() method below.

UP

 
wissen/tips_und_tricks/perl_excel_dateien_erstellen_mit_spreadsheet_writeexcel.txt · Zuletzt geändert: 06.09.2010 02:31
 
Recent changes RSS feed Donate Valid XHTML 1.0 Valid CSS Recent cached RSS feed cacert-signed web site: inhalt.serviert.de