注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

php开发lamp

《西安--木木》-经历丰富了生活。 架构师QQ群: 246695517

 
 
 

日志

 
 

phpexcel 使用  

2013-12-26 09:04:11|  分类: thinkphp框架 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

<?php 

public function down_excel() {
           $post = $this->_request();
           if (isset($post['id'])) {
            $contract_id = $post['id'] + 0;
          
            $where = ' contract_id =' . $contract_id;
            $voList = D()->query(D("Sale/SaleOutlist")->get_sale_outlist_sql($where, '', ''));
            // $this->func('$.Log',D()->getLastSql());
            foreach ($voList as $key => $val) {
                $voList[$key]['user_time'] = date('Y/m/d', $val['user_time']);
                $voList[$key]['car_type_info'] =$voList[$key]['factory_name'] . '-' . $voList[$key]['car_brand_name'] . '-' . $voList[$key]['biaozhun_name'] . '-' . $voList[$key]['car_models'] . '-' . $voList[$key]['models'] . '-' . $voList[$key]['car_colour'] . '-' . $voList[$key]['car_inter'];
            }
            $filename =date('Y-m-d',mktime()).$contract_id.'.xls';
           // $filename= iconv('utf-8', 'gb2312', date('Y-m-d',mktime()).$contract_id.'.xls');
            ///print_r($voList);
            vendor('Excel.PHPExcel');//载入excel 类库文件
     $phpExcel = new PHPExcel();
     $phpExcel -> getProperties() -> setCreator("oversea.iscard.cn") -> setLastModifiedBy("oversea.iscard.cn") -> setTitle("数据EXCEL导出") -> setSubject("数据EXCEL导出") -> setDescription("备份数据") -> setKeywords("excel") -> setCategory("result file");

            // 添加一些数据
            $num=6;
            foreach($voList as $key => $val){
            ++$num;
            $phpExcel -> setActiveSheetIndex(0)->setCellValue('A'.$num, $val['id'])->setCellValue('B'.$num, $val['car_chassis'])->setCellValue('C'.$num, $val['car_type_info'])->setCellValue('D'.$num, $val['user_time']);
            }
            $phpExcel -> getActiveSheet() -> setCellValue('A6','编号');
            $phpExcel -> getActiveSheet() -> setCellValue('B6','车架号');
            $phpExcel -> getActiveSheet() -> setCellValue('C6', '车型');
            $phpExcel -> getActiveSheet() -> setCellValue('D6','日期');
           
            $sale_contract_array = D('sale_contract')->where(array('id'=>$contract_id))->find();
            $phpExcel -> getActiveSheet() -> setCellValue('A2','合同号');
            $phpExcel -> getActiveSheet() -> setCellValue('A3','客户名称');
            $phpExcel -> getActiveSheet() -> setCellValue('A4', '日期');
           
            $phpExcel -> getActiveSheet() -> setCellValue('B2',$sale_contract_array['contract_name']);
            $phpExcel -> getActiveSheet() -> setCellValue('B3',$sale_contract_array['client_name']);
            $phpExcel -> getActiveSheet() -> setCellValue('B4',date('Y/m/d',$sale_contract_array['contract_time']));
           
            //align
            $phpExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
           
            $phpExcel->getActiveSheet()->getStyle('A5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('B5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('C5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel->getActiveSheet()->getStyle('D5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
           
            //设置宽度
            $phpExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
            $phpExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
            $phpExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
            $phpExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
           
            //合并单元格
            $phpExcel->getActiveSheet()->mergeCells('A1:D1')-> setCellValue('A1','出库详表');
            $phpExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $phpExcel -> getActiveSheet() -> getRowDimension('1') -> setRowHeight(30);
           
            $phpExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
           
            //字体加粗
            $phpExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
            $phpExcel->getActiveSheet()->getStyle('B2')->getFont()->setBold(true);
            $phpExcel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
           
            //设置边框
            //$phpExcel->getActiveSheet()->getStyle('A5:D5')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            $styleArray = array('borders' => array('allborders' => array(
   //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
   'style' => PHPExcel_Style_Border::BORDER_THIN, //细边框
   //'color' => array('argb' => 'FFFF0000'),
   ), ), 'alignment' => array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, ), );
     $phpExcel -> getActiveSheet() -> getStyle('A6:D' . $num) -> applyFromArray($styleArray);
           
   $phpExcel -> setActiveSheetIndex(0);
            ob_clean();//清除ob缓存防止乱码

            header("Content-type:text/html;charset=utf-8");//设置页面编码
   header('Content-Type: application/vnd.ms-excel');
   header('Content-Disposition: attachment;filename="' .$filename . '.xls"');
   header('Cache-Control: max-age=0');
   $objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5');
   $objWriter -> save('php://output');
                        }

              }

 

?>


  评论这张
 
阅读(306)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017