分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

PHP导出excel文件的多种方式

发布时间:2023-09-06 01:23责任编辑:傅花花关键词:excelPHP


1、第一种实现的方法

set_time_limit(0); //逐条导出数据ob_end_clean();header("Content-type: application/vnd.ms-excel");header(‘Content-Disposition: attachment; filename="文章信息统计‘.date(‘YmdHis‘).‘.xls"‘);$fp = fopen(‘php://output‘, ‘w‘);fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));$title = array(‘文章标题‘,‘平台‘,‘名称(id)‘,‘分类‘,‘发布时间‘,‘文章位置‘);fputcsv($fp, $title, "\t");//查询出要导出的内容 此处可根据自己要查询的内容做修改$carwlInfo = $crawlModel->setFields(‘*‘)->where($where)->order($order)->select();$body = array();foreach($carwlInfo as $key=>$val){ ???$body[‘title‘] = $val[‘title‘]; ???$body[‘name‘] = $val[‘name‘]; ???$body[‘type‘] = $val[‘type‘]; ???$body[‘behotTime‘] = $val[‘behotTime‘]; ???$body[‘position‘] = $val[‘position‘]; ???fputcsv($fp, $body, "\t");}

 2、第二种实现的方法:

//输出的文件类型为excelheader("Content-type:application/vnd.ms-excel");//提示下载header("Content-Disposition:attachement;filename=Report_".date("Ymd").".xls");//报表数据$ReportArr = array( ???array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘), ???array(‘文章id‘,‘文章标题‘,‘文章url‘,‘文章发布时间‘,‘文章相似数‘),);$ReportContent = ‘‘;$num1 = count($ReportArr);for ($i=0; $i<$num1; $i++) { ???$num2 = count($ReportArr[$i]); ???for ($j=0; $j<$num2; $j++) { ???????//ecxel都是一格一格的,用\t将每一行的数据连接起来 ???????$ReportContent .= ‘"‘.$ReportArr[$i][$j].‘"‘."\t"; ???} ???//最后连接\n 表示换行 ???$ReportContent .= "\n";}//用的utf-8 最后转换一个编码为gb$ReportContent = mb_convert_encoding($ReportContent, "gb2312", "utf-8");//输出即提示下载echo $ReportContent;

PHP导出excel文件的多种方式

原文地址:http://www.cnblogs.com/php-studys/p/7810600.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved