Rumah >php教程 >php手册 >一个导出类分享给大家,有更好的也可以讨论

一个导出类分享给大家,有更好的也可以讨论

WBOY
WBOYasal
2016-06-07 11:37:34889semak imbas

php导出类。支持csv和phpexcel,以前写的
<?php <br /> <br> namespace Common\Event;<br> use Common\Controller\CommonController;<br>     /**<br>      * 数据导出类 用于导出数据<br>      * @author  Jack<br>      * @time    20150113<br>      */<br>     class DataPutEvent extends CommonController{<br>         public function _initialize(){<br>             parent::_initialize();<br>             C('SHOW_PAGE_TRACE',false);<br>         }<br>         /***<br>         *测试导出方法,必须放到Controller运行<br>         function test(){<br>             $head = array('哈哈','呵呵','嘿嘿','嘎嘎','赫赫','嘻嘻',);<br>             $data = array(<br>                 '0'=>array(<br>                     'aa'=>1,<br>                     'bb'=>1,<br>                     'cc'=>1,<br>                     'dd'=>1,<br>                     'ee'=>1,<br>                     'ff'=>1,<br>                 ),<br>                 '1'=>array(<br>                     'aa'=>1,<br>                     'bb'=>2,<br>                     'cc'=>1,<br>                     'dd'=>74,<br>                     'ee'=>1,<br>                     'ff'=>52,<br>                 ),<br>                 '2'=>array(<br>                     'aa'=>17,<br>                     'bb'=>257,<br>                     'cc'=>157,<br>                     'dd'=>747,<br>                     'ee'=>1527,<br>                     'ff'=>5275,<br>                 )<br>             );<br>             $event = A('Common/DataPut','Event');<br>             //$result = $event->csvPut($data,$head);<br>             //$result = $event->csvPut($data);<br>             //$result = $event->phpexcelPut($data,$head);<br>             //$result = $event->phpexcelPut($data);<br>         }<br>         ****/<br>         <br>         <br>         /**<br>          * csv数据格式导出<br>          * @author  Jack<br>          * @param     Array     $head     Excel列名信息 <br>           * @param     Array     $data     Excel数据 二维数组<br>          * @param     String     $filename     Excel文件名<br>          * @return  Bool<br>          **/<br>         public function csvPut($data=array(), $head=array(), $filename=''){<br> <br>             if(!$data) return false;<br>             $array_keys = array_keys($data[0]); //数据数组字段名称<br>             $head = $head ? $head : $array_keys;<br>             $filename = $filename ? $filename : (date('YmdHis').'导出记录.csv');<br>             // 输出Excel文件头<br>             header("Content-Type: application/vnd.ms-excel");<br>             header("Content-Disposition: attachment;filename={$filename}");<br>             header("Cache-Control: max-age=0");<br>             <br>             // 打开PHP文件句柄,php://output 表示直接输出到浏览器<br>             $fp = fopen('php://output', 'a');<br>             if(!$fp) return false;<br>             <br>             // 输出Excel列名信息        <br>             foreach ($head as $i => $v) {<br>                 // CSV的Excel支持GBK编码,一定要转换,否则乱码<br>                 $head[$i] = iconv('utf-8', 'gbk', $v);<br>             }<br>             <br>             // 将头信息通过fputcsv写到文件句柄<br>             fputcsv($fp, $head);<br>             <br>             // 计数器<br>             $cnt = 0;<br>             // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小<br>             $limit = 100000;<br>             <br>             // 逐行取出数据,不浪费内存<br>             $count = count($data);<br>             for($t=0;$t                 $cnt ++;<br>                 if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题<br>                     ob_flush();<br>                     flush();<br>                     $cnt = 0;<br>                 }<br>                 $row = $data[$t];<br>                 foreach ($row as $i => $v) {<br>                     $row[$i] = iconv('utf-8', 'gbk', $v);<br>                 }<br>                 fputcsv($fp, $row);<br>             }<br>             <br>             return true;<br>         }<br>         <br>         <br>         <br>         <br>         <br>         <br>         /**<br>          * phpexcel数据格式导出<br>          * @author  Jack<br>          * @param     Array     $head     Excel列名信息<br>           * @param     Array     $data     Excel数据 二维数组<br>          * @param     String     $title     Excel文件标题<br>          * @return  Bool<br>          */<br>         public function phpexcelPut($data=array(),$head=array(),$title=''){<br>             if(!$data) return false;<br>             $array_keys = array_keys($data[0]); //数据数组字段名称<br>             $head = $head ? $head : $array_keys;<br>             $title = $title ? $title : (date('YmdHis').'导出记录');<br>             // 输出Excel文件头<br>             if(!import('ORG.Phpexcel.Phpexcel')) return false;<br>             // Create new PHPExcel object<br>             $objPHPExcel = new PHPExcel();<br>             // Set document properties<br>             $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")<br>                                          ->setLastModifiedBy("Maarten Balliauw")<br>                                          ->setTitle("Office 2007 XLSX Test Document")<br>                                          ->setSubject("Office 2007 XLSX Test Document")<br>                                          ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")<br>                                          ->setKeywords("office 2007 openxml php")<br>                                          ->setCategory("Test result file");    <br>                                          <br>             $count = count($data);<br>             $Array = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');<br>             if($head){ <br>                 foreach($head as $k    => $v){<br>                     $objPHPExcel->getActiveSheet()->getColumnDimension($Array[$k])->setAutoSize(true);   //内容自适应<br>                     $objPHPExcel->setActiveSheetIndex(0)->setCellValue($Array[$k]."1", $head[$k]);<br>                 }<br>             }<br>             <br>             $objPHPExcel->setActiveSheetIndex(0);   <br>             for($i = 2; $i                  foreach($head as $key => $val){<br>                     $objPHPExcel->getActiveSheet()->setCellValue($Array[$key].$i,' '.$data[$i-2][$array_keys[$key]]);<br>                     <br>                 }<br> <br>             }<br>             <br>             $objPHPExcel->getActiveSheet()->setTitle($title);<br>             $objPHPExcel->setActiveSheetIndex(0);<br>             // Redirect output to a client's web browser (Excel5)<br>             header("Content-Type: application/vnd.ms-excel");<br>             header('Content-Disposition: attachment;filename="'.$title.'.xls"');<br>             header("Cache-Control: max-age=0");<br>             // If you're serving to IE 9, then the following may be needed<br>             header("Cache-Control: max-age=1");<br> <br>             // If you're serving to IE over SSL, then the following may be needed<br>             header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past<br>             header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified<br>             header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1<br>             header ('Pragma: public'); // HTTP/1.0<br> <br>             $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');<br>             $objWriter->save('php://output');<br>             exit;<br>         }<br>     <br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn