Excel-Writer-XLSX はPerl製のソフトウェアです。ブックやワークシートの生成、印刷設定、書式設定等、Excelで行う作業をコーディングで行うことができます。例えば、

  • 定期的に定型の様式で Excel を用いて資料を作成している。
  • データベースから検索結果を得て、Excelで資料を作成している。

な感じにExcelを利用して手作業で資料を作成している方々に便利です。

Excelには数式やマクロといった便利な機能がありますが、このソフトウェアを利用することで自動的に資料を作成できるようになります。業務システムの帳票を電子化する場合にも役立つでしょう。

以下はコーディングの例です。

use Excel::Writer::XLSX;

# Create a new Excel workbook
my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

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

#  Add and define a format
$format = $workbook->add_format();
$format->set_bold();
$format->set_color( 'red' );
$format->set_align( 'center' );

# Write a formatted and unformatted string, row and column notation.
$col = $row = 0;
$worksheet->write( $row, $col, 'Hi Excel!', $format );
$worksheet->write( 1, $col, 'Hi Excel!' );

# Write a number and a formula using A1 notation
$worksheet->write( 'A3', 1.2345 );
$worksheet->write( 'A4', '=SIN(PI()/4)' );

プログラム終了時に、自動的にブックを閉じる仕様となっています。プログラム内で、同じブックを再び開く場合には、明示的にブックを閉じる必要があります。

$workbook = close();

主な特徴について

1) メソッドは、ブック、ワークシート、印刷設定、書式設定に関して用意されています。

主なメソッドは次の通りです。

WORKBOOK METHODS

    new()
    add_worksheet( $sheetname )
    add_format( %properties )
    ...

WORKSHEET METHODS

    write( $row, $column, $token, $format )
    write_number( $row, $column, $number, $format )
    write_string( $row, $column, $string, $format )
    ...

PAGE SET-UP METHODS

    set_landscape()
    set_portrait()
    set_page_view()
    set_paper( $index )
    ...

FORMAT METHODS

    set_format_properties( %properties )
    set_font( $fontname )
    set_size()
    set_color()
    ...

2) ブックのプロパティを設定できます。

ブックを作成する度に、手作業でプロパティを設定することは大変わずらわしいものです。コーディングにより、入力間違いを起こすことなく楽に設定することができます。

$workbook->set_properties(
    title    => 'This is an example spreadsheet',
    author   => 'John McNamara',
    comments => 'Created with Perl and Excel::Writer::XLSX',
);

3) 独自の色を定義できます。

手作業では Excel の GUI で設定しますが、既に色の構成が分かっている場合には便利な機能です。CSSに慣れている方で、色の定義を流用したい場面でも役立つでしょう。

$workbook->set_custom_color( 40, 255,  102,  0 );       # Orange
$workbook->set_custom_color( 40, 0xFF, 0x66, 0x00 );    # Same thing
$workbook->set_custom_color( 40, '#FF6600' );           # Same thing

my $font = $workbook->add_format( color => 40 );        # Modified colour

Excel-Writer-XLSX のインストール

sudo cpanm Excel::Writer::XLSX でインストールできます。

今後の予定によれば、既存のブックに対する更新が可能になるようです。印刷設定や書式をプログラムで設定するとそれなりに時間がかかりますが、既存のブックをテンプレートとして利用した場合は、この工数を削減できます。資料のデザインは Excel で行い、値の設定をプログラムで行えるようになりますので、デザインを分離した設計と作成が可能になるでしょう。

TO DO

The roadmap is as follows:

    New separated data/formatting API to allow cells to be formatted after data is added.
    More charting features.
    Excel::Reader::XLSX and Excel::Rewriter::XLSX. Hopefully.
    Pivot tables, maybe.

Excel-Writer-XLSX はPerl製、GPLとArtistic Licenseのオープンソース・ソフトウェアです。

Excel::Writer::XLSX - Create a new file in the Excel 2007+ XLSX format.

source code