标签:icon 过程 encoding 服务器端 imp tco setimage tps images
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>"; }
标签:icon 过程 encoding 服务器端 imp tco setimage tps images
原文地址:http://www.cnblogs.com/kengdiexienima/p/6194411.html