首页 > IT职业 > IT职场 > 用php把数据导出excel
一:先获取数据
<!--more-->
public function output($dataResult=array()){
$headTitle = date('YmdHis');
$title = date('YmdHis');
$headtitle= "{$headTitle} ";
$titlename = '';
$headtitle = "
ID
条码号
商品组合
项目明细
病人
医生姓名
医生折扣
代理姓名
业务员
原价
实付
退款
日期
";
$filename = $title.".xls";
$this->excelData($dataResult,$titlename,$headtitle,$filename);
} 二:输出
/*
*处理Excel导出
*@param $datas array 设置表格数据
*@param $titlename string 设置head
*@param $title string 设置表头
*/
private function excelData($datas,$titlename,$title,$filename){
$str = "rnrnrnrn";
$str .="
".$titlename."";
$str .= $title;
foreach ($datas as $key=> $rt )
{
$str .= "";
foreach ( $rt as $k => $v )
{
$str .= "'{$v} ";
}
$str .= " n";
}
$str .= "
";
header("Content-type:application/vnd.ms-excel");
//header( "Content-Type: application/vnd.ms-excel; name='excel'" );
//header( "Content-type: application/octet-stream" );
header( "Content-Disposition: attachment; filename=".$filename );
//header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Pragma: no-cache" );
header( "Expires: 0" );
exit( $str );
} 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4686/viewspace-2799408/ ,如需转载,请注明出处,否则将追究法律责任。