ThinkPHP 使用 phpExcel-1.8 第三方类 导出excel 导入excel

一、phpExcel-1.8 第三方类 下载地址:https://download.csdn.net/download/m0_37711659/10558941

二、将phpExcel解压到\vendor目录  如图:

/**
 * 导出excel
 * @param array $container    容器
 * @param array $title    标题
 * @param array $data    表格内容
 * @param string $filename 文件名
 * @param string $suffix 后缀
 */
function exportExcel($container, $title, $data, $filename, $suffix='xls'){
    /** 错误报告 */
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Shanghai');
    
    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');
    
    /** Include PHPExcel */
    vendor("PHPExcel.PHPExcel"); //vendor/PHPExcel/PHPExcel.php
    
    // Create new PHPExcel object
    $objPHPExcel = new \PHPExcel();
    
    $objProps = $objPHPExcel->getProperties();
    $objProps->setCreator("Zeal Li");   
    $objProps->setLastModifiedBy("Zeal Li");   
    $objProps->setTitle("Office XLS Test Document");   
    $objProps->setSubject("Office XLS Test Document, Demo");   
    $objProps->setDescription("Test document, generated by PHPExcel.");   
    $objProps->setKeywords("office excel PHPExcel");   
    $objProps->setCategory("Test");
    $objPHPExcel->setActiveSheetIndex(0);
    $objActSheet = $objPHPExcel->getActiveSheet();
    //设置当前活动sheet的名称
    $objActSheet->setTitle('sheet1');

    //设置单元格内容
    $n=0;
    foreach($title as $v){
        $objActSheet->setCellValue($container[$n].'1', $v);
        ++$n;
    }
    $n=0;
    $k=1;
    foreach($data as $val){
        ++$k;
        foreach($val as $v){
            $objActSheet->setCellValueExplicit($container[$n].$k, $v,\PHPExcel_Cell_DataType::TYPE_STRING);
            ++$n;
        }
        $n=0;
    }
    
    // 将输出重定向到客户端的Web浏览器 (Excel5)
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.'-'.date('Y-m-d_H-i-s').'.'.$suffix.'"');
    header('Cache-Control: max-age=0');
    // 如果您服务于IE 9,则可能需要以下内容
    header('Cache-Control: max-age=1');
    
    // 如果您在SSL上服务于IE,则可能需要以下内容
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0
    
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}

/**
 * 导入excel
 * @param string $filename excel文件
 * @param string $exts 后缀
 */
function importExcel($filename, $exts='xls'){
    //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能import导入
    //vendor('PHPExcel.PHPExcel');
    //创建PHPExcel对象,注意,不能少了\
    //$PHPExcel = new \PHPExcel();
    //如果excel文件后缀名为.xls,导入这个类
    if ($exts == 'xls') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel5');
        $PHPReader = new \PHPExcel_Reader_Excel5();
    } else if ($exts == 'xlsx') {
        Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        $PHPReader = new \PHPExcel_Reader_Excel2007();
    } else {
        return [];
    }
 
    //载入文件
    $PHPExcel = $PHPReader->load($filename);
    //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
    $currentSheet = $PHPExcel->getSheet(0);
    //获取总行数 6
    $allRow = $currentSheet->getHighestRow();
    
    //获取总列数 AK
    $allColumn = $currentSheet->getHighestColumn();
    
    $allColumn = str_split($allColumn);
    $a = $allColumn[0]; //A
    $b = $allColumn[1]; //K

    //循环字母 Y+1=Z    Z+1=AA AA<Z(因为A<Z,先比较第一位再比较第二位)
    for ($row = 1; $row <= $allRow; $row++) {
        
        //循环单字母
        if ($b || $a == 'Z') {
            for ($k = 'A'; $k != 'AA'; $k++) {
                //数据坐标
                $address = $k . $row;
                
                $cell = $currentSheet->getCell($address)->getValue();
                
                if ($cell instanceof PHPExcel_RichText) {
                    $cell = $cell->__toString();
                }
                
                $data[$row][$k] = $cell;
            }
        } else {
            for ($k = 'A'; $k <= $a; $k++) { //单字母
                //数据坐标
                $address = $k . $row;
                //读取到的数据,保存到数组$data中
                $cell = $currentSheet->getCell($address)->getValue();
                
                if ($cell instanceof PHPExcel_RichText) {
                    $cell = $cell->__toString();
                }
                
                $data[$row][$k] = $cell;
            }
        }
        
        if($b){ //循环多字母 当
            
            if($a != 'Z' && $b != 'Z'){
                for ($i = 'A'; $i <= $a; $i++) { //第1字母
                    for ($j = 'A'; $j <= $b; $j++) { //第2字母
                        $address = $i . $j . $row;
                        //读取到的数据,保存到数组$data中
                        $cell = $currentSheet->getCell($address)->getValue();
             
                        if ($cell instanceof PHPExcel_RichText) {
                            $cell = $cell->__toString();
                        }
                        
                        $data[$row][$i.$j] = $cell;
                    }
                }
            }
            
            if($a == 'Z' && $b != 'Z'){
                for ($i = 'A'; $i != 'AA'; $i++) { //第1字母
                    for ($j = 'A'; $j <= $b; $j++) { //第2字母
                        $address = $i . $j . $row;
                        //读取到的数据,保存到数组$data中
                        $cell = $currentSheet->getCell($address)->getValue();
             
                        if ($cell instanceof PHPExcel_RichText) {
                            $cell = $cell->__toString();
                        }
                        
                        $data[$row][$i.$j] = $cell;
                    }
                }
            }
            
            if($a != 'Z' && $b == 'Z'){
                for ($i = 'A'; $i <= $a; $i++) { //第1字母
                    for ($j = 'A'; $j != 'AA'; $j++) { //第2字母
                        $address = $i . $j . $row;
                        //读取到的数据,保存到数组$data中
                        $cell = $currentSheet->getCell($address)->getValue();
             
                        if ($cell instanceof PHPExcel_RichText) {
                            $cell = $cell->__toString();
                        }
                        
                        $data[$row][$i.$j] = $cell;
                    }
                }
            }
            
            if($a == 'Z' && $b == 'Z'){
                for ($i = 'A'; $i != 'AA'; $i++) { //第1字母
                    for ($j = 'A'; $j != 'AA'; $j++) { //第2字母
                        $address = $i . $j . $row;
                        //读取到的数据,保存到数组$data中
                        $cell = $currentSheet->getCell($address)->getValue();
             
                        if ($cell instanceof PHPExcel_RichText) {
                            $cell = $cell->__toString();
                        }
                        
                        $data[$row][$i.$j] = $cell;
                    }
                }
            }
            
        }
        
    }
    
    return $data;
}

/**
     * 导出excel
     */
    public function exportExcel(){
        $container = array('A','B','C','D','E','F'); //容器
        $title = array('A1标题','B1标题','C1标题','D1标题','E1标题','F1标题'); //标题
        $data = array( //内容
            array('A2内容','','C2内容','','E2内容','F2内容'),
            array('A3内容','','C3内容','','E3内容','F3内容'),
            array('A4内容','B4内容','','D4内容','E4内容',''),
        );
        

        //导出excel
        exportExcel($container, $title, $data, 'filename');
    }

/**
     * 导入excel
     */
    public function importExcel(){
        //上传 https://www.kancloud.cn/manual/thinkphp5/155159
        $file = $this->request->file('excel');//接收excel文件
        
        //上传文件
        $info = $file->rule('uniqid')->validate(['ext'=>'xls,xlsx'])->move(ROOT_PATH . 'public' . DS . 'upload\\excel',false);
        
        if(!$info){
            $this->error($file->getError());
        }
        
        // 成功上传后 转数组  最后操作数据库
        $data = importExcel('public\\upload\\excel\\'.$info->getFilename(), $info->getExtension());
        
        dump($data);
        //操作数据库
    }

猜你喜欢

转载自blog.csdn.net/m0_37711659/article/details/81198562