PHPexcel 下载
<?php
$dsn="mysql:dbname=me;host=127.0.0.1;charset=utf8";
//连接mysql数据库
$user="root";
$pwd=‘root‘;
$pdo=new PDO($dsn,$user,$pwd);
$sql="select id,name,content,decs from goods";
$psd=$pdo->prepare($sql);
$psd->execute();
//echo "<pre>";
$arr=$psd->fetchAll(PDO::FETCH_ASSOC);
//引入类文件
require_once ‘PHPExcel.php‘;
require_once ‘PHPExcel/IOFactory.php‘;
require_once ‘PHPExcel/Reader/Excel5.php‘;
//创建新的PHPExcel对象
$objPHPExcel = new PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
// $key = ord("A");//ord() 函数返回字符串第一个字符的 ASCII 值。
$i=0;
foreach($arr as $k=>$v){
//print_r($v);die;
//array_flip();
//$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $v);//setCellValue()单元格赋值;
//$key += 1;
if($i<1){
//返回数组中的所有键名
$arrd=array_keys($v);
//print_r($arrd);die;
//返回字符的ASCII码值
$key = ord("A");
foreach($arrd as $ka=>$va){
//print_r($arrd);die;
//返回指定字符
$colum = chr($key);
//print_r($colum);die;
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.‘1‘, $va);//setCellValue()单元格赋值;
$key+=1;
}
}
$i++;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
foreach($arr as $key => $rows){ //行写入
//print_r($rows);die;
$span = ord("A");
foreach($rows as $keyName=>$value){// 列写入
$h = chr($span);
$objActSheet->setCellValue($h.$column, $value);
$span++;
}
$column++;
}
//写入类容
//$obwrite = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);
//保存文件(直接生成文件)
//$obwrite->save(‘shuju.xls‘);
//下载
//将输出重定向到一个客户端web浏览器(Excel2007)
$fileName = iconv("utf-8", "gbk", "haha.xls");
//header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
header(‘Content-Type: application/vnd.ms-excel; charset=gbk‘);
header("Content-Disposition: attachment; filename=\"$fileName\"");
header(‘Cache-Control: max-age=0‘);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);
$objWriter->save(‘php://output‘); //文件通过浏览器下载
?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。