• PHPExcel导出主要代码记录


    /*
     * 获取下一个列名
     * 注意,此方法最多支持到两位
     * */
    function getNextColum($colum){
        if(empty($colum))return 'A';
        $key = ord(substr($colum,strlen($colum)-1,1));
        if($key<90){
            $str = strlen($colum)>1?substr($colum,0,1).chr($key+1):chr($key+1);
        }else{
            $str = strlen($colum)>1?chr(ord(substr($colum,0,1))+1).'A':'AA';
        }
        return $str;
    }
    
    function exportExcel(){
        $keyValue = array(
            'field_01' => '第一列标题',
            'field_02' => '第二列标题',
            'field_03' => '第三列标题',
            'field_04' => '第四列标题',
        );
        $list = array(
            array('field_01' => 'value_01', 'field_02' => 'value_02', 'field_03' => 'value_03', 'field_04' => 'value_04'),
            array('field_01' => 'value_01', 'field_02' => 'value_02', 'field_03' => 'value_03', 'field_04' => 'value_04'),
            array('field_01' => 'value_01', 'field_02' => 'value_02', 'field_03' => 'value_03', 'field_04' => 'value_04'),
            array('field_01' => 'value_01', 'field_02' => 'value_02', 'field_03' => 'value_03', 'field_04' => 'value_04')
        );
        $this->import('PHPExcel');
        $PHPExcel = new PHPExcel();
        $fileName = iconv("utf-8", "GBK", 'excel文件名_导出时间(' . date('YmdHi', time()) . ').xlsx');
        $colum = "A";//设置表头
        foreach ($keyValue as $keys => $value) {
            $PHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $value);
            $PHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth(30);
            $colum = getNextColum($colum);
        }
        $row = 2;
        foreach ($list as $value) {
            $colum = "A";//设置表头
            foreach ($keyValue as $v => $fieldContent) {
                $PHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($colum . $row, empty($value[$v]) ? '--' : $value[$v], PHPExcel_Cell_DataType::TYPE_STRING);
                $PHPExcel->getActiveSheet()->getColumnDimension($colum)->setWidth(30);
                $colum = getNextColum($colum);
            }
            $row++;
        }
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header("Content-Disposition: attachment; filename="$fileName"");
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
        $objWriter->save('php://output');
    }
  • 相关阅读:
    【Codeforces Round #435 (Div. 2) A B C D】
    【Codeforces 851D Arpa and a list of numbers】
    预科班第三次考试试卷总结
    python判断结构总结
    《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 4 章 答案
    《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 2 章 答案
    《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 1 章 答案
    python函数总结
    python字符串、列表和文件对象总结
    python字符串格式化之format
  • 原文地址:https://www.cnblogs.com/qifeng1991/p/4864801.html
Copyright © 2020-2023  润新知