浏览器正常导出文件phpecxel
【摘要】
header("Content-type: text/html; charset=utf-8");require "./plugin/phpexcel-1.8/Classes/PHPExcel/IOFactory.php";require_once './plugin/phpexcel-1.8/Classes/PHPExcel.php...
-
-
header("Content-type: text/html; charset=utf-8");
-
require "./plugin/phpexcel-1.8/Classes/PHPExcel/IOFactory.php";
-
require_once './plugin/phpexcel-1.8/Classes/PHPExcel.php';
-
require_once './plugin/phpexcel-1.8/Classes/PHPExcel/Writer/Excel2007.php';
-
-
/**
-
* 创建(导出)Excel数据表格
-
* @param array $list 要导出的数组格式的数据
-
* @param string $filename 导出的Excel表格数据表的文件名
-
* @param array $indexKey $list数组中与Excel表格表头$header中每个项目对应的字段的名字(key值)
-
* @param array $startRow 第一条数据在Excel表格中起始行
-
* @param [bool] $excel2007 是否生成Excel2007(.xlsx)以上兼容的数据表
-
* 比如: $indexKey与$list数组对应关系如下:
-
* $indexKey = array('id','username','sex','age');
-
* $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24));
-
*/
-
/*function exportExcel($list,$filename,$indexKey,$startRow=1,$excel2007=false){
-
-
if(empty($filename)) $filename = time();
-
if( !is_array($indexKey)) return false;
-
-
$header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
-
//初始化PHPExcel()
-
$objPHPExcel = new PHPExcel();
-
-
//设置保存版本格式
-
if($excel2007){
-
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
-
$filename = $filename.'.xlsx';
-
}else{
-
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
-
$filename = $filename.'.xls';
-
}
-
//接下来就是写数据到表格里面去
-
$objActSheet = $objPHPExcel->getActiveSheet();
-
$objActSheet->setCellValue('A2', "活动名称:江南极客");
-
$objActSheet->setCellValue('C2', "导出时间:".date('Y-m-d H:i:s'));
-
-
-
//$startRow = 1;
-
foreach ($list as $row) {
-
foreach ($indexKey as $key => $value){
-
//这里是设置单元格的内容
-
$objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);
-
}
-
$startRow++;
-
}
-
-
// 下载这个表格,在浏览器输出
-
// header("Pragma: public");
-
// header("Expires: 0");
-
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
-
header("Content-Type:application/force-download");
-
header("Content-Type:application/vnd.ms-execl");
-
header("Content-Type:application/octet-stream");
-
header("Content-Type:application/download");;
-
header('Content-Disposition:attachment;filename='.$filename.'');
-
header("Content-Transfer-Encoding:binary");
-
$res=$objWriter->save('php://output');
-
return $res;
-
}*/
-
-
function exportExcel2($list,$filename,$indexKey=array()){
-
$header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
-
-
//$objPHPExcel = new PHPExcel(); //初始化PHPExcel(),不使用模板
-
$template = 'D:/Download/test.xlsx'; //使用模板
-
// $template = dirname(__FILE__).'/template.xls'; //使用模板
-
$objPHPExcel = PHPExcel_IOFactory::load($template); //加载excel文件,设置模板
-
-
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); //设置保存版本格式
-
-
//接下来就是写数据到表格里面去
-
$objActSheet = $objPHPExcel->getActiveSheet();
-
$objActSheet->setCellValue('A2', "活动名称:江南极客");
-
$objActSheet->setCellValue('C2', "导出时间:".date('Y-m-d H:i:s'));
-
$i = 4;
-
foreach ($list as $row) {
-
foreach ($indexKey as $key => $value){
-
//这里是设置单元格的内容
-
$objActSheet->setCellValue($header_arr[$key].$i,$row[$value]);
-
}
-
$i++;
-
}
-
-
// 1.保存至本地Excel表格
-
//$objWriter->save($filename.'.xls');
-
-
// 2.接下来当然是下载这个表格了,在浏览器输出就好了
-
header("Pragma: public");
-
header("Expires: 0");
-
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
-
header("Content-Type:application/force-download");
-
header("Content-Type:application/vnd.ms-execl");
-
header("Content-Type:application/octet-stream");
-
header("Content-Type:application/download");;
-
header('Content-Disposition:attachment;filename="'.$filename.'.xls"');
-
header("Content-Transfer-Encoding:binary");
-
$objWriter->save('php://output');
-
}
-
-
$indexKey = array('id','username','sex','age');
-
$data = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24));
-
exportExcel2($data,$filename='test',$indexKey);
-
// exportExcel($data,$filename='test',$indexKey,$startRow=1,$excel2007=true);
文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/lxw1844912514/article/details/120788409
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)