码迷,mamicode.com
首页 > Web开发 > 详细

php 下载导出xls格式

时间:2017-12-23 20:15:39      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:date   binary   ade   result   class   explicit   gets   nta   xls   

    //xls格式下载
        set_time_limit(0);
        $line_num = 1;
        $resultPHPExcel = new PHPExcel();

        $resultPHPExcel->getActiveSheet()->setCellValue("A1", ‘业务参考号‘);
        $resultPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(10);    //设置单元格宽度
        $resultFillA1 = $resultPHPExcel->getActiveSheet()->getStyle("A1")->getFill("A1");//颜色
        $resultFillA1->setFillType(PHPExcel_Style_Fill::FILL_SOLID);

        $resultPHPExcel->getActiveSheet()->setCellValue("B1", ‘收款人编号‘);
        $resultPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(10);    //设置单元格宽度
        $resultFillA1 = $resultPHPExcel->getActiveSheet()->getStyle("B1")->getFill("B1");//颜色
        $resultFillA1->setFillType(PHPExcel_Style_Fill::FILL_SOLID);

       
        //字体加粗
        $resultPHPExcel->getActiveSheet()->getStyle(‘A1:B1‘)->getFont()->setBold(true);
        //居中
        $resultPHPExcel->getActiveSheet()->getStyle(‘A1:B1‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $outputFileName = date(‘Y年m月d日 H时i分‘,time()) . ‘合同信息下载.xls‘;

        //每次取数据的步进
        $step = 1000;
        $round = ceil($count / $step);
        for ($i = 1; $i <= $round; $i++)
        {
            $criteria->limit = $step;    //取1条数据,如果小于0,则不作处理
            $criteria->offset = ($i - 1) * $step;   //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10
            $rent_contract_list = T_rent_contract::model()->findAll($criteria);

            if (count($rent_contract_list))
            {
                foreach ($rent_contract_list as $rent_contract)
                {
                    
                    $line_num++;
                    $resultPHPExcel->getActiveSheet()->setCellValue("A{$line_num}",$content_a);
                    $resultPHPExcel->getActiveSheet()->setCellValue("B{$line_num}",content_b);
                    //$resultPHPExcel->getActiveSheet()->setCellValueExplicit("C{$line_num}", $rent_contract->f_rent_number,PHPExcel_Cell_DataType::TYPE_STRING);    //银行卡账号,防止导出到excel后数据格式不对
                }
            }
        }

        define(‘PHPEXCEL_ROOT‘, dirname(__FILE__));
        require_once PHPEXCEL_ROOT . ‘PHPExcel/Writer/Excel5.php‘;
        $xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header(‘Content-Disposition:inline;filename="‘ . $outputFileName . ‘"‘);
        header("Content-Transfer-Encoding: binary");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $xlsWriter->save("php://output");
        exit;

 

php 下载导出xls格式

标签:date   binary   ade   result   class   explicit   gets   nta   xls   

原文地址:http://www.cnblogs.com/zwtqf/p/8094043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!