CentOS7にLibXLをインストールしてPHP5.4からExcelを出力する
LibXLをダウンロードする
ここからでも入手できます
http://www.libxl.com/
# cd /usr/local/src
# wget http://libxl.com/download/libxl-lin-3.6.4.tar.gz
# tar -zxvf libxl-lin-3.6.4.tar.gz
php_excelをインストールする
libxml2
がインストールされていない場合はyumでインストールする
yum install libxml2-devel
# wget https://github.com/iliaal/php_excel/archive/master.zip
# unzip master.zip
# cd php_excel-master
# phpize
# ./configure --with-libxl-incdir=../libxl-3.6.4.0/include_c --with-libxl-libdir=../libxl-3.6.4.0/lib64 --with-libxml-dir=/usr/include/libxml2
# make
# make install
インストールされたモジュールをPHPに組み込む
/etc/php.d/excel.ini
extension=excel.so
;excel.license_name=LICENSE_NAME
;excel.license_key=LICENSE_KEY
Apacheを再起動して有効になっているかを確認する
# systemctl restart httpd
# php -i | grep excel
excel support => enabled
PHPからExcelを出力する
<?php
$excel = new ExcelBook(null, null, true);
$sheet = $excel->addSheet('sheet1');
for ($row = 0; $row < 100; $row++) {
for ($col = 0; $col < 100; $col++) {
$sheet->write($row, $col, time());
}
}
header('Content-Type: application/octet-stream');
header('Content-Disposition:attachment;filename="sample.xlsx"');
$excel->save('php://output');