วันอาทิตย์ที่ 25 พฤศจิกายน พ.ศ. 2555

php excel class

<?php
$a = array('A','B','C');
$fs = array('field1','field2','field3');
$cs = array(
    array('aaa1','bbb1','ccc1ทดสอบ'),
    array('aaa2','bbb2','ccc2'),
    array('aaa3','bbb3','ccc3'),
);
require_once './excel/PHPExcel.php';
$e = new PHPExcel();
$e->getProperties()->setCreator("num")
        ->setLastModifiedBy("num")
        ->setTitle("Report")
        ->setSubject("Report")
        ->setDescription("Report")
        ->setKeywords("report")
        ->setCategory("report");

$default_border = array(
    'style' => PHPExcel_Style_Border::BORDER_THIN,
    'color' => array('rgb'=>'1006A3')
);
$style_header = array(
    'borders' => array(
        'bottom' => $default_border,
        'left' => $default_border,
        'top' => $default_border,
        'right' => $default_border,
    ),
    'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'color' => array('rgb'=>'E1E0F7'),
    ),
    'font' => array(
        'bold' => true,
        'name' => 'CordiaUPC',
        'size' => 30,
    )
);
$sh = $e->setActiveSheetIndex(0);
$sh->getStyle('A1:C4')->applyFromArray( $style_header );
$sh->getColumnDimension('C')->setWidth(20);
foreach($fs as $i=>$f)
        $sh->setCellValue($a[$i].'1', $f);
$row = 2;
foreach($cs as $c){
        $vals = array_values($c);
        foreach($fs as $i=>$f)
                $sh->setCellValue($a[$i].$row, $vals[$i]);
        $row++;
}
$e->getActiveSheet()->setTitle('Report');
$e->setActiveSheetIndex(0);
$writer = PHPExcel_IOFactory::createWriter($e, 'Excel5');
$writer->save('test.xls');
?>



PHPExcel.php ดาวนโหลดได้ที่ http://phpexcel.codeplex.com/