gtag

2010年11月11日 星期四

讓PHP可以匯出EXCEL

使用PEAR的功能,即可讓PHP製作出EXCEL檔:

1.安裝PEAR php-pear.noarch 1:1.4.9-4 FOR CENTOS

[root@dns ~]# yum install php-pear
[root@dns ~]# pear install http://download.pear.php.net/package/OLE-0.6.0.tgz
[root@dns ~]# pear install http://download.pear.php.net/package/Spreadsheet_Excel_Writer-0.9.1.tgz

建立一個PHP檔(test.php)

<?php
// Include PEAR::Spreadsheet_Excel_Writer
require_once "Spreadsheet/Excel/Writer.php";

// Create an instance
$xls =& new Spreadsheet_Excel_Writer();

// Send HTTP headers to tell the browser what's coming
$xls->send("/var/www/html/rpt/xls/test.xls");

// Add a worksheet to the file, returning an object to add data to
$sheet =& $xls->addWorksheet('Binary Count');
$FontSize12 =& $xls->addFormat();
$FontSize12->setFontFamily('新細明體');
$FontSize12->setSize(12);

// Write some numbers
for ( $i=0;$i<11;$i++ ) {
 // Use PHP's decbin() function to convert integer to binary
 $sheet->write($i,0,decbin($i));
 $sheet->writeString($i,1,"書籍代號",$FontSize12);
 $sheet->writeString($i,2,"0001");
 $sheet->write($i,3,"2007/10");
$sheet->writeNumber($i,4,12345,$FontSize12);
}

// Finish the spreadsheet, dumping it to the browser
$xls->close();

?>

程式中的require_once "Spreadsheet/Excel/Writer.php";
實際路徑在/usr/share/pear/Spreadsheet/Excel/Writer.php

另外還有很多EXCEL功能可在下列網站找到:

http://www.pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php

沒有留言:

張貼留言