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 

微擎生成公众号二维码 PHP

微擎生成公众号二维码(临时二维码)

$barcode = array(
    'expire_seconds' => 2592000,
    'action_name' => 'QR_STR_SCENE',
    'action_info' => array(
        'scene' => array(
            'scene_id'=>'100001',
            'scene_str' => '1_1'
        ),
    ),
);
$account_api = WeAccount::create();
$result = $account_api->barCodeCreateDisposable($barcode);
print_r($result);die;
//使用 即可

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

php 生成随机邀请码 PHP

//随机生成邀请码
function getRandomString($len, $chars=null)
{
    if (is_null($chars)){
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }

    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}

getRandomString(6); //生成6位随机邀请码

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

php phpOffice/PhpSpreadsheet 使用记录 PHP

<?php

//先自行使用composer下载phpoffice到项目文件夹
//官网地址:https://phpspreadsheet.readthedocs.io/en/latest/
//安装命令:composer require phpoffice/phpspreadsheet

//引入自动加载
require "./vendor/autoload.php";

//运行命名空间
use PhpOffice\PhpSpreadsheet;

//文件名
$inputFileName="1.xlsx";

//自动识别文件类型
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
/**  Create a new Reader of the type that has been identified  **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/**  Load $inputFileName to a Spreadsheet Object  **/
$spreadsheet = $reader->load($inputFileName);

//获取工作簿
$sheet=$spreadsheet->getActiveSheet();

//读取表格全部数据为数组
$sheetData = $sheet->toArray(null, true, true, true);

//获取单个单元格内容
$sheetvalue=$sheet->getCell('A1')->getValue();
$sheetvalue=$sheet->getCellByColumnAndRow(1,1)->getValue();

//设置单元格内容
$sheet->setCellValueByColumnAndRow(1,1,'2020级学生学号');

$sheet->setCellValue('A2', 'www.phpexcel.net');
//将上面单元格设置可点击的链接    注:网址需要加http://
$sheet->getCell('A2')->getHyperlink()->setUrl('http://www.phpexcel.net');

//设置工作簿标签颜色
//$sheet->getTabColor()->setRGB('F9F900');

//写入图片到单元格
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath('1.jpg');
//单元格坐标
$drawing->setCoordinates('A2');
//设置图片宽高
$drawing->setWidth(30);
$drawing->setHeight(30);
$drawing->setWorksheet($sheet);

//多个图片需用遍历new
/*for ($i=3;$i<=5;$i++){
    $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
    $drawing->setName('Logo');
    $drawing->setDescription('Logo');
    $drawing->setPath('1.jpg');
    $drawing->setCoordinates('A'.$i);
    $drawing->setWidth(30);
    $drawing->setHeight(30);
    $drawing->setWorksheet($sheet);
}*/

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('01simple.xlsx');

//写入文件到项目目录
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("2.xlsx");

//下载文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
//header(‘Content-Type:application/vnd.ms-excel‘);//告诉浏览器将要输出Excel03版本文件
header('Content-Disposition: attachment;filename="文档.xlsx"');//告诉浏览器输出浏览器名称
header('Cache-Control: max-age=0');//禁止缓存
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

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