$objPHPExcel = new PHPExcel(); //设置活动单指数到第一个表,使Excel打开默认sheet1 $objPHPExcel->setActiveSheetIndex(0); //导出的name $name = '用户购买商品信息'; $objPHPExcel->getActiveSheet()->setTitle($name); $objActSheet = $objPHPExcel->getActiveSheet(); //设置表头 $headArr = array( '用户ID', '商品ID','商品名称','用户昵称', '用户邮箱', '联系方式','支付状态', '支付时间', '支付金额', '收货地址' ); $index = ord('A'); foreach($headArr as $v){ $column = chr($index); $objActSheet->setCellValue($column.'1', $v); $index++; } //设置单元格的宽度 $objActSheet->getColumnDimension('A')->setWidth(8); $objActSheet->getColumnDimension('B')->setWidth(18); $objActSheet->getColumnDimension('C')->setWidth(18); $objActSheet->getColumnDimension('C')->setWidth(18); $objActSheet->getColumnDimension('E')->setWidth(20); $objActSheet->getColumnDimension('F')->setWidth(12); $objActSheet->getColumnDimension('G')->setWidth(15); $objActSheet->getColumnDimension('H')->setWidth(22); $objActSheet->getColumnDimension('I')->setWidth(20); $objActSheet->getColumnDimension('I')->setWidth(20); $orderList = array_values($result); foreach ($orderList as $key => $info) { if (0 == $info['status']) { $statusMsg = '未支付'; } elseif( 1 == $info['status']) { $statusMsg = '支付成功'; } else { $statusMsg = '支付失败'; } $i = $key + 2; $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $info['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $info['goods_id']); $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $info['goods_name']); $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $info['user_name']); $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $info['user_email']); $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $info['user_phone']); $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $statusMsg); $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $info['pay_time']); $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $info['amount']); $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $info['address']); } // 输出 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $name . '.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output');