phpexecel自己的使用记录

2018-06-22 05:17:10来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

$user_sql = "select * from `table`";

$user_list = $db->fetchAll($user_sql);//数据库查询

require_once dirname(__FILE__) . '/../../class/PHPExcel.php';//加载phpexcel.php,根据自己项目的路径调整

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");

//设置标题名称
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '编号')
->setCellValue('B1', '在线人数')
->setCellValue('C1', '手机')

->setCellValue('D1', '记录时间');  

//循环每个数据,如果遇到手机号码的数据导出,可以在对应的数据前、后添加一个空格,达到将数据变成字符串的方式,令execl不是讲其当成科学数据法的方式显示出来
foreach ($user_list as $k => $v) {
$num=$k+2;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$num, $v['id'])
->setCellValue('B'.$num, $v['online'])

->setCellValue('C'.$num, ' '.$v['phone_num'].' ')
->setCellValue('D'.$num, date('Y-m-d H:i:s',$v['dateline']));
}


$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);//对某列单元格设置宽度


// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('sheet1');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

$time_name = '在线人数'.date('YmdHis',time()).'.xls';//设置下载时文件的默认名

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename='$time_name'");
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
die;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:phpmailer发送邮件

下一篇:php头像上传预览