以下是總結的幾個使用方法
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//或include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的
建立一個excel
$objPHPExcel = new PHPExcel();
保存excel—2007格式
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter ->save("xxx.xlsx");
直接輸出到瀏覽器
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
header("Pragma: public");
header("Expires: 0″); ("Cache-Control:must-revalidate, post-check=0, pre-check=0″);
header("Content-Type:application/force-download");
header("Content-Type:application/ vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename ="resume.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
———————————— ————————————————————————————–
設定excel的屬性:
創建人
$objPHPExcel->getProperties()->setCreator( "Maarten Balliauw");
最後修改人
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
標題
$objPHPExcel->getProperties()->
標題
$objPHPExcel->getProperties()->
標題
$objPHPExcel->getProperties()->
標題;
題目
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
描述
$objPHPExcel->getProperties()->setDescription描述
$objPHPExcel->getProperties()->setDescription 描述
$objPHPExcel->getProperties()->setDescription 描述
$objPHPExcel->getProperties()->setDescription("Tests document for SX. .");
關鍵字
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
種類
$objPHPExcel->getProperties()->setCategory($objPHPExcel->getProperties()->setCategory( result PHPExcel->getProperties()->setCategory( ———————————————————————————————————–
設定目前的sheet
$objPHPExcel->setActiveSheetIndex( 0);
設定sheet的name
$objPHPExcel->getActiveSheet()->setTitle('Simple');
設定單元格的值
$objPHPExcel->getActiveSheet()->setCellValue('A1', ''A1', ''A1', ''A1', ''A1', ''A1', ''A1', ''A1', ''A1', ''A1', '' );
$objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
$objPHPExcel->getActiveSheet()->setCellValue('A3', true);
$objPHPHPExcel->getActive( setCellValue('C5', '=SUM(C2:C4)');
保護單元
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // 需要設定為 true 才能啟用任何工作表保護!
$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
設定格式
//設定儲存格數字格式
回顯日期('H:i:s')。 " 設定單元格數字格式n";
$objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURREN_EUR_SIMPLE);EUR_objet; duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
設定寬
//設定列寬
$objPHPExcel->getActiveSheet()->Sheet()- )- >setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
設定字體
$objPHPExcel->getActiveSheet()-getStyle($objPHPExcel->getActiveSheet()- -> getFont()->setName('Candara');
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20);
$objPHPExcel->getActiveSheet() -> getStyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcelUNDERLINEtyle_Font::::ERER_SINGLE) ;
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
$objPHPExcel->get-Set( E1' )->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true) ;
$objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true);
設定align
$objPHPExcel->getActiveSheet()->getStyle('D11')-> getAlignment( )->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->AlzoNHOSC四處_D12')。
$objPHPExcel->getActiveSheet() ->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPcel-8 ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment: :HORIZONTAL_JUSTIFY);
//垂直居中
$objPHPExcel->getActiveSheet()->getStyle($objPHPExcel->getActiveSheet()->getStyle('A18')-AgetpignmentP. 。 getActiveSheet()->getStyle('B4')->getBorders ()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcelorder->getActiveSheet()-getStyle( ()->getTop()->setBorderStyle(PHPExcel_Style_Border:: BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->PfScelle( BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle ('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); ()->getStyle('D13')->getBorders( )->getLeft()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getStyle('D13') ->getBorders()->getTop()->getColor( )->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->Sheet() ->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB(' FF993300');
$objPHPExcel->getActiveSheet()-> getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); ()->getStyle('E13')->getBorders( )->getRight()->getColor()->setARGB('FF993300');
設定填滿顏色
$objPHPExcel->getActiveSheet()->getStyle()->getStyle( 'A1')->getFill()->setFillType(PHPExcel_Style_Fill) ::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartARor()->setAR( 'FF808080');
$objPHPExcel->getActiveSheet()-> getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objgetPExpcel> ')->getFill()->getStartColor()->setARGB ('FF808080');
加圖片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo'); setDescription('Logo');
$objDrawing->setPath(' ./images/officelogo.jpg');
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($objPHPPcel->getActive) ;
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing-> setName('Paid');
$objDrawing->setDescription('Pid');
>objDrawing->objDrawing-cpad.//M );
$objDrawing->setCooperatives('B15');
$objDrawing->setOffsetX(110);
$objDrawing->setRotation(25);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
$objDrawing->setWorksWorksheet(objPHakPExcel. getActiveSheet());
//處理中文輸出問題
需要將字串轉換為UTF-8編碼,才能正常輸出,否則中文字元將輸出為空白,如下處理:
$str = iconv('gb2312', ' utf-8', $str);
或你可以寫一個函數專門處理中文字串:
function convertUTF8($str)
{
if(empty($str)) return '';
return iconv2'gbstr)) return '';
return iconv2'gb233331 ', 'utf-8', $str);
}
//從資料庫輸出資料處理方式
從資料庫讀取資料如:
$db = new Mysql($dbconfig);
$sql = "SELECT * FROM表名";
$row = $db->GetAll($sql); // $row 為二維數組
$count = count($row);for ($i = 2; $i $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, convertUTF8($row[$i-2][1]));
$objPHPExcel->getActiveSheet() ->setCellValue('B' . $i, convertUTF8($row[$i-2][2]));
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, convertUTF8($row [$i-2][3]));
$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, convertUTF8($row[$i-2][4]));
$objPHPExcel ->getActiveSheet()->setCellValue('E' . $i, convertUTF8(date("Y-m-d", $row[$i-2][5])));
$objPHPExcel->getActiveSheet()->setCellValueValue ('F' . $i, convertUTF8($row[$i-2][6]));
$objPHPExcel->getActiveSheet()->setCellValue('G' . $i, convertUTF8($row[$i -2][7]));
$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, convertUTF8($row[$i-2][8]));
}
在預設sheet後,建立一個worksheet
echo date('H:i:s') . " Create new Worksheet objectn";
$objPHPExcel->createSheet();
$objWritercel = PHPExcel_IOy::createSheet();
$objWriter-save('php://output');