zoukankan      html  css  js  c++  java
  • thinkphp3.2.3集成phpexcel1.8导出设置单元格合并

    1 到这里下载classes里面的文件

    https://github.com/PHPOffice/PHPExcel

    2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexcel  然后把文件放进去

    3 在封装一个函数

    function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') { 
        vendor('Phpexcel.PHPExcel'); //从PHPMailer目录导class.phpmailer.php类文件
        //import("Vendor.excel.phpexcel"); 
        //若没有指定文件名则为当前时间戳 
        if (is_null($savefile)) { 
            $savefile = time(); 
        } 
        //若指字了excel表头,则把表单追加到正文内容前面去 
        if (is_array($title)) { 
            array_unshift($data, $title); 
        } 
     
        $objPHPExcel = new PHPExcel(); 
        //Excel内容 
        $head_num = count($data); 
     
        $obj = $objPHPExcel->setActiveSheetIndex(0); 
        $obj->mergeCells('A1:K1'); //第一行
        $obj->setCellValue('A1','');  
        $obj->mergeCells('A2:K2'); //第二行
        $obj->setCellValue('A2','好助教支付清单');  
        $obj->mergeCells('A3:K3'); //第三行
        $obj->setCellValue('A3','用户:北交大出版社    时间:2019-01-12至2019-12-12练习册:200种   支付金额:20000元');  
       
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(20);  //设置字体大小
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setSize(12);  //设置字体大小
     
        $objPHPExcel->getActiveSheet()->getRowDimension('A2')->setRowHeight(40);//行高
        $objPHPExcel->getActiveSheet()->getRowDimension('A3')->setRowHeight(30);//行高
        
        foreach ($data as $k => $v) { 
            
            $row = $k + 5; //
            $nn = 0; 
     
            foreach ($v as $vv) { 
                $col = chr(65 + $nn); //
                $obj->setCellValue($col . $row, $vv); //列,行,值 
                $nn++; 
            } 
        } 
        //设置列头标题 
        for ($i = 0; $i < 9; $i++) { 
            $alpha = chr(65 + $i); 
            //$objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //单元宽度自适应  
            $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setWidth(20);
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '5')->getFont()->setSize(12);  //设置大小 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '5')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 
        } 
     
       $objPHPExcel->getActiveSheet()->setTitle($sheetname); //题目 
       $objPHPExcel->setActiveSheetIndex(0); //设置当前的sheet   
       header('Content-Type: application/vnd.ms-excel'); 
       header('Content-Disposition: attachment;filename="' . $savefile . '.xls"'); //文件名称 
       header('Cache-Control: max-age=0'); 
       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //Excel5 Excel2007 
       $objWriter->save('php://output'); 
    }

    4 然后调用这个函数

            $list=array(0=>array('1','5.3数学练习册','*','23121','23122','34000','140000','100','149900','*'),
                        1=>array('','','founder','*','23123','23124','4','*','*','2019/1/12 21:12:32'),
                        2=>array('','','nick','*','23124','23125','7','100','149900','*')
            );
            $title = array('序号', '练习册名称', '终端用户', '累计数量(二维码)', '个数区间开始', '个数区间结束', '分成', '剩余处理费用', '最终支付', '购买时间'); //设置要导出excel的表头 
            exportExcel($list, 1, $title);

    5 然后结果的样子

  • 相关阅读:
    MySQL技术内幕(第5版)
    Creo Parametric 4.0基础、进阶、高手一本通
    PostgreSQL 9.0性能调校
    Python程序设计案例课堂
    Creo2.0完全学习手册
    Excel会计电算化与应用(第3版)
    JPEG与RAW的较量 数码影像拍摄与后期全流程详解
    [Objective-c 基础
    [Objective-c 基础
    iOS开发网络篇—GET请求和POST请求(转)
  • 原文地址:https://www.cnblogs.com/baker95935/p/11182595.html
Copyright © 2011-2022 走看看