phpexcel 导入导出

        ini_set("memory_limit", "2018M"); // 不够继续加大
        set_time_limit(0);
        $templateName = 'excel.xlsx';
        $objReader = new \PHPExcel_Reader_Excel2007();
        if(!$objReader->canRead($templateName)){
            $objReader = new PHPExcel_Reader_Excel5();
            if(!$objReader->canRead($templateName)){
                echo '无法识别的Excel文件!';
                return false;
            }
        }
        $arr2 =[];
        $objPHPExcel=$objReader->load($templateName);//导入
            $sheet=$objPHPExcel->getSheet(1);//工作表
            $highestRow=$sheet->getHighestRow();//获取总行数
            for($i= 1 ;$i<$highestRow;$i++)
            {
                $str =  $sheet->getCell("C$i")->getValue();
                $arr = explode(',',$str);
                $a = $sheet->getCell("A$i")->getValue();
                foreach ($arr as $k=>$v)
                {
                    $arr2[] = [$a,$v];
                }
            }
        $objPHPExcel= new PHPExcel();
       foreach ($arr2 as  $k=>$v)
       {
           $k++;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$k,$v[0]);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$k,$v[1]);
       }
       $objWriter =\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
        $filename = '整理.xlsx';
        ob_end_clean();//清除缓存以免乱码出现
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter -> save('php://output');
    }

 

posted @ 2018-07-08 17:10  无限REIZ  阅读(90)  评论(0编辑  收藏  举报