[ PHP ] – 利用PHPEXCEL 產生EXCEL 檔

利用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" />

你可能會喜歡

喜歡我的文章嗎?歡迎按讚加分享,
或是點擊站內廣告讚助我喝一杯咖啡吧 - \(^_^)/

建立一個對話

你的電子郵件位址並不會被公開。 必要欄位標記為 *