php怎么导出Excel?
程序员文章站
2022-05-13 16:43:42
...
php导出excel的方法
方法一:直接用头部信息输出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中文网!!
上一篇: PHP实现邮件群发机_PHP教程
推荐阅读
-
php中使用PHPExcel读写excel(xls)文件的方法
-
建议收藏:.net core 使用EPPlus导入导出Excel详细案例,精心整理源码已更新至开源模板
-
php数组怎么根据其中的一个值进行排序?解决方法
-
详解PHP导入导出CSV文件
-
fputcsv 导出excel,解决内存、性能、乱码、科学计数法问题
-
C#_.NetCore_WebAPI项目_EXCEL数据导出(ExcelHelper_第二版_优化逻辑)
-
ABBYY FineReader14怎么新建Excel电子表格?
-
C#使用NPOI将List数据导出到Excel文档
-
3ds max渲染图片正常但导出图片颜色很淡该怎么办?
-
CorelDRAW导出JPG偏黄色图片严重失真该怎么办?