要导出的数据 $dataList public function CustomSaveExcel($dataList){ //引入核心文件 ini_set('memory_limit','1024M'); include Yii::$app->basePath . '/../../library/PHPExcelLib/PHPExcel/Writer/IWriter.php'; include Yii::$app->basePath . '/../../library/PHPExcelLib/PHPExcel.php'; include Yii::$app->basePath . '/../../library/PHPExcelLib/PHPExcel/Writer/Excel2007.php'; include Yii::$app->basePath . '/../../library/PHPExcelLib/PHPExcel/IOFactory.php'; // 要读取的文件的路径 $filename = Yii::$app->basePath.'/../web/upload/order-goodslist.xlsx'; $objPHPExcelReader = \PHPExcel_IOFactory::load($filename); $letter = 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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','ZZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV');
$i = 0;$j=0; foreach ($dataList as $key => $row) { if($i>0){ $objPHPExcelReader->createSheet(); } $newobj = $objPHPExcelReader->setActiveSheetIndex($i); foreach ($row as $rowInex => $rowValue) { $index = $letter[$rowInex].($j+5); if(is_numeric($rowValue) && !is_float($rowValue) && !is_double($rowValue) && strlen($rowValue)>10){ $newobj->setCellValueExplicit($index, $rowValue, \PHPExcel_Cell_DataType::TYPE_STRING); } else{ $newobj->setCellValue($index, $rowValue); } } $j++; $title = 'Sheet'.$i; if(!empty($sheetList[$i])){ $title = $sheetList[$i]; } $objPHPExcelReader->getActiveSheet()->setTitle($title); $i++; } $objPHPExcelReader->setActiveSheetIndex(0); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'Excel2007'); $objWriter->save('php://output'); return true; }