學無止境,教學相長

phpexcel

[ PHP ] – 利用PHPEXCEL 產生EXCEL 檔

Spread the love

利用PHPEXCEL 產生EXCEL 檔

<?php
include('Classes/PHPExcel.php');
require_once('Classes/PHPExcel/IOFactory.php');
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->setActiveSheetIndex(0);

$sql = "SELECT * FROM `XXX` ";

$i=2;
$no=0;
foreach ( $db->query($sql) as $row){

$no+=1;
$objPHPExcel->getActiveSheet()->setTitle('TAB標題');
$objPHPExcel->getActiveSheet()->setCellValue('A1','流水號'); 
$objPHPExcel->getActiveSheet()->setCellValue('B1','欄位一'); 
$objPHPExcel->getActiveSheet()->setCellValue('C1','欄位二'); 
$objPHPExcel->getActiveSheet()->setCellValue('D1','欄位三'); 
$objPHPExcel->getActiveSheet()->setCellValue('E1','欄位四');  
$objPHPExcel->getActiveSheet()->setCellValue('F1','欄位五');  

$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$no);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$row['欄位一']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i,$row['欄位二']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i,$row['欄位三']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i,$row['欄位四']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$i,$row['欄位五']); 

$i+=1;
}


	
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save('export/export_abc.xlsx');//
header('Location:export/export_abc.xlsx');
exit;
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
如果我的文章有幫助到您的話,請幫我點擊廣告,一天最少需有五人點擊本站才有足夠經費繼續經營下去,感謝! ⇩⇩⇩ 如果我的文章有幫助到您的話,請幫我點擊廣告,一天最少需有五人點擊本站才有足夠經費繼續經營下去,感謝! ⇧⇧⇧

發佈留言