PHP数据导出Excel

需要将php数据导出Exel表格中,可以用PHPExcel,但是也有更快的方法,但是会出现些小问题

比如:

/* 
*处理Excel导出 
*@param $datas array 设置表格数据 
*@param $titlename string 设置head 
*@param $title string 设置表头 
*/ 
/**
* 
*/

function excelData($datas,$titlename,$title,$filename){ 
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; 
    $str .= $title; 
    $str .="<table border=1><head>".$titlename."</head>"; 
    foreach ($datas  as $key=> $rt ) 
    { 
        $str .= "<tr>"; 
        foreach ( $rt as $k => $v ) 
        { 
            $str .= "<td>{$v}</td>"; 
        } 
        $str .= "</tr>\n"; 
    } 
    $str .= "</table></body></html>"; 
    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 ); 
} 
  

实例:

$dataResult=[[1,2,3,4,5,6]];  //导出数据
$headTitle = "测试excel项目记录"; 
$title = "测试excel"; 
$headtitle= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>"; 
$titlename = "<tr> 
               <th style='width:70px;' >项目1</th> 
               <th style='width:70px;' >项目2</th> 
               <th style='width:70px;'>项目3</th> 
               <th style='width:150px;'>项目4</th> 
               <th style='width:70px;'>项目5</th> 
               <th style='width:70px;'>项目6</th> 
           </tr>"; 
           $filename = $title.".xls"; 
excelData($dataResult,$titlename,$headtitle,$filename); 



实践可以使用,自己封装

猜你喜欢

转载自blog.csdn.net/a519395243/article/details/78558683