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中文网!!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft