记录一个php导出csv方法 PHP

 public function export_excel(){

       //查询列表传入即可

        $html = $this->wxapp_house_export_parse_zan($list);
        header("Content-type:text/csv");
        header("Content-Disposition:attachment; filename=点赞信息.csv");
        echo $html;
        exit();

    }

    //改善后
        function export_jslog($list) {
            if (empty($list)) {
                return false;
            }
            $header = array(
                'id'=>'id',
                'openid' => 'openid',
                'nickname' => '用户名称',
                'mobile'=>'手机号',
                'province'=>'省',
                'city'=>'市',
                'area'=>'区',
                'mj'=>'面积',
                'shi'=>'室',
                'ting'=>'厅',
                'chu'=>'厨',
                'wei'=>'卫',
                'addtime'=>'计算时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";

            foreach ($list as $row) {

                $fans=$this->get_fansinfo($row['openid']);
                $row['nickname']=$fans['nickname'];
                $row['addtime']=date('Y-m-d H:i:s',$row['addtime']);
                foreach ($keys as $key) {
                    $data[] = $row[$key];
                }
                $user[] = implode("\t ,", $data) . "\t ,";
                unset($data);
            }

            $html .= implode("\n", $user) . "\n";

            return $html;
        }

    function wxapp_house_export_parse_zan($cards) {
        if (empty($cards)) {
            return false;
        }
        $header = array(
            'openid' => 'openid',
            'name' => '用户名称','phone'=>'手机号','dw'=>'单位','zw'=>'职务','address'=>'地址'
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        $count = count($cards);
        $pagesize = ceil($count / 5000);
        for ($j = 1; $j <= $pagesize; $j++) {
            $list = array_slice($cards, ($j - 1) * 5000, 5000);
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    $user = array();
                    foreach ($buffer as $row) {
                        $area = pdo_fetchcolumn("SELECT name FROM " . tablename('amouse_wxapp_category') . " WHERE id=:id  limit 1", array(":id" => $row['categoryId']));
                        $row['categoryId'] = $area;
                        $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                    $html .= implode("\n", $user) . "\n";
                }
            }
        }
        return $html;
    }

杨佳乐 发布于  2024-4-30 11:03 

PHP gd库写入文字居中 PHP

function mergeText_mobile($target, $data, $text)
{
    $font = IA_ROOT . "/web/resource/fonts/msyhbd.ttf";
    $colors = hex2rgb($data["color"]);

    $color = imagecolorallocate($target, $colors["red"], $colors["green"], $colors["blue"]);

    $res=imagettfbbox(16,0,$font,$text);
    logging_run($res);
    $x=(640-$res[2]*2)/2;

    //计算文字占用像素

    imagettftext($target, $data["size"], 0, $x, $data["top"] + $data["size"], $color, $font, $text);
    return $target;
}

杨佳乐 发布于  2024-4-30 11:01 

php gd库处理微信头像为圆形 PHP

function yuanjiao($imgpath) {
    $ext = pathinfo($imgpath);
    $src_img = null;
    switch ($ext['extension']) {
        case 'jpg':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case 'png':
            $src_img = imagecreatefrompng($imgpath);
            break;
    }
    $wh = getimagesize($imgpath);
    $w = $wh[0];
    $h = $wh[1];
    $w = min($w, $h);
    $h = $w;
    $img = imagecreatetruecolor($w, $h);
    //这一句一定要有
    imagesavealpha($img, true);
    //拾取一个完全透明的颜色,最后一个参数127为全透明
    $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    imagefill($img, 0, 0, $bg);
    $r = $w / 2; //圆半径
    $y_x = $r; //圆心X坐标
    $y_y = $r; //圆心Y坐标
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $rgbColor = imagecolorat($src_img, $x, $y);
            if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                imagesetpixel($img, $x, $y, $rgbColor);
            }
        }
    }
    return $img;
}

杨佳乐 发布于  2024-4-30 11:00 

php 微擎 导入excel记录 PHP

load()->library('phpexcel/PHPExcel');
load()->library('phpexcel/PHPExcel/IOFactory');
load()->library('phpexcel/PHPExcel/Reader/Excel5');

if (checksubmit('submit')){
    load()->func('file');
    if($_FILES['excel']['type']=="application/octet-stream"){

        //文件上传
        if (!file_move($_FILES['excel']['tmp_name'], ATTACHMENT_ROOT . '/leexcel/'.TIMESTAMP.'.xls')) {
           message('文件上传失败,请尝试重新上传!','','error');
        }else{
            $objReader = PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = PHPExcel_IOFactory::load(ATTACHMENT_ROOT . 'leexcel/'.TIMESTAMP.'.xls');
            $sheet=$objPHPExcel->getSheet(0);//获取第一个工作表

            $highestRow=$sheet->getHighestRow();//取得总行数
            $highestColumn = $sheet- >getHighestColumn(); // 取得总列数 可以遍历行数的时候同时遍历列数
            if($highestRow<2){
                message('未发现导入数据!');
            }
            $succ=0;
            for($j=2;$j<=$highestRow;$j++){
                $kh=$objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();
                $mm=$objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
                $je=$objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();
                //获取日期处理$i=date('Y/m/d',PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("I" . $j)->getValue()));

                //echo $kh.'--'.$mm.'--'.$je."<br/>";
                //插入数据
                //查询卡号是否存在
                $is_cz=pdo_get('tiger_newhu_le_km',array('kh'=>$kh));
                if(empty($is_cz)){
                    //插入
                    if(!empty($kh) && !empty($mm) && !empty($je)){
                        $data=array(
                            'kh'=>$kh,
                            'mm'=>$mm,
                            'je'=>$je,
                            'createtime'=>TIMESTAMP
                        );
                        pdo_insert('tiger_newhu_le_km',$data);
                        $succ++;
                    }
                }

            }
            message('导入完成,成功导入:'.$succ.'条','','success');
        }

    }else{
        message('文件类型错误!','','error');
    }

}

Uncaught exception 'PHPExcel_Exception' with message 'Unknown codepage: 10008' in 错误处理

在phpexcel/shared/codepage.php 增加

case 10008: return 'MAC';               break;

杨佳乐 发布于  2024-4-30 10:59 

微擎上传远程附件 PHP

微擎上传远程附件 注:需要 load->func('file');

elseif ($operation == 'wxupload') {
    load()->func('file');
    $account_api = WeAccount::create();

    $data = array('err' => 0, 'msg' => '', 'imgUrl' => '');
    //access_token
    $sAccessToken = $account_api->getAccessToken();
    $sMediaId = trim($_GPC['mediaId']);
    if (!$sMediaId) {
        $data['msg'] = '参数错误,请重新选择图片上传!';
        echo json_encode($data);
        die;
    }
    $sUrl = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=' . $sAccessToken . '&media_id=' . $sMediaId;

    //文件名称
    $folder = IA_ROOT . '/' . $_W['config']['upload']['attachdir'] . "/images/{$_W['uniacid']}" . '/' . date('Y/m/');
    $filepath = "images/{$_W['uniacid']}" . '/' . date('Y/m/');
    //$filename = date("ymdhis",time()).".jpg";
    !is_dir($folder) && @mkdir($folder, 0755, true);
    //$new_file = $filepath.$filename;
    $filename = random(30) . '.' . 'jpg';
    //$sImgPath = date('Y',time()).'/'.date('md',time()). '/'.time(). '.jpg';
    //$sImgName =  PHPCMS_ROOT.'/front/public/uploadfile/' .$sImgPath;

    $oFileInfo = downloadWeixinFile($sUrl); //downloadWeixinFile
    if ($oFileInfo['body']) {
        saveWechatFile($folder, $filename, $oFileInfo['body']);
        if (!empty($_W['setting']['remote']['type'])) {
            $original_img = $filepath . $filename;

            $remotestatus = file_remote_upload($original_img);
            if (is_error($remotestatus)) {
                //file_delete($pathname);
                $result['error'] = 1;
                $result['message'] = '远程附件上传失败,请检查配置并重新上传';
                die(json_encode($result));
            } else {
                file_delete($original_img);
            }
        }
        $data['imgUrl'] = $filepath . $filename;
        echo json_encode($data);
        die;
    } else {
        $data['msg'] = '图片上传失败!';
        echo json_encode($data);
        die;
    }
}

function downloadWeixinFile($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
    curl_close($ch);
    $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
    return $imageAll;
}

function saveWechatFile($newfolder, $filename, $filecontent) {
    createFolder($newfolder);
    $local_file = fopen($newfolder . "/" . $filename, 'w');
    if (false !== $local_file) {

        if (false !== fwrite($local_file, $filecontent)) {

            fclose($local_file);
        }
    }
}

杨佳乐 发布于  2024-4-30 10:58