include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel.php'); include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel/IOFactory.php'); $header_arr = 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'); $objPHPExcel = new PHPExcel();//实例化一个要保存的phpExcel对象 //写数据开始 //在激活的工作区写入数据 (单条写入数据演示) $objPHPExcel->getActiveSheet()->setCellValue('B8', '汉字'); //在激活的工作区写入数据 (数组写入数据演示) $startRow = 1; foreach ($list as $row) { foreach ($indexKey as $key => $value){ //这里是设置单元格的内容 $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]); } $startRow++; } //写数据结束 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save($file);
参考: https://blog.csdn.net/weixin_39696369/article/details/78646494