//导入excel public function test1(){ try{ $datainfo = Db::connect("db_configB")->name('lzf_detail')->field('name,mobile,id_card,score,result,addtime')->select(); $outputFileName = date("Ymd") . '数据明细.csv'; $str = iconv('utf-8', 'gb2312', "姓名,手机号码,身份证号码,灵芝分,是否命中黑名单,添加时间") . "\n"; foreach ($datainfo as $k => $v) { $v['result'] = empty($v['result'])? '空':$v['result']; $str .= iconv('utf-8', 'gbk//IGNORE', $v['name'] . "," . $v['mobile'] . "\t," . $v['id_card'] . "\t," . $v['score'] . "," . $v['result'] . "," . date("Y-m-d H:i:sa",$v['addtime'])) . "\n"; } header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=" . $outputFileName); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); echo $str; exit(); }catch (\Exception $e){ logNew('',$e); echo "错误";die(); } }
//引入PHPExcel vendor('PHPExcel.Classes.PHPExcel'); //导出Excel function excelToArray(){ $filename = ''; foreach ($_FILES as $k){ $filename = $k['tmp_name']; } // require_once . '/PHPExcel/Classes/PHPExcel/IOFactory.php'; //加载excel文件 // $filename = dirname(__FILE__).'/result.xlsx'; // $filename = 'F:/滴滴花黑名单.xlsx'; $objPHPExcelReader = \PHPExcel_IOFactory::load($filename); $reader = $objPHPExcelReader->getWorksheetIterator(); //循环读取sheet foreach($reader as $sheet) { //读取表内容 $content = $sheet->getRowIterator(); //逐行处理 $res_arr = array(); foreach($content as $key => $items) { $rows = $items->getRowIndex(); //行 $columns = $items->getCellIterator(); //列 $row_arr = array(); //确定从哪一行开始读取 if($rows < 2){ continue; } //逐列读取 foreach($columns as $head => $cell) { //获取cell中数据 $data = $cell->getValue(); $row_arr[] = $data; } $res_arr[] = $row_arr; } } return json_encode($res_arr); }
本文为Jsky原创文章,转载无需和我联系,但请注明来自Jsky博客 www.tjin.link