ITPub博客

首页 > IT职业 > IT职场 > 用php把数据导出excel

用php把数据导出excel

IT职场 作者:dead_lee 时间:2021-09-09 09:06:02 0 删除 编辑

一:先获取数据

<!--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 .= "";
        }
        $str .= "n";
    }
    $str .= "
'{$v}
"; 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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2021-06-25

  • 博文量
    82
  • 访问量
    15432