您好,欢迎访问一九零五行业门户网

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

php导出类。支持csv和phpexcel,以前写的
array(
                    'aa'=>1,
                    'bb'=>1,
                    'cc'=>1,
                    'dd'=>1,
                    'ee'=>1,
                    'ff'=>1,
                ),
                '1'=>array(
                    'aa'=>1,
                    'bb'=>2,
                    'cc'=>1,
                    'dd'=>74,
                    'ee'=>1,
                    'ff'=>52,
                ),
                '2'=>array(
                    'aa'=>17,
                    'bb'=>257,
                    'cc'=>157,
                    'dd'=>747,
                    'ee'=>1527,
                    'ff'=>5275,
                )
            );
            $event = a('common/dataput','event');
            //$result = $event->csvput($data,$head);
            //$result = $event->csvput($data);
            //$result = $event->phpexcelput($data,$head);
            //$result = $event->phpexcelput($data);
        }
        ****/
/**
         * csv数据格式导出
         * @author  jack
         * @param     array     $head     excel列名信息 
          * @param     array     $data     excel数据 二维数组
         * @param     string     $filename     excel文件名
         * @return  bool
         **/
        public function csvput($data=array(), $head=array(), $filename=''){
if(!$data) return false;
            $array_keys = array_keys($data[0]); //数据数组字段名称
            $head = $head ? $head : $array_keys;
            $filename = $filename ? $filename : (date('ymdhis').'导出记录.csv');
            // 输出excel文件头
            header(content-type: application/vnd.ms-excel);
            header(content-disposition: attachment;filename={$filename});
            header(cache-control: max-age=0);
// 打开php文件句柄,php://output 表示直接输出到浏览器
            $fp = fopen('php://output', 'a');
            if(!$fp) return false;
// 输出excel列名信息        
            foreach ($head as $i => $v) {
                // csv的excel支持gbk编码,一定要转换,否则乱码
                $head[$i] = iconv('utf-8', 'gbk', $v);
            }
// 将头信息通过fputcsv写到文件句柄
            fputcsv($fp, $head);
// 计数器
            $cnt = 0;
            // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
            $limit = 100000;
// 逐行取出数据,不浪费内存
            $count = count($data);
            for($t=0;$t                $cnt ++;
                if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
                    ob_flush();
                    flush();
                    $cnt = 0;
                }
                $row = $data[$t];
                foreach ($row as $i => $v) {
                    $row[$i] = iconv('utf-8', 'gbk', $v);
                }
                fputcsv($fp, $row);
            }
return true;
        }
/**
         * phpexcel数据格式导出
         * @author  jack
         * @param     array     $head     excel列名信息
          * @param     array     $data     excel数据 二维数组
         * @param     string     $title     excel文件标题
         * @return  bool
         */
        public function phpexcelput($data=array(),$head=array(),$title=''){
            if(!$data) return false;
            $array_keys = array_keys($data[0]); //数据数组字段名称
            $head = $head ? $head : $array_keys;
            $title = $title ? $title : (date('ymdhis').'导出记录');
            // 输出excel文件头
            if(!import('org.phpexcel.phpexcel')) return false;
            // create new phpexcel object
            $objphpexcel = new phpexcel();
            // set document properties
            $objphpexcel->getproperties()->setcreator(maarten balliauw)
                                         ->setlastmodifiedby(maarten balliauw)
                                         ->settitle(office 2007 xlsx test document)
                                         ->setsubject(office 2007 xlsx test document)
                                         ->setdescription(test document for office 2007 xlsx, generated using php classes.)
                                         ->setkeywords(office 2007 openxml php)
                                         ->setcategory(test result file);
$count = count($data);
            $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');
            if($head){ 
                foreach($head as $k    => $v){
                    $objphpexcel->getactivesheet()->getcolumndimension($array[$k])->setautosize(true);   //内容自适应
                    $objphpexcel->setactivesheetindex(0)->setcellvalue($array[$k].1, $head[$k]);
                }
            }
$objphpexcel->setactivesheetindex(0);   
            for($i = 2; $i                 foreach($head as $key => $val){
                    $objphpexcel->getactivesheet()->setcellvalue($array[$key].$i,' '.$data[$i-2][$array_keys[$key]]);
}
}
$objphpexcel->getactivesheet()->settitle($title);
            $objphpexcel->setactivesheetindex(0);
            // redirect output to a client's web browser (excel5)
            header(content-type: application/vnd.ms-excel);
            header('content-disposition: attachment;filename='.$title.'.xls');
            header(cache-control: max-age=0);
            // if you're serving to ie 9, then the following may be needed
            header(cache-control: max-age=1);
// if you're serving to ie over ssl, then the following may be needed
            header ('expires: mon, 26 jul 1997 05:00:00 gmt'); // date in the past
            header ('last-modified: '.gmdate('d, d m y h:i:s').' gmt'); // always modified
            header ('cache-control: cache, must-revalidate'); // http/1.1
            header ('pragma: public'); // http/1.0
$objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');
            $objwriter->save('php://output');
            exit;
        }
}
ad:真正免费,域名+虚机+企业邮箱=0元
其它类似信息

推荐信息