PHP读取excel表格,和导出表格
读取表格
public function excel(){ import("Common.Vendor.Excel.PHPExcel"); $filename="./files.xlsx"; $PHPExcel=new \PHPExcel(); import("Common.Vendor.Excel.PHPExcel.Reader.Excel2007"); $PHPReader=new \PHPExcel_Reader_Excel2007(); $PHPExcel=$PHPReader->load($filename); $currentSheet=$PHPExcel->getSheet(0); $allColumn=$currentSheet->getHighestColumn(); $allRow=$currentSheet->getHighestRow(); for($currentRow=2;$currentRow<=$allRow;$currentRow++){ $arr['id'] = $PHPExcel->getActiveSheet()->getCell('A'.$currentRow)->getValue(); $arr['catId'] = $PHPExcel->getActiveSheet()->getCell('B'.$currentRow)->getValue(); $arr['parentId'] = trim($PHPExcel->getActiveSheet()->getCell('C'.$currentRow)->getValue()); $arr['name'] = $PHPExcel->getActiveSheet()->getCell('D'.$currentRow)->getValue(); $arr['catClass'] = $PHPExcel->getActiveSheet()->getCell('E'.$currentRow)->getValue(); $arr['state'] = $PHPExcel->getActiveSheet()->getCell('F'.$currentRow)->getValue(); $arr['one'] = $PHPExcel->getActiveSheet()->getCell('G'.$currentRow)->getValue(); $data[] =$arr; } return $data ; }
导出表格
function exportexcel($data=array(),$title=array(),$filename='report'){ header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=".$filename.".xls"); header("Pragma: no-cache"); header("Expires: 0"); //导出xls 开始 if (!empty($title)){ foreach ($title as $k => $v) { $title[$k]=iconv("UTF-8", "GBK",$v); } $title= implode("\t", $title); echo "$title\n"; } if (!empty($data)){ foreach($data as $key=>$val){ foreach ($val as $ck => $cv) { $data[$key][$ck]=iconv("UTF-8", "GBK", $cv); } $data[$key]=implode("\t", $data[$key]); } echo implode("\n",$data); } }