<?php include_once "header.php"; require_once 'class/PHPExcel.php'; //引入 PHPExcel 物件庫 require_once 'class/PHPExcel/IOFactory.php'; //引入 PHPExcel_IOFactory 物件庫 $objPHPExcel = new PHPExcel(); //實體化Excel //----------內容-----------// header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename=通訊錄.xls'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->setPreCalculateFormulas(false); $objWriter->save('php://output'); exit; ?>
$objPHPExcel->setActiveSheetIndex(0); //設定預設顯示的工作表 $objActSheet = $objPHPExcel->getActiveSheet(); //指定預設工作表為 $objActSheet $objActSheet->setTitle("通訊錄"); //設定標題 $objPHPExcel->createSheet(); //建立新的工作表,上面那三行再來一次,編號要改
$objActSheet->setCellValue("A1", '姓名')->setCellValue("B1", '電話'); $objActSheet->setCellValueExplicit("C2", '0987654321',PHPExcel_Cell_DataType:: TYPE_STRING);
$objActSheet->getColumnDimension('A')->setWidth(8); //固定寬度8 $objActSheet->getColumnDimension('B')->setAutoSize(true); //自動寬度
$objPHPExcel->getDefaultStyle()->getFont()->setName('標楷體'); $objPHPExcel->getDefaultStyle()->getFont()->setSize(16); $objActSheet->getStyle('A1:E1')->getFill()->setFillType(PHPExcel_Style_Fill:: FILL_SOLID)->getStartColor()->setARGB('FFFFCC99');
$objActSheet->mergeCells("A10:D10")->setCellValue("A10", '資料數共計');
$n=$i-1; $objActSheet->setCellValue("E{$i}", "=COUNTA(E2:E{$n})");
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); $objPHPExcel->getActiveSheet()->protectCells("E{$i}", 'password');