方法一:直接用头部信息输出excel格式文件,内容以表格形式展示
//直接用头部信息输出excel格式文件,内容以表格形式展示。 $filename='orderexcel'; header("Content-type: application/vnd.ms-excel; charset=gbk"); header("Content-Disposition: attachment; filename=$filename.xls"); //$list为数据库查询结果,既二维数组。利用循环出表格,直接输出,既在线生成execl文件 foreach($list as $key => $val) { $data .= "<table border='1'>"; $data .= "<tr><td colspan='2'>订单号:".$val['order_sn']. "</td><td>用户名:".$val['user_name']."</td><td colspan='2'>收货人:". $val['consignee']."</td><td colspan='2'>联系电话:".$val['tel']."</td></tr>"; $data .= "<tr><td colspan='5'>送货地址:".$val['address']. "</td><td colspan='2'>下单时间:".$val['add_time']."</td></tr>"; $data .= "<tr bgcolor='#999999'><th>序号</th><th>货号</th><th>商品名称</th><th>市场价</th> <th>本店价</th><th>购买数量</th><th>小计</th></tr>"; $data .= "<tr><th>1</th><th>".$val['goods_sn']."</th><th>".$val['goods_name']."</th><th>". $val['market_price']."</th><th>".$val['goods_price']."</th><th>".$val['goods_number']."</th><th>". $val['money']."</th></tr>"; $data .= "</table>"; $data .= "<br>"; } $data.='</table>'; if (EC_CHARSET != 'gbk') { echo yzy_iconv(EC_CHARSET, 'gbk', $data) . "\t"; } else { echo $data. "\t"; }
方法二:利用excel导出插件PHPExcel
//利用excel导出插件PHPExcel // 引入phpexcel核心类文件 require_once ROOT_PATH . '/includes/phpexcel/Classes/PHPExcel.php'; // 实例化excel类 $objPHPExcel = new PHPExcel(); // 操作第一个工作表 $objPHPExcel->setActiveSheetIndex(0); // 设置sheet名 $objPHPExcel->getActiveSheet()->setTitle('xx列表'); // 设置表格宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(5); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20); // 列名表头文字加粗 $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true); // 列表头文字居中 $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 列名赋值 $objPHPExcel->getActiveSheet()->setCellValue('A1', '编号'); $objPHPExcel->getActiveSheet()->setCellValue('B1', '姓名'); $objPHPExcel->getActiveSheet()->setCellValue('C1', '电话'); $objPHPExcel->getActiveSheet()->setCellValue('D1', '擅长'); $objPHPExcel->getActiveSheet()->setCellValue('E1', '创建日期'); $objPHPExcel->getActiveSheet()->setCellValue('F1', '审核'); $objPHPExcel->getActiveSheet()->setCellValue('G1', '审核时间'); // 数据起始行 $row_num = 2; // 向每行单元格插入数据 foreach($res as $value) { // 设置所有垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'J' . $row_num)->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 设置价格为数字格式 $objPHPExcel->getActiveSheet()->getStyle('D' . $row_num)->getNumberFormat() ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); // 居中 $objPHPExcel->getActiveSheet()->getStyle('E' . $row_num . ':' . 'H' . $row_num)->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 设置单元格数值 $objPHPExcel->getActiveSheet()->setCellValue('A' . $row_num, $value['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row_num, $value['teacher_name']); $objPHPExcel->getActiveSheet()->setCellValue('C' . $row_num, $value['teacher_mobile']); $objPHPExcel->getActiveSheet()->setCellValue('D' . $row_num, $value['teacher_desc']); $objPHPExcel->getActiveSheet()->setCellValue('E' . $row_num, date('Y-m-d h:i:s',$value['createtime'])); $objPHPExcel->getActiveSheet()->setCellValue('F' . $row_num, $value['state'] ? '√' : '×'); $objPHPExcel->getActiveSheet()->setCellValue('G' . $row_num, date('Y-m-d h:i:s',$value['statetime'])); $row_num++; } $outputFileName = 'teacher_' . time() . '.xls'; $xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="' . $outputFileName . '"'); header("Content-Transfer-Encoding: binary"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); $xlsWriter->save("php://output"); echo file_get_contents($outputFileName);
推荐教程:PHP视频教程
Atas ialah kandungan terperinci php怎么导出excel. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!