PHP导出Excel实例讲解,导出excel实例讲解
本次实现PHP导出Excel文件使用的是PHP开源程序PHPExcel,部分关键代码分享给大家,具体内容如下
<?php error_reporting(E_ALL); date_default_timezone_set('Asia/Shanghai'); require_once './Classes/PHPExcel.php'; $data=array( 0=>array( 'id'=>1001, 'username'=>'张飞', 'password'=>'123456', 'address'=>'三国时高老庄250巷101室' ), 1=>array( 'id'=>1002, 'username'=>'关羽', 'password'=>'123456', 'address'=>'三国时花果山' ), 2=>array( 'id'=>1003, 'username'=>'曹操', 'password'=>'123456', 'address'=>'延安西路2055弄3号' ), 3=>array( 'id'=>1004, 'username'=>'刘备', 'password'=>'654321', 'address'=>'愚园路188号3309室' ) ); $objPHPExcel=new PHPExcel(); $objPHPExcel->getProperties()->setCreator('http://www.bkjia.com') ->setLastModifiedBy('http://www.bkjia.com') ->setTitle('Office 2007 XLSX Document') ->setSubject('Office 2007 XLSX Document') ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office 2007 openxml php') ->setCategory('Result file'); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1','ID') ->setCellValue('B1','用户名') ->setCellValue('C1','密码') ->setCellValue('D1','地址'); $i=2; foreach($data as $k=>$v){ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,$v['id']) ->setCellValue('B'.$i,$v['username']) ->setCellValue('C'.$i,$v['password']) ->setCellValue('D'.$i,$v['address']); $i++; } $objPHPExcel->getActiveSheet()->setTitle('三年级2班'); $objPHPExcel->setActiveSheetIndex(0); $filename=urlencode('学生信息统计表').'_'.date('Y-m-dHis'); //生成xlsx文件 /* 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($objPHPExcel,'Excel2007'); */ //生成xls文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;
注意:如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,代码类似如下:
查看代码打印
$str=mb_convert_encoding("gb2312","UTF-8",$str);
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:
- php将数据库导出成excel的方法
- PHP导出MySQL数据到Excel文件(fputcsv)
- phpExcel导出大量数据出现内存溢出错误的解决方法
- 基于PHP导出Excel的小经验 完美解决乱码问题
- 基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
- php导入导出excel实例
- 利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
- 利用phpexcel把excel导入数据库和数据库导出excel实现
- ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

SublimeText3 Chinese version
Chinese version, very easy to use