• php生成Excel


    简单粗暴直接上代码

        /**
         * @param $res  //二维数组(要保存的数据,$res[0]为excel表头)
         * @param null $FileName  //文件名
         * @param null $width     //每一列的宽度,键名与数据键名相同
         * @param string $rgb     //第一行背景颜色(16进制)
         * @return mixed
         * @throws PHPExcel_Exception
         * @throws PHPExcel_Writer_Exception
         */
        function Excel_Create($res,$FileName =null, $width=null,$rgb=null)
        {
            require_once 'Classes/PHPExcel.php';
    //
            ob_end_clean();
    
            // 实例化excel类
            $objPHPExcel = new PHPExcel();
            // 操作第一个工作表
            $objPHPExcel->setActiveSheetIndex(0);
            // 设置sheet名
            $objPHPExcel->getActiveSheet()->setTitle('Sheet1');
    
            // 列名表头文字加粗
            $objPHPExcel->getActiveSheet()->getStyle('A1:Z1')->getFont()->setBold(true);
            // 列表头文字居中
            $objPHPExcel->getActiveSheet()->getStyle('A1:Z1')->getAlignment()
                ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
            //获取表头
            $keys = array_keys($res[0]);
    
            // 列名赋值
            $col = 'A';
            foreach ($keys as $key) {
                // 列名赋值
                $objPHPExcel->getActiveSheet()->setCellValue($col . '1', $res[0][$key]);
    
                if($rgb!=null){
                    // 设置第一行颜色
                    $objPHPExcel->getActiveSheet()->getStyle($col . '1')->getFill()->applyFromArray(array(
                        'type' => PHPExcel_Style_Fill::FILL_SOLID,
                        'startcolor' => array(
                            'rgb' => $rgb
                        )
                    ));
                }
    
                //设置表格宽度
                if($width!=null){
                    $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setWidth($width[$key]);
                }else{
                    $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setWidth(20);
                }
    
                $col = chr(ord($col) + 1);
            }
            unset($res[0]);
    
            // 数据起始行
            $row_num = 2;
            // 向每行单元格插入数据
            foreach ($res as $value) {
                // 设置所有垂直居中
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'Z' . $row_num)->getAlignment()
                    ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                // 设置价格为数字格式
    //            $objPHPExcel->getActiveSheet()->getStyle('D' . $row_num)->getNumberFormat()
    //                ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
                // 居中
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'Z' . $row_num)->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
                // 设置单元格值
                $col = 'A';
                foreach ($keys as $key) {
                    // 单元格赋值
                    $objPHPExcel->getActiveSheet()->setCellValue($col . $row_num, $value[$key]);
                    $col = chr(ord($col) + 1);
                }
    
                $row_num++;
            }
    
            if($FileName==null){
                $FileName=time();
            }
    
            $outputFileName = $FileName . '.xls';
            $xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
            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-type:application / vnd.ms-excel; charset = UTF-8");
    //        header('Content-Disposition:attachment; filename =“'.iconv("gb2312",'utf-8',$outputFileName).'.xlsx”');
            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");
    //        echo file_get_contents($outputFileName);
            return $res;
        }
    

      

      引入文件(PHPExcel.php)传送门 :http://xcdn.php.cn/leiku/chuli/PHPExcel-1.8.zip?sign=32bd8b6174060344d512ff228b4bc229&timestamp=1571102641

                       :https://www.php.cn/xiazai/leiku/1491

  • 相关阅读:
    ASP.Net Core一个项目中如何支持多种身份认证方式
    c#正则表达式
    Newtonsoft.Json笔记 -JsonPath
    DotLiquid-介绍
    ASP.NET Core-请求频率限制(AspNetCoreRateLimit)
    使用dotnet-gcdump 查找耗内存的大对象【转】
    使用dotnet-dump 查找 .net core占CPU100%的原因【转】
    ADO.NET
    .NET Core-IServiceCollection扩展一个Replace方法
    SafeList-线程安全的List(c#)
  • 原文地址:https://www.cnblogs.com/beiman/p/11677947.html
Copyright © 2020-2023  润新知