1.POWINTPOINT系列
之前參與過一個商城的項目,裡面有將excel 導出的功能,但是如果要弄成PPT的我們應該怎麼辦呢?PHP是屬於服務器端的 總不能在裡面裝個Powintpoint吧。於是就有了下面的內容
在國外找到了一個PPT的類庫
github:https://github.com/PHPOffice/PHPPresentation
外國文檔:http://phppresentation.readthedocs.io/en/latest/intro.html
一個簡單的案例分析
<?php use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Style\Alignment; use PhpOffice\PhpPresentation\Style\Bullet; use PhpOffice\PhpPresentation\Style\Color; include_once 'Sample_Header.php'; $colorBlack = new Color( 'FF000000' ); // Create new PHPPresentation object echo date('H:i:s') . ' Create new PHPPresentation object'.EOL; $objPHPPresentation = new PhpPresentation(); // Set properties echo date('H:i:s') . ' Set properties'.EOL; $objPHPPresentation->getDocumentProperties()->setCreator('PHPOffice') ->setLastModifiedBy('PHPPresentation Team') ->setTitle('Sample 02 Title') ->setSubject('Sample 02 Subject') ->setDescription('Sample 02 Description') ->setKeywords('office 2007 openxml libreoffice odt php') ->setCategory('Sample Category'); // Remove first slide echo date('H:i:s') . ' Remove first slide'.EOL; $objPHPPresentation->removeSlideByIndex(0); // Create templated slide echo date('H:i:s') . ' Create templated slide'.EOL; $currentSlide = createTemplatedSlide($objPHPPresentation); // local function // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(200); $shape->setWidth(600); $shape->setOffsetX(10); $shape->setOffsetY(400); $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Introduction to'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor($colorBlack); $shape->createBreak(); $textRun = $shape->createTextRun('PHPPresentation'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(60); $textRun->getFont()->setColor($colorBlack); // Create templated slide echo date('H:i:s') . ' Create templated slide'.EOL; $currentSlide = createTemplatedSlide($objPHPPresentation); // local function // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(50); $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); $textRun = $shape->createTextRun('What is PHPPresentation?'); $textRun->getFont()->setBold(true) ->setSize(48) ->setColor($colorBlack); // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape() ->setHeight(600) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(130); $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT) ->setMarginLeft(25) ->setIndent(-25); $shape->getActiveParagraph()->getFont()->setSize(36) ->setColor($colorBlack); $shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET); $shape->createTextRun('A class library'); $shape->createParagraph()->createTextRun('Written in PHP'); $shape->createParagraph()->createTextRun('Representing a presentation'); $shape->createParagraph()->createTextRun('Supports writing to different file formats'); // Create templated slide echo date('H:i:s') . ' Create templated slide'.EOL; $currentSlide = createTemplatedSlide($objPHPPresentation); // local function // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape() ->setHeight(100) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(50); $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('What\'s the point?'); $textRun->getFont()->setBold(true) ->setSize(48) ->setColor($colorBlack); // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(130); $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT) ->setMarginLeft(25) ->setIndent(-25); $shape->getActiveParagraph()->getFont()->setSize(36) ->setColor($colorBlack); $shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET); $shape->createTextRun('Generate slide decks'); $shape->createParagraph()->getAlignment()->setLevel(1) ->setMarginLeft(75) ->setIndent(-25); $shape->createTextRun('Represent business data'); $shape->createParagraph()->createTextRun('Show a family slide show'); $shape->createParagraph()->createTextRun('...'); $shape->createParagraph()->getAlignment()->setLevel(0) ->setMarginLeft(25) ->setIndent(-25); $shape->createTextRun('Export these to different formats'); $shape->createParagraph()->getAlignment()->setLevel(1) ->setMarginLeft(75) ->setIndent(-25); $shape->createTextRun('PHPPresentation 2007'); $shape->createParagraph()->createTextRun('Serialized'); $shape->createParagraph()->createTextRun('... (more to come) ...'); // Create templated slide echo date('H:i:s') . ' Create templated slide'.EOL; $currentSlide = createTemplatedSlide($objPHPPresentation); // local function // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(50); $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Need more info?'); $textRun->getFont()->setBold(true) ->setSize(48) ->setColor($colorBlack); // Create a shape (text) echo date('H:i:s') . ' Create a shape (rich text)'.EOL; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600) ->setWidth(930) ->setOffsetX(10) ->setOffsetY(130); $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Check the project site on GitHub:'); $textRun->getFont()->setSize(36) ->setColor($colorBlack); $shape->createBreak(); $textRun = $shape->createTextRun('https://github.com/PHPOffice/PHPPresentation/'); $textRun->getFont()->setSize(32) ->setColor($colorBlack); $textRun->getHyperlink()->setUrl('https://github.com/PHPOffice/PHPPresentation/') ->setTooltip('PHPPresentation'); // Save file echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers); if (!CLI) { include_once 'Sample_Footer.php'; }
1.開始引入所有需要的文件
2.創建ppt對象
$objPHPPresentation = new PhpPresentation();
3.創建原型(一些文檔的基本信息)
$objPHPPresentation->getDocumentProperties()->setCreator('PHPOffice') ->setLastModifiedBy('PHPPresentation Team') ->setTitle('Sample 02 Title') ->setSubject('Sample 02 Subject') ->setDescription('Sample 02 Description') ->setKeywords('office 2007 openxml libreoffice odt php') ->setCategory('Sample Category');
4.移除默認的首頁
$objPHPPresentation->removeSlideByIndex(0);
5.創建自定義的頁
$currentSlide = createTemplatedSlide($objPHPPresentation);
6.創建文本域的參數
從四個維度出發 ,寬高,左邊距,右邊距,文本域中的文字表現形式
$shape = $currentSlide->createRichTextShape(); $shape->setHeight(200); $shape->setWidth(900); $shape->setOffsetX(10); $shape->setOffsetY(400); $shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_LEFT );
7.創建文字
$textRun = $shape->createTextRun('What is PHPPresentation?'); $textRun->getFont()->setBold(true) ->setSize(48) ->setColor($colorBlack);
每一頁都是由以上的過程而來的,只要重復上面的步驟就可以了。
注意加入圖片
要new一個gd庫
$shape = new Drawing\Gd(); $shape->setName('Sample image') ->setDescription('Sample image') ->setImageResource($gdImage) ->setRenderingFunction(Drawing\Gd::RENDERING_JPEG) ->setMimeType(Drawing\Gd::MIMETYPE_DEFAULT) ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); $currentSlide->addShape($shape);
至於下載的事情呢?
這個可以把東西都放到一個目錄下面 用正則去弄就可以了
2.EXCEL系列
關於excel的相關操作有兩種可行的辦法
1.利用PHP中的擴展實現
github:https://github.com/iliaal/php_excel
2.利用PHPEXCEL類是的第三方工具類實現
github:https://github.com/faisalman/simple-excel-php
現在我們介紹的是第二種方法
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 = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式 $objWriter->save("xxx.xlsx"); 直接輸出到浏覽器 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Pragma: public"); header("Expires: 0″); header("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()->setTitle("Office 2007 XLSX Test Document"); 題目 $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); 描述 $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes."); 關鍵字 $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php"); 種類 $objPHPExcel->getProperties()->setCategory("Test result file"); ——————————————————————————————————————– 設置當前的sheet $objPHPExcel->setActiveSheetIndex(0); 設置sheet的name $objPHPExcel->getActiveSheet()->setTitle('Simple'); 設置單元格的值 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'String'); $objPHPExcel->getActiveSheet()->setCellValue('A2', 12); $objPHPExcel->getActiveSheet()->setCellValue('A3', true); $objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)'); $objPHPExcel->getActiveSheet()->setCellValue('B8', '=MIN(B2:C5)'); 合並單元格 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22'); 分離單元格 $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); 保護cell $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection! $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel'); 設置格式 // Set cell number formats echo date('H:i:s') . " Set cell number formats\n"; $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ); 設置寬width // Set column widths $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12); 設置font $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('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()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); //垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 設置column的border $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 設置border的color $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300'); 設置填充顏色 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080'); 加圖片 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath('./images/officelogo.jpg'); $objDrawing->setHeight(36); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Paid'); $objDrawing->setDescription('Paid'); $objDrawing->setPath('./images/paid.png'); $objDrawing->setCoordinates('B15'); $objDrawing->setOffsetX(110); $objDrawing->setRotation(25); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(45); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //處理中文輸出問題 需要將字符串轉化為UTF-8編碼,才能正常輸出,否則中文字符將輸出為空白,如下處理: $str = iconv('gb2312', 'utf-8', $str); 或者你可以寫一個函數專門處理中文字符串: function convertUTF8($str) { if(empty($str)) return ''; return iconv('gb2312', 'utf-8', $str); } //從數據庫輸出數據處理方式 從數據庫讀取數據如: $db = new Mysql($dbconfig); $sql = "SELECT * FROM 表名"; $row = $db->GetAll($sql); // $row 為二維數組 $count = count($row); for ($i = 2; $i <= $count+1; $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()->setCellValue('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 object\n"; $objPHPExcel->createSheet(); $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5'); $objWriter-save('php://output');
步驟:
1。創建一個excel
2.保存excel—2007格式
3.直接輸出到浏覽器
4.設置excel的屬性
5.設置當前的sheet
6.選填:
加圖片
7.讀取數據庫(選填)
更多請看github上面的demo
3.WORD系列
1.word的創建
本來想要寫的但是發現博客園的一哥們寫得很全很不錯:http://www.cnblogs.com/phphuaibei/archive/2011/11/30/2269427.html
2.word的導出
不使用插件的話一般有兩種方法:‘
1.利用mht
<?php //中文編碼轉換 function utf8_unicode($c) { switch(strlen($c)) { case 1: return ord($c); case 2: $n = (ord($c[0]) & 0x3f) << 6; $n += ord($c[1]) & 0x3f; return $n; case 3: $n = (ord($c[0]) & 0x1f) << 12; $n += (ord($c[1]) & 0x3f) << 6; $n += ord($c[2]) & 0x3f; return $n; case 4: $n = (ord($c[0]) & 0x0f) << 18; $n += (ord($c[1]) & 0x3f) << 12; $n += (ord($c[2]) & 0x3f) << 6; $n += ord($c[3]) & 0x3f; return $n; } } function cn2mht($str) { preg_match_all("/[\x80-\xff]?./",$str,$ar); foreach($ar[0] as $v) $ret.="&#".utf8_unicode(iconv("GB2312","UTF-8",$v)).";"; return $ret; } ob_start();//打開輸出緩沖區 header("content-type:application/doc"); header("content-disposition:attachment;filename={$YEAR}年度考核登記表.doc"); $template="template.mht"; $contents=file_get_contents($template); //替換 $contents=str_replace("{{year}}",$YEAR,$contents); $contents=str_replace("{{name}}",cn2mht($NAME),$contents); //換行符替換:\n-><br>,<br>-><br> $ITEM_DESC = cn2mht(str_replace( "\n", "<br>", $ITEM_DESC )); $ITEM_DESC = str_replace( "<br>", "<br>", $ITEM_DESC ); echo $contents;
2.建立一個緩沖區,將內容輸出到緩沖區中,再賦予新建的文件中
<?php class word { function start() //定義要保存數據的開始 { ob_start(); //開始輸出緩沖 //設置生成word的格式 print '<html xmlns="urn:schemas-microsoft-comfficeffice" xmlns:w="urn:schemas-microsoft-comffice:word" xmlns="http://www.w3.org/TR/REC-html40">'; } function save($path) //定義要保存數據的結束,同時把數據保存到word中 //所要保存的數據必須限定在該類的start()和save()之間 { print "</html>"; $data=ob_get_contents(); //返回內部緩沖的內容 即把輸出變成字符串 ob_end_clean(); //結束輸出緩沖,清潔(擦除)輸出緩沖區並關閉輸出緩沖 $this->wirtetoword($path,$data); } function wirtetoword($fn,$data) //將數據已二進制的形式保存到word中 { $fp=fopen($fn,"wb"); fwrite($fp,$data); fclose($fp); } } if(@$_GET[id]!='') { $word=new word();//示例化對象 $word->start();//定義要保存數據的開始 } echo "<font color=\"red\">out to word</font>"; if(@$_GET[id]!='') { $word->save('word.doc');//定義要保存數據的結束,同時把數據保存到word中 } if(@$_GET[id]=='') { //超鏈接中的x僅僅是為了傳一個值,確認下載,沒有其他的實際意義 ?> <a href="javascript:;"><div onclick="window.location.href = window.location.href+'?id=x'">點擊跳到下載頁面</div></a> <?php }else{ echo "<a href=\"word.doc\">下載</a>"; }