PHP的Excel导出与导入

下载地址(注意php版本大于7.3可能会报错)

GitHub - PHPOffice/PHPExcel: ARCHIVED

解压

PHP的Excel导出与导入_第1张图片

1、导出 Excel

$data=[
    ['name'=>'a','age'=>11],
    ['name'=>'b','age'=>22],
    ['name'=>'d','age'=>33],
];
$fileds=[
    "name"=>"名称",
    "age"=>"年龄",
];

push($data,'test',$fileds);
function push($data,$name='fage_Excel',$fields=array()){
    require_once("./Classes/PHPExcel.php");

    error_reporting(E_ALL);
    ini_set('date.timezone','PRC');
    $objPHPExcel = new \PHPExcel();
    ini_set('memory_limit', '-1');
    $username = 'a';
    /*以下是一些设置 ,什么作者  标题啊之类的*/
    $objPHPExcel->getProperties()->setCreator($username)
        ->setLastModifiedBy($username)
        ->setTitle("订单数据导出")
        ->setSubject("订单数据导出")
        ->setDescription("订单数据")
        ->setKeywords("excel")
        ->setCategory("result file");
    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

    //Excel表头
    $num = 0;
    foreach($fields as $k => $v){
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cellName[$num])->setWidth(15);
        $objPHPExcel->setActiveSheetIndex(0)
            //Excel的第A列,uid是你查出数组的键值,下面以此类推
            ->setCellValue($cellName[$num]."1", $v);
        $num++;
    }

    /*Excel里的数据*/
    foreach($data as $k => $v){
        $num=$k+2;
        $vk=0;
        foreach($fields as $kk => $vv){
            if($kk=="imgurl")
            {
                $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$vk].$num,"http://wx.walch.net.cn/walchsales/Uploads/".$v[$kk]);
                //$objPHPExcel->setActiveSheetIndex(0)->
                $objPHPExcel->setActiveSheetIndex(0)->getCell($cellName[$vk].$num)->getHyperlink()->setUrl("http://wx.walch.net.cn/walchsales/Uploads/".$v[$kk]);
            }
            else if(strpos($kk,'img') > 0)
            {

                $v[$kk] =  str_replace(base_url(),"",$v[$kk]) ;
                $path='.'.$v[$kk];
                if($v[$kk]&&file_exists($path)){

                    $objPHPExcel->setActiveSheetIndex(0)->getRowDimension($num)->setRowHeight(80);
                    $img=new \PHPExcel_Worksheet_Drawing();
                    $img->setPath($path);//写入图片路径
                    $img->setHeight(100);//写入图片高度
                    //$img->setWidth(100);//写入图片宽度
                    $img->setOffsetX(1);//写入图片在指定格中的X坐标值
                    $img->setOffsetY(1);//写入图片在指定格中的Y坐标值
                    $img->setRotation(1);//设置旋转角度
                    $img->getShadow()->setVisible(true);//
                    $img->getShadow()->setDirection(10);//
                    $img->setCoordinates($cellName[$vk].$num);//设置图片所在表格位置
                    $img->setWorksheet($objPHPExcel->setActiveSheetIndex(0));//把图片写到当前的表格中
                }

            }
            else
            {

                if($kk == "number")
                {
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cellName[$vk].$num, $num-1);
                }
                else
                {
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cellName[$vk].$num, $v[$kk]);
                }
            }


            $vk++;
        }
    }

    $objPHPExcel->getActiveSheet()->setTitle('User');
    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-type: application/vnd.ms-excel;charset=utf-8;name="' . $name . '.xlsx"');
    header("Content-Disposition: attachment; filename=$name.xls");
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    $objWriter->save('php://output');
    exit;
}

PHP的Excel导出与导入_第2张图片

2、导入Excel

先通过文件上传方式到服务器

php+html+js+ajax实现文件上传_php上传文件ajax-CSDN博客

读取数据

$file='test.xls';
$data=read($file);
var_dump($data);exit;
function read($file){
    require_once("./Classes/PHPExcel.php");
    // 加载Excel文件
    $objPHPExcel = PHPExcel_IOFactory::load($file);

    // 获取第一个工作表
    $sheet = $objPHPExcel->getActiveSheet();
    $rowDataAll=[];
      // 循环读取每一行数据
    foreach ($sheet->getRowIterator() as $row) {
        $rowData = array();
        // 循环读取每一列数据
        foreach ($row->getCellIterator() as $cell) {
            $rowData[] = $cell->getValue();
        }
        $rowDataAll[]=$rowData;

    }
    return $rowDataAll;
}

PHP的Excel导出与导入_第3张图片

你可能感兴趣的:(乱七八糟,php,php,excel,开发语言)